Skip to content

Commit b15477d

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

File tree

3 files changed

+6
-24
lines changed

3 files changed

+6
-24
lines changed

sphinx/writers/latex.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,30 +1843,10 @@ def add_target(id: str) -> None:
18431843
and node['refid'] == prev_node['refid']
18441844
):
18451845
# a target for a hyperlink reference having alias
1846-
pass
1846+
return
18471847
else:
18481848
add_target(node['refid'])
1849-
# Temporary fix for https://github.com/sphinx-doc/sphinx/issues/11093
1850-
# TODO: investigate if a more elegant solution exists
1851-
# (see comments of https://github.com/sphinx-doc/sphinx/issues/11093)
1852-
if node.get('ismod', False):
1853-
# Detect if the previous nodes are label targets. If so, remove
1854-
# the refid thereof from node['ids'] to avoid duplicated ids.
1855-
def has_dup_label(sib: Node | None) -> bool:
1856-
return isinstance(sib, nodes.target) and sib.get('refid') in node['ids']
1857-
1858-
prev = get_prev_node(node)
1859-
if has_dup_label(prev):
1860-
ids = node['ids'][:] # copy to avoid side-effects
1861-
while has_dup_label(prev):
1862-
ids.remove(prev['refid']) # type: ignore[index]
1863-
prev = get_prev_node(prev) # type: ignore[arg-type]
1864-
else:
1865-
ids = iter(node['ids']) # read-only iterator
1866-
else:
1867-
ids = iter(node['ids']) # read-only iterator
1868-
1869-
for id in ids:
1849+
for id in node['ids']:
18701850
add_target(id)
18711851

18721852
def depart_target(self, node: Element) -> None:

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)