From da4e41639461347ffe93810b2e70ab8a8461876f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20B=2E?= <2589111+jfbu@users.noreply.github.com> Date: Thu, 5 Jun 2025 10:18:11 +0200 Subject: [PATCH] LaTeX: revisit and trim visit_target() Refs: #13609, #11093 (#11333), #6026 (#6051). - Fix misnomer in test-latex-labels/index.rst text. Refs: https://github.com/sphinx-doc/sphinx/issues/13609#issuecomment-2934964031 - Patch writers/latex.py as per the proposal at end of: https://github.com/sphinx-doc/sphinx/issues/13609#issuecomment-2937087733 - Update test_latex_build.py::test_latex_labels to detect non desired extra label in LaTeX output. Nota bene: this commit does not revert "Tests: update LaTeX label test expectations from Docutils r10151 (#13610)" (commit 68d56109ff50dd81dd31d4a01e3dccbd006c50ee) as it is still necessary for compatibility with both Docutils 0.21.2 and Docutils HEAD at their r10151. Rebased on e1bd9cb3863c with resolved Conflicts: sphinx/writers/latex.py --- sphinx/writers/latex.py | 25 ++----------------------- tests/roots/test-latex-labels/index.rst | 2 +- tests/test_builders/test_build_latex.py | 4 +++- 3 files changed, 6 insertions(+), 25 deletions(-) diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 39aef55ddfe..98d8cff0de1 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -1889,36 +1889,15 @@ def add_target(id: str) -> None: and node['refid'] == prev_node['refid'] ): # a target for a hyperlink reference having alias - pass + return else: add_target(node['refid']) - # Temporary fix for https://github.com/sphinx-doc/sphinx/issues/11093 - # TODO: investigate if a more elegant solution exists - # (see comments of https://github.com/sphinx-doc/sphinx/issues/11093) - if node.get('ismod', False): - # Detect if the previous nodes are label targets. If so, remove - # the refid thereof from node['ids'] to avoid duplicated ids. - prev = get_prev_node(node) - if self._has_dup_label(prev, node): - ids = node['ids'][:] # copy to avoid side-effects - while self._has_dup_label(prev, node): - ids.remove(prev['refid']) # type: ignore[index] - prev = get_prev_node(prev) # type: ignore[arg-type] - else: - ids = iter(node['ids']) # read-only iterator - else: - ids = iter(node['ids']) # read-only iterator - - for id in ids: + for id in node['ids']: add_target(id) def depart_target(self, node: Element) -> None: pass - @staticmethod - def _has_dup_label(sib: Node | None, node: Element) -> bool: - return isinstance(sib, nodes.target) and sib.get('refid') in node['ids'] - def visit_attribution(self, node: Element) -> None: self.body.append(CR + r'\begin{flushright}' + CR) self.body.append('---') diff --git a/tests/roots/test-latex-labels/index.rst b/tests/roots/test-latex-labels/index.rst index 0021d5d4215..4abf72e2e03 100644 --- a/tests/roots/test-latex-labels/index.rst +++ b/tests/roots/test-latex-labels/index.rst @@ -69,6 +69,6 @@ subsubsection otherdoc -* Embedded standalone hyperlink reference: `subsection `_. +* Named hyperlink reference with embedded alias reference: `subsection `_. .. See: https://github.com/sphinx-doc/sphinx/issues/5948 diff --git a/tests/test_builders/test_build_latex.py b/tests/test_builders/test_build_latex.py index 16f3437c154..cf7af44a7a7 100644 --- a/tests/test_builders/test_build_latex.py +++ b/tests/test_builders/test_build_latex.py @@ -2019,9 +2019,11 @@ def test_latex_labels(app: SphinxTestApp) -> None: r'\label{\detokenize{otherdoc::doc}}' ) in result - # Embedded standalone hyperlink reference + # Named hyperlink reference with embedded alias reference # See: https://github.com/sphinx-doc/sphinx/issues/5948 assert result.count(r'\label{\detokenize{index:section1}}') == 1 + # https://github.com/sphinx-doc/sphinx/issues/13609 + assert r'\phantomsection\label{\detokenize{index:id' not in result @pytest.mark.sphinx('latex', testroot='latex-figure-in-admonition')