Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exception for SVG images #97

Merged
merged 9 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion docs/source/socialcards.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,31 @@ ogp_social_cards = {
}
```

## Update the top-right image

By default the top-right image will use the image specified by `html_logo` if it exists.
To update it, specify another path in the **`image`** key like so:

```{code-block} python
:caption: conf.py

ogp_social_cards = {
"image": "path/to/image.png",
}
```

```{admonition} The image cannot be an SVG
:class: warning

Matplotlib does not support easy plotting of SVG images, so ensure that your image is a PNG or JPEG file, not SVG.
```

## Customize the card

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

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

Expand Down
23 changes: 20 additions & 3 deletions sphinxext/opengraph/socialcards.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import matplotlib
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
from sphinx.util import logging

matplotlib.use("agg")


LOGGER = logging.getLogger(__name__)
HERE = Path(__file__).parent
MAX_CHAR_PAGE_TITLE = 75
MAX_CHAR_DESCRIPTION = 175
Expand Down Expand Up @@ -91,6 +92,22 @@ def create_social_card(
Path(__file__).parent / "_static/sphinx-logo-shadow.png"
)

# Validation on the images
for img in ["image_mini", "image"]:
impath = kwargs_fig.get(img)
if not impath:
continue

# If image is an SVG replace it with None
if impath.suffix.lower() == ".svg":
LOGGER.warn(f"[Social card] {img} cannot be an SVG image, skipping...")
kwargs_fig[img] = None

# If image doesn't exist, throw a warning and replace with none
if not impath.exists():
LOGGER.warn(f"[Social card]: {img} file doesn't exist, skipping...")
kwargs_fig[img] = None

# These are passed directly from the user configuration to our plotting function
pass_through_config = ["text_color", "line_color", "background_color", "font"]
for config in pass_through_config:
Expand Down Expand Up @@ -265,12 +282,12 @@ def create_social_card_objects(
c=site_url_color,
)

if image_mini:
if isinstance(image_mini, Path):
img = mpimg.imread(image_mini)
axim_mini.imshow(img)

# Put the logo in the top right if it exists
if image:
if isinstance(image, Path):
img = mpimg.imread(image)
yw, xw = img.shape[:2]

Expand Down
12 changes: 12 additions & 0 deletions tests/roots/test-social-cards-svg/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
extensions = ["sphinxext.opengraph"]

master_doc = "index"
exclude_patterns = ["_build"]

html_theme = "basic"
ogp_site_url = "http://example.org/en/latest/"

# The image is an SVG, and so it should not be included in the social cards
ogp_social_cards = {
"image": "foo.svg",
}
1 change: 1 addition & 0 deletions tests/roots/test-social-cards-svg/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +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.
6 changes: 6 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ def test_local_image(og_meta_tags):
)


@pytest.mark.sphinx("html", testroot="social-cards-svg")
def test_social_cards_svg(app: Sphinx, og_meta_tags):
"""If the social cards image is an SVG, it should not be in the social card."""
assert app.statuscode == 0


@pytest.mark.sphinx("html", testroot="image")
def test_image_alt(og_meta_tags):
assert get_tag_content(og_meta_tags, "image:alt") == "Example's Docs!"
Expand Down