Skip to content

Commit 6deac99

Browse files
authored
Add exception for SVG images (#97)
* Add exception for SVG images * posix to str * Mini svg as well * Document image svg * Lower * Better social image path validation * Fix test * Black * Check for None
1 parent 99f2521 commit 6deac99

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

docs/source/socialcards.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,31 @@ ogp_social_cards = {
2323
}
2424
```
2525

26+
## Update the top-right image
27+
28+
By default the top-right image will use the image specified by `html_logo` if it exists.
29+
To update it, specify another path in the **`image`** key like so:
30+
31+
```{code-block} python
32+
:caption: conf.py
33+
34+
ogp_social_cards = {
35+
"image": "path/to/image.png",
36+
}
37+
```
38+
39+
```{admonition} The image cannot be an SVG
40+
:class: warning
41+
42+
Matplotlib does not support easy plotting of SVG images, so ensure that your image is a PNG or JPEG file, not SVG.
43+
```
44+
2645
## Customize the card
2746

2847
There are several customization options to change the text and look of the social media preview card.
2948
Below is a summary of these options.
3049

3150
- **`site_url`**: Set a custom site URL.
32-
- **`image`**: Over-ride the top-right image (by default, `html_logo` is used).
3351
- **`line_color`**: Color of the border line at the bottom of the card, in hex format.
3452
% TODO: add an over-ride for each part of the card.
3553

sphinxext/opengraph/socialcards.py

+20-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
import matplotlib
55
from matplotlib import pyplot as plt
66
import matplotlib.image as mpimg
7+
from sphinx.util import logging
78

89
matplotlib.use("agg")
910

10-
11+
LOGGER = logging.getLogger(__name__)
1112
HERE = Path(__file__).parent
1213
MAX_CHAR_PAGE_TITLE = 75
1314
MAX_CHAR_DESCRIPTION = 175
@@ -91,6 +92,22 @@ def create_social_card(
9192
Path(__file__).parent / "_static/sphinx-logo-shadow.png"
9293
)
9394

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+
94111
# These are passed directly from the user configuration to our plotting function
95112
pass_through_config = ["text_color", "line_color", "background_color", "font"]
96113
for config in pass_through_config:
@@ -265,12 +282,12 @@ def create_social_card_objects(
265282
c=site_url_color,
266283
)
267284

268-
if image_mini:
285+
if isinstance(image_mini, Path):
269286
img = mpimg.imread(image_mini)
270287
axim_mini.imshow(img)
271288

272289
# Put the logo in the top right if it exists
273-
if image:
290+
if isinstance(image, Path):
274291
img = mpimg.imread(image)
275292
yw, xw = img.shape[:2]
276293

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
extensions = ["sphinxext.opengraph"]
2+
3+
master_doc = "index"
4+
exclude_patterns = ["_build"]
5+
6+
html_theme = "basic"
7+
ogp_site_url = "http://example.org/en/latest/"
8+
9+
# The image is an SVG, and so it should not be included in the social cards
10+
ogp_social_cards = {
11+
"image": "foo.svg",
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse at lorem ornare, fringilla massa nec, venenatis mi. Donec erat sapien, tincidunt nec rhoncus nec, scelerisque id diam. Orci varius natoque penatibus et magnis dis parturient mauris.

tests/test_options.py

+6
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ def test_local_image(og_meta_tags):
8787
)
8888

8989

90+
@pytest.mark.sphinx("html", testroot="social-cards-svg")
91+
def test_social_cards_svg(app: Sphinx, og_meta_tags):
92+
"""If the social cards image is an SVG, it should not be in the social card."""
93+
assert app.statuscode == 0
94+
95+
9096
@pytest.mark.sphinx("html", testroot="image")
9197
def test_image_alt(og_meta_tags):
9298
assert get_tag_content(og_meta_tags, "image:alt") == "Example's Docs!"

0 commit comments

Comments
 (0)