Skip to content

Commit

Permalink
Close sphinx-doc#7032: html_scaled_image_link is disabled for individ…
Browse files Browse the repository at this point in the history
…ual image
  • Loading branch information
tk0miya committed Jan 25, 2020
1 parent f8fc607 commit 1b731cd
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Bugs fixed
----------

* #6925: html: Remove redundant type="text/javascript" from <script> elements
* #7032: html: :confval:`html_scaled_image_link` is disabled for images having
``no-scaled-link`` class
* #6906, #6907: autodoc: failed to read the source codes encoeded in cp1251
* #6961: latex: warning for babel shown twice
* #6559: Wrong node-ids are generated in glossary directive
Expand Down
13 changes: 13 additions & 0 deletions doc/usage/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions sphinx/builders/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,13 +836,17 @@ def post_process_images(self, doctree: Node) -> None:

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:
Expand Down
Empty file.
Binary file added tests/roots/test-html_scaled_image_link/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions tests/roots/test-html_scaled_image_link/index.rst
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions tests/test_build_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 1b731cd

Please sign in to comment.