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

Regression: restore extraction of data-URI images from source for builders whose output formats do not support them natively. #12344

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions sphinx/builders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ class Builder:
#: The list of MIME types of image formats supported by the builder.
#: Image files are searched in the order in which they appear here.
supported_image_types: list[str] = []
#: The builder supports remote images or not.
#: The builder can produce output documents that may fetch external images when opened.
supported_remote_images = False
#: The builder supports data URIs or not.
#: The file format produced by the builder allows images to be embedded using data-URIs.
supported_data_uri_images = False

def __init__(self, app: Sphinx, env: BuildEnvironment) -> None:
Expand Down
4 changes: 1 addition & 3 deletions sphinx/transforms/post_transforms/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,8 @@ class DataURIExtractor(BaseImageConverter):
default_priority = 150

def match(self, node: nodes.image) -> bool:
if not self.app.builder.supported_remote_images:
return False
if self.app.builder.supported_data_uri_images is True:
return False
return False # do not transform the image; data URIs are valid in the build output
return node['uri'].startswith('data:')

def handle(self, node: nodes.image) -> None:
Expand Down
5 changes: 5 additions & 0 deletions tests/roots/test-images/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ test-image

.. non-exist remote image
.. image:: http://localhost:7777/NOT_EXIST.PNG

.. a self-contained image within a data URI
This image was generated using ImageMagick 6.9 with the command ``convert -pointsize 32 -font Noto-Sans-Egyptian-Hieroglyphs-Regular caption:$(printf '\U13080') -trim -border 2 -monochrome eoh.png``
.. image:: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACoAAAAjAQAAAADKt6U+AAAAAmJLR0QAAd2KE6QAAAAHdElNRQfoBQIVBgOBlOMTAAAAEGNhTnYAAAAtAAAAOwAAAAEAAAATst46RgAAAJtJREFUCNdNz70KwkAMAOA8iOhjuGh9HB9BCtoTHHwMH0Mc7KWTmx0dHDpovUk6HCil3sUmATHLR/4IAeJA+LEWPmbEeHJMWbTMZDA0CNFn8x1COFPaIHQ55R7hlZGdIjwj2aovRjJbhPvMLNN+r0g2vB7ByIWbHqqVh3LR3lhZWM0qYV8qjU6+lc4J7ZVx4SjEINBKOSinv/+YL1xvsJE6ztdqAAAADHRFWHRjYXB0aW9uAPCTgoD4hdKUAAAAD3RFWHRjYXB0aW9uOmxpbmVzADGoBz2RAAAAAElFTkSuQmCC
:alt: The Eye of Horus in a black font on a white background.
1 change: 1 addition & 0 deletions tests/test_builders/test_build_epub.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ def test_copy_images(app, status, warning):
images = {image.name for image in images_dir.rglob('*')}
images.discard('python-logo.png')
assert images == {
# 'ba30773957c3fe046897111afd65a80b81cad089.png', # epub: image from data:image/png URI in source
'img.png',
'rimg.png',
'rimg1.png',
Expand Down
1 change: 1 addition & 0 deletions tests/test_builders/test_build_html_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def test_copy_images(app, status, warning):
images_dir = Path(app.outdir) / '_images'
images = {image.name for image in images_dir.rglob('*')}
assert images == {
# 'ba30773957c3fe046897111afd65a80b81cad089.png', # html: image from data:image/png URI in source
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep; it's intended as a hint that the same image exists as in the other test_copy_images unit tests, but that it isn't written to file because HTML (and similarly ePub) can natively include img src="data:..." elements.

Probably not required, but if some future change affects the logic, it's possible that all of the relevant test cases would need to be updated, and in that case the cross-reference provided by the comment might be useful.

'img.png',
'rimg.png',
'rimg1.png',
Expand Down
1 change: 1 addition & 0 deletions tests/test_builders/test_build_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1711,6 +1711,7 @@ def test_copy_images(app, status, warning):
}
images.discard('sphinx.png')
assert images == {
'ba30773957c3fe046897111afd65a80b81cad089.png', # latex: image from data:image/png URI in source
'img.pdf',
'rimg.png',
'testimäge.png',
Expand Down
1 change: 1 addition & 0 deletions tests/test_builders/test_build_texinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def test_copy_images(app, status, warning):
images = {image.name for image in images_dir.rglob('*')}
images.discard('python-logo.png')
assert images == {
'ba30773957c3fe046897111afd65a80b81cad089.png', # texinfo: image from data:image/png URI in source
'img.png',
'rimg.png',
'testimäge.png',
Expand Down