Merge pull request #7062 from tk0miya/7032_no-scaled_link

Close #7032: html_scaled_image_link is disabled for individual image
This commit is contained in:
Takeshi KOMIYA 2020-02-22 17:38:04 +09:00 committed by GitHub
commit 8987f3c031
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 55 additions and 6 deletions

View File

@ -53,6 +53,8 @@ Features added
in the documentation.
* #1027: Support backslash line continuation in :rst:dir:`productionlist`.
* #7108: config: Allow to show an error message from conf.py via ``ConfigError``
* #7032: html: :confval:`html_scaled_image_link` will be disabled for images having
``no-scaled-link`` class
Bugs fixed
----------

View File

@ -1357,8 +1357,21 @@ that use Sphinx's HTMLWriter class.
'target' option or scale related options: 'scale', 'width', 'height'.
The default is ``True``.
Document authors can this feature manually with giving ``no-scaled-link``
class to the image:
.. code-block:: rst
.. image:: sphinx.png
:scale: 50%
:class: no-scaled-link
.. versionadded:: 1.3
.. versionchanged:: 2.4
It is disabled for images having ``no-scaled-link`` class
.. confval:: html_math_renderer
The name of math_renderer extension for HTML output. The default is

View File

@ -807,13 +807,17 @@ class StandaloneHTMLBuilder(Builder):
if self.config.html_scaled_image_link and self.html_scaled_image_link:
for node in doctree.traverse(nodes.image):
scale_keys = ('scale', 'width', 'height')
if not any((key in node) for key in scale_keys) or \
isinstance(node.parent, nodes.reference):
# docutils does unfortunately not preserve the
# ``target`` attribute on images, so we need to check
# the parent node here.
if not any((key in node) for key in ['scale', 'width', 'height']):
# resizing options are not given. scaled image link is available
# only for resized images.
continue
elif isinstance(node.parent, nodes.reference):
# A image having hyperlink target
continue
elif 'no-scaled-link' in node['classes']:
# scaled image link is disabled for this node
continue
uri = node['uri']
reference = nodes.reference('', '', internal=True)
if uri in self.images:

Binary file not shown.

After

Width:  |  Height:  |  Size: 65 KiB

View File

@ -0,0 +1,11 @@
test-html_scaled_image_link
===========================
.. image:: img.png
.. image:: img.png
:scale: 50%
.. image:: img.png
:scale: 50%
:class: no-scaled-link

View File

@ -1542,3 +1542,22 @@ def test_validate_html_static_path(app):
]
validate_html_static_path(app, app.config)
assert app.config.html_static_path == ['_static']
@pytest.mark.sphinx(testroot='html_scaled_image_link')
def test_html_scaled_image_link(app):
app.build()
context = (app.outdir / 'index.html').text()
# no scaled parameters
assert re.search('\n<img alt="_images/img.png" src="_images/img.png" />', context)
# scaled_image_link
assert re.search('\n<a class="reference internal image-reference" href="_images/img.png">'
'<img alt="_images/img.png" src="_images/img.png" style="[^"]+" /></a>',
context)
# no-scaled-link class disables the feature
assert re.search('\n<img alt="_images/img.png" class="no-scaled-link"'
' src="_images/img.png" style="[^"]+" />',
context)