From 5ef8da518f308f117b4f9041d2e1b08b861c9923 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Wed, 11 Apr 2018 02:01:03 +0900 Subject: [PATCH] Fix #4784: latex_show_urls assigns incorrect footnote numbers --- CHANGES | 2 ++ sphinx/builders/latex.py | 10 ++++++++++ sphinx/transforms/references.py | 30 ++++++++++++++++++++++++++++++ tests/test_build_latex.py | 9 +++++++++ 4 files changed, 51 insertions(+) create mode 100644 sphinx/transforms/references.py diff --git a/CHANGES b/CHANGES index 89ad3d8d5a0..93377cd201d 100644 --- a/CHANGES +++ b/CHANGES @@ -24,6 +24,8 @@ Bugs fixed different * #4812: autodoc ignores type annotated variables * #4817: wrong URLs on warning messages +* #4784: latex: :confval:`latex_show_urls` assigns incorrect footnote numbers if + hyperlinks exists inside substitutions Testing -------- diff --git a/sphinx/builders/latex.py b/sphinx/builders/latex.py index c66d00d4f8a..3e366ba050d 100644 --- a/sphinx/builders/latex.py +++ b/sphinx/builders/latex.py @@ -24,6 +24,8 @@ from sphinx.environment.adapters.asset import ImageAdapter from sphinx.errors import SphinxError, ConfigError from sphinx.locale import _ +from sphinx.transforms import SphinxTransformer +from sphinx.transforms.references import SubstitutionDefinitionsRemover from sphinx.util import texescape, logging, status_iterator from sphinx.util.console import bold, darkgreen # type: ignore from sphinx.util.docutils import new_document @@ -144,6 +146,7 @@ def write(self, *ignored): docname, toctree_only, appendices=((docclass != 'howto') and self.config.latex_appendices or [])) doctree['tocdepth'] = tocdepth + self.apply_transforms(doctree) self.post_process_images(doctree) logger.info("writing... ", nonl=1) doctree.settings = docsettings @@ -210,6 +213,13 @@ def assemble_doctree(self, indexfile, toctree_only, appendices): pendingnode.replace_self(newnodes) return largetree + def apply_transforms(self, doctree): + # type: (nodes.document) -> None + transformer = SphinxTransformer(doctree) + transformer.set_environment(self.env) + transformer.add_transforms([SubstitutionDefinitionsRemover]) + transformer.apply_transforms() + def finish(self): # type: () -> None self.copy_image_files() diff --git a/sphinx/transforms/references.py b/sphinx/transforms/references.py new file mode 100644 index 00000000000..affe4012b64 --- /dev/null +++ b/sphinx/transforms/references.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +""" + sphinx.transforms.references + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + Docutils transforms used by Sphinx. + + :copyright: Copyright 2007-2018 by the Sphinx team, see AUTHORS. + :license: BSD, see LICENSE for details. +""" + +from docutils import nodes +from docutils.transforms.references import Substitutions + +from sphinx.transforms import SphinxTransform + + +class SubstitutionDefinitionsRemover(SphinxTransform): + """Remove ``substitution_definition node from doctrees. + + .. note:: In Sphinx-1.7, this transform is only used in LaTeX builder. + """ + + # should be invoked after Substitutions process + default_priority = Substitutions.default_priority + 1 + + def apply(self): + # type: () -> None + for node in self.document.traverse(nodes.substitution_definition): + node.parent.remove(node) diff --git a/tests/test_build_latex.py b/tests/test_build_latex.py index e7731f063b8..be9d800b375 100644 --- a/tests/test_build_latex.py +++ b/tests/test_build_latex.py @@ -750,6 +750,15 @@ def test_latex_show_urls_is_no(app, status, warning): '{sphinx-dev@googlegroups.com}\n') in result +@pytest.mark.sphinx( + 'latex', testroot='footnotes', + confoverrides={'latex_show_urls': 'footnote', + 'rst_prolog': '.. |URL| replace:: `text `__'}) +def test_latex_show_urls_footnote_and_substitutions(app, status, warning): + # hyperlinks in substitutions should not effect to make footnotes (refs: #4784) + test_latex_show_urls_is_footnote(app, status, warning) + + @pytest.mark.sphinx('latex', testroot='image-in-section') def test_image_in_section(app, status, warning): app.builder.build_all()