Skip to content

Commit 40a6088

Browse files
committed
Fix size detection
1 parent 1f85095 commit 40a6088

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from __future__ import annotations
2+
3+
import io
4+
import pathlib
5+
6+
extensions = ['sphinxext.opengraph']
7+
8+
master_doc = 'index'
9+
exclude_patterns = ['_build']
10+
11+
html_theme = 'basic'
12+
13+
ogp_site_url = 'http://example.org/en/latest/'
14+
15+
16+
def setup(app):
17+
app.connect('generate-social-card', generate)
18+
19+
20+
def generate(
21+
app,
22+
contents,
23+
check_if_signature_exists,
24+
) -> None | tuple[io.BytesIO, str]:
25+
signature = 'custom-signature'
26+
check_if_signature_exists(signature)
27+
28+
return io.BytesIO(pathlib.Path(app.srcdir / 'pixel.png').read_bytes()), signature
Lines changed: 1 addition & 0 deletions
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.
95 Bytes
Loading

tests/test_options.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def test_image_alt(og_meta_tags):
119119

120120

121121
@pytest.mark.sphinx('html', testroot='simple')
122-
def test_image_social_cards(meta_tags):
122+
def test_image_social_cards(content, meta_tags):
123123
"""Social cards should automatically be added if no og:image is given."""
124124
pytest.importorskip('matplotlib')
125125
# Asserting `in` instead of `==` because of the hash that is generated
@@ -136,6 +136,33 @@ def test_image_social_cards(meta_tags):
136136
assert 'summary_large_image' in get_tag_content(
137137
meta_tags, 'card', kind='name', prefix='twitter'
138138
)
139+
png_file = content.outdir / get_tag_content(meta_tags, 'image').split('/', 5)[-1]
140+
assert png_file.is_file()
141+
# https://en.wikipedia.org/wiki/List_of_file_signatures
142+
assert png_file.read_bytes()[:8] == b'\x89PNG\r\n\x1a\n'
143+
assert get_tag_content(meta_tags, 'image:width') == '1146'
144+
assert get_tag_content(meta_tags, 'image:height') == '600'
145+
146+
147+
@pytest.mark.sphinx('html', testroot='custom-social-card-generation')
148+
def test_image_social_cards_custom(content, meta_tags):
149+
"""Providing a custom generation function for social cards."""
150+
# Asserting `in` instead of `==` because of the hash that is generated
151+
assert (
152+
'http://example.org/en/latest/_images/social_previews/summary_index'
153+
in get_tag_content(meta_tags, 'image')
154+
)
155+
# Make sure the extra tags are in the HTML
156+
assert 'summary_large_image' in get_tag_content(
157+
meta_tags, 'card', kind='name', prefix='twitter'
158+
)
159+
assert get_tag_content(meta_tags, 'image:width') == '1'
160+
assert get_tag_content(meta_tags, 'image:height') == '1'
161+
png_file = content.outdir / get_tag_content(meta_tags, 'image').split('/', 5)[-1]
162+
assert png_file.is_file()
163+
# https://en.wikipedia.org/wiki/List_of_file_signatures
164+
assert png_file.read_bytes()[:8] == b'\x89PNG\r\n\x1a\n'
165+
assert len(png_file.read_bytes()) == 95 # Size of the provided pixel.png
139166

140167

141168
@pytest.mark.sphinx('html', testroot='type')

0 commit comments

Comments
 (0)