Skip to content

Commit da4e416

Browse files
committed
LaTeX: revisit and trim visit_target()
Refs: #13609, #11093 (#11333), #6026 (#6051). - Fix misnomer in test-latex-labels/index.rst text. Refs: #13609 (comment) - Patch writers/latex.py as per the proposal at end of: #13609 (comment) - 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 68d5610) as it is still necessary for compatibility with both Docutils 0.21.2 and Docutils HEAD at their r10151. Rebased on e1bd9cb with resolved Conflicts: sphinx/writers/latex.py
1 parent e1bd9cb commit da4e416

File tree

3 files changed

+6
-25
lines changed

3 files changed

+6
-25
lines changed

sphinx/writers/latex.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,36 +1889,15 @@ def add_target(id: str) -> None:
18891889
and node['refid'] == prev_node['refid']
18901890
):
18911891
# a target for a hyperlink reference having alias
1892-
pass
1892+
return
18931893
else:
18941894
add_target(node['refid'])
1895-
# Temporary fix for https://github.com/sphinx-doc/sphinx/issues/11093
1896-
# TODO: investigate if a more elegant solution exists
1897-
# (see comments of https://github.com/sphinx-doc/sphinx/issues/11093)
1898-
if node.get('ismod', False):
1899-
# Detect if the previous nodes are label targets. If so, remove
1900-
# the refid thereof from node['ids'] to avoid duplicated ids.
1901-
prev = get_prev_node(node)
1902-
if self._has_dup_label(prev, node):
1903-
ids = node['ids'][:] # copy to avoid side-effects
1904-
while self._has_dup_label(prev, node):
1905-
ids.remove(prev['refid']) # type: ignore[index]
1906-
prev = get_prev_node(prev) # type: ignore[arg-type]
1907-
else:
1908-
ids = iter(node['ids']) # read-only iterator
1909-
else:
1910-
ids = iter(node['ids']) # read-only iterator
1911-
1912-
for id in ids:
1895+
for id in node['ids']:
19131896
add_target(id)
19141897

19151898
def depart_target(self, node: Element) -> None:
19161899
pass
19171900

1918-
@staticmethod
1919-
def _has_dup_label(sib: Node | None, node: Element) -> bool:
1920-
return isinstance(sib, nodes.target) and sib.get('refid') in node['ids']
1921-
19221901
def visit_attribution(self, node: Element) -> None:
19231902
self.body.append(CR + r'\begin{flushright}' + CR)
19241903
self.body.append('---')

tests/roots/test-latex-labels/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,6 @@ subsubsection
6969

7070
otherdoc
7171

72-
* Embedded standalone hyperlink reference: `subsection <section1_>`_.
72+
* Named hyperlink reference with embedded alias reference: `subsection <section1_>`_.
7373

7474
.. See: https://github.com/sphinx-doc/sphinx/issues/5948

tests/test_builders/test_build_latex.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2019,9 +2019,11 @@ def test_latex_labels(app: SphinxTestApp) -> None:
20192019
r'\label{\detokenize{otherdoc::doc}}'
20202020
) in result
20212021

2022-
# Embedded standalone hyperlink reference
2022+
# Named hyperlink reference with embedded alias reference
20232023
# See: https://github.com/sphinx-doc/sphinx/issues/5948
20242024
assert result.count(r'\label{\detokenize{index:section1}}') == 1
2025+
# https://github.com/sphinx-doc/sphinx/issues/13609
2026+
assert r'\phantomsection\label{\detokenize{index:id' not in result
20252027

20262028

20272029
@pytest.mark.sphinx('latex', testroot='latex-figure-in-admonition')

0 commit comments

Comments
 (0)