Skip to content

Commit

Permalink
Merge branch 'master' into collapse-union-literals
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Apr 25, 2024
2 parents f4d7e6a + a665468 commit ffbc959
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Bugs fixed
Patch by Will Lachance.
* #12251: Fix ``merge_domaindata()`` in ``sphinx.ext.duration``.
Patch by Matthias Geier.
* #12224: Properly detect WebP files.
Patch by Benjamin Cabé.

Testing
-------
Expand Down
1 change: 1 addition & 0 deletions sphinx/ext/imgconverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ImagemagickConverter(ImageConverter):
('image/gif', 'image/png'),
('application/pdf', 'image/png'),
('application/illustrator', 'image/png'),
('image/webp', 'image/png'),
]

def is_available(self) -> bool:
Expand Down
7 changes: 3 additions & 4 deletions sphinx/transforms/post_transforms/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,6 @@ class ImageConverter(BaseImageConverter):
#: ]
conversion_rules: list[tuple[str, str]] = []

def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)

def match(self, node: nodes.image) -> bool:
if not self.app.builder.supported_image_types:
return False
Expand Down Expand Up @@ -232,10 +229,12 @@ def is_available(self) -> bool:
raise NotImplementedError

def guess_mimetypes(self, node: nodes.image) -> list[str]:
# The special key ? is set for nonlocal URIs.
if '?' in node['candidates']:
return []
elif '*' in node['candidates']:
guessed = guess_mimetype(node['uri'])
path = os.path.join(self.app.srcdir, node['uri'])
guessed = guess_mimetype(path)
return [guessed] if guessed is not None else []
else:
return node['candidates'].keys()
Expand Down
1 change: 1 addition & 0 deletions sphinx/util/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
'.svg': 'image/svg+xml',
'.svgz': 'image/svg+xml',
'.ai': 'application/illustrator',
'.webp': 'image/webp',
}
_suffix_from_mime = {v: k for k, v in reversed(mime_suffixes.items())}

Expand Down
46 changes: 46 additions & 0 deletions tests/test_transforms/test_transforms_post_transforms_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from types import SimpleNamespace

from docutils import nodes

from sphinx.transforms.post_transforms.images import ImageConverter
from sphinx.util.docutils import new_document

WEBP_DATA = (
b'RIFF\xa8\x01\x00\x00WEBPVP8X\n\x00\x00\x00'
b"\x10\x00\x00\x00\x0f\x00\x00\x0f\x00\x00ALPH\xc3\x00\x00\x00\x01'"
b'\xa2\xa8\x91$\xe5z\xe7\x18_\xe7\xdf*\x99\x88\x98\xfftq\x8d\xe0'
b'&0\xe2\xe1\x8bw2\xc8\xc1\x11\\\x83+0\xe8\xb0x\x15\x8ex'
b'Q5\xc1\x08\x0c\x02O\x92\xa0j\xb0U\x19\x1c\xd6\xb6mF/N'
b'\xc6v<\xb6\xedw\xfb\xaf)\xae!\xa2\xffI\xd1\xfd\x8f\x90\xf7\xba'
b'DI$\x1b:%\x914\xf3\x14m\x0e\xc7\xd3\xe5\x16 \xf4\x0b\x14'
b'\xbe\x90\xe1\x83\xb7\x1a2\x9e6\x82\x7f\x1d)~Nv\x08\xfb\x88\x9e'
b'\xb3\x91\xef\x99sF\xe82\x82\xdb\xf8\xccH\xb2\xf7E0} \xfd'
b'6\x17\x8c!2V-\xa5\xd6k#\xbc]\xe3\xa5Y\x15\xd5\x9c\x81'
b'\xa4\xd9n\x96u\x8a\x181\x0f\x8a\xaa,P4\xfa0\x82\xdf\xbak'
b'PR)\xb5-\xcf\xe9T\x14\n\x01\x00\x00\x00VP8 \xbe\x00'
b'\x00\x00\x90\x02\x00\x9d\x01*\x10\x00\x10\x00\x03\x004%\xb0\x02t0'
b'O\x08\x85\x0c|\x03\x1d\x08,\xfd\xe8\x00\xfe\xfdt\xa0\xfd\x02\x9b\x1f'
b'\x8a\xf7C|\x9c7\xf6\xd2\x0c\xaf\xd3\xff5h\xe2\xee\xa7\xbd\xc9o'
b'\x1b\xf4\xaa\xc5c\xae\xba\x9f\x97\x84\xdfA\xa2;\xda[\xe4\xef\xf8\xcb'
b'\xf1\xbd\x7f\xe1\xaf\xfa?\xe5\t\xec\xf4\xbbf_\xff\xaa)\xd9\x7f\xc9'
b'l\xe7\x86\xe6\xac\x97\xb9\xe4\xc6\xf4\x93#\x8c_\xdd\x8f9U \x7f'
b'\x95O\xfc9\xf8\xffo\xd2k\x03\xe8\x9f\xbc\x83\x98fm\xb1\xd5\x13'
b'\xffv\x17\xe6\xb1\xfe]\x8a\xe4\x9fG\xbf\xb3\xfa\xbf\xfe\x1d\x1d\xf3\x12'
b'\x8f\xfe\\\xcf\xc1\xfa\xf9\x18\xc3\xbd\xcf\xcf\x1f\x919\xa0\x01\xfd\x9a\x01'
b'K1,\xde\xbc\xd9{\xaa\xac\x00\x00\x00'
)


def test_guess_mimetype_webp(tmp_path):
document = new_document('<source>')
document.settings.env = SimpleNamespace(app=SimpleNamespace(srcdir=tmp_path))
converter = ImageConverter(document)

file_webp = 'webp-image.webp'
image = nodes.image(uri=file_webp, candidates={'*': file_webp})
assert converter.guess_mimetypes(image) == ['image/webp']

file_dat = 'webp-image.dat'
tmp_path.joinpath(file_dat).write_bytes(WEBP_DATA)
image = nodes.image(uri=file_dat, candidates={'*': file_dat})
assert converter.guess_mimetypes(image) == ['image/webp']
1 change: 1 addition & 0 deletions tests/test_util/test_util_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ def test_restify_type_hints_containers():
"[:py:obj:`None`]")


@pytest.mark.xfail(sys.version_info[:2] <= (3, 11), reason='Needs fixing.')
def test_restify_Annotated():
assert restify(Annotated[str, "foo", "bar"]) == ':py:class:`~typing.Annotated`\\ [:py:class:`str`]'
assert restify(Annotated[str, "foo", "bar"], 'smart') == ':py:class:`~typing.Annotated`\\ [:py:class:`str`]'
Expand Down

0 comments on commit ffbc959

Please sign in to comment.