Skip to content

Commit

Permalink
Regression: restore extraction of data-URI images from source for bui…
Browse files Browse the repository at this point in the history
…lders whose output formats do not support them natively (#12344)
  • Loading branch information
jayaddison committed Jul 3, 2024
1 parent b5b383f commit 778013f
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ Bugs fixed
* #12459: Add valid-type arguments to the ``linkcheck_rate_limit_timeout``
configuration setting.
Patch by James Addison.
* #12331: Resolve data-URI-image-extraction regression from v7.3.0 affecting
builders without native support for data-URIs in their output format.
Patch by James Addison.

Improvements
------------
Expand Down
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:: https://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
'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

0 comments on commit 778013f

Please sign in to comment.