|
4 | 4 | import matplotlib
|
5 | 5 | from matplotlib import pyplot as plt
|
6 | 6 | import matplotlib.image as mpimg
|
| 7 | +from sphinx.util import logging |
7 | 8 |
|
8 | 9 | matplotlib.use("agg")
|
9 | 10 |
|
10 |
| - |
| 11 | +LOGGER = logging.getLogger(__name__) |
11 | 12 | HERE = Path(__file__).parent
|
12 | 13 | MAX_CHAR_PAGE_TITLE = 75
|
13 | 14 | MAX_CHAR_DESCRIPTION = 175
|
@@ -91,6 +92,22 @@ def create_social_card(
|
91 | 92 | Path(__file__).parent / "_static/sphinx-logo-shadow.png"
|
92 | 93 | )
|
93 | 94 |
|
| 95 | + # Validation on the images |
| 96 | + for img in ["image_mini", "image"]: |
| 97 | + impath = kwargs_fig.get(img) |
| 98 | + if not impath: |
| 99 | + continue |
| 100 | + |
| 101 | + # If image is an SVG replace it with None |
| 102 | + if impath.suffix.lower() == ".svg": |
| 103 | + LOGGER.warn(f"[Social card] {img} cannot be an SVG image, skipping...") |
| 104 | + kwargs_fig[img] = None |
| 105 | + |
| 106 | + # If image doesn't exist, throw a warning and replace with none |
| 107 | + if not impath.exists(): |
| 108 | + LOGGER.warn(f"[Social card]: {img} file doesn't exist, skipping...") |
| 109 | + kwargs_fig[img] = None |
| 110 | + |
94 | 111 | # These are passed directly from the user configuration to our plotting function
|
95 | 112 | pass_through_config = ["text_color", "line_color", "background_color", "font"]
|
96 | 113 | for config in pass_through_config:
|
@@ -265,12 +282,12 @@ def create_social_card_objects(
|
265 | 282 | c=site_url_color,
|
266 | 283 | )
|
267 | 284 |
|
268 |
| - if image_mini: |
| 285 | + if isinstance(image_mini, Path): |
269 | 286 | img = mpimg.imread(image_mini)
|
270 | 287 | axim_mini.imshow(img)
|
271 | 288 |
|
272 | 289 | # Put the logo in the top right if it exists
|
273 |
| - if image: |
| 290 | + if isinstance(image, Path): |
274 | 291 | img = mpimg.imread(image)
|
275 | 292 | yw, xw = img.shape[:2]
|
276 | 293 |
|
|
0 commit comments