Skip to content

Commit bda34f9

Browse files
authored
LaTeX: revisit and trim visit_target() (#13616)
1 parent 90fd2a2 commit bda34f9

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
@@ -1864,36 +1864,15 @@ def add_target(id: str) -> None:
18641864
and node['refid'] == prev_node['refid']
18651865
):
18661866
# a target for a hyperlink reference having alias
1867-
pass
1867+
return
18681868
else:
18691869
add_target(node['refid'])
1870-
# Temporary fix for https://github.com/sphinx-doc/sphinx/issues/11093
1871-
# TODO: investigate if a more elegant solution exists
1872-
# (see comments of https://github.com/sphinx-doc/sphinx/issues/11093)
1873-
if node.get('ismod', False):
1874-
# Detect if the previous nodes are label targets. If so, remove
1875-
# the refid thereof from node['ids'] to avoid duplicated ids.
1876-
prev = get_prev_node(node)
1877-
if self._has_dup_label(prev, node):
1878-
ids = node['ids'][:] # copy to avoid side-effects
1879-
while self._has_dup_label(prev, node):
1880-
ids.remove(prev['refid']) # type: ignore[index]
1881-
prev = get_prev_node(prev) # type: ignore[arg-type]
1882-
else:
1883-
ids = iter(node['ids']) # read-only iterator
1884-
else:
1885-
ids = iter(node['ids']) # read-only iterator
1886-
1887-
for id in ids:
1870+
for id in node['ids']:
18881871
add_target(id)
18891872

18901873
def depart_target(self, node: Element) -> None:
18911874
pass
18921875

1893-
@staticmethod
1894-
def _has_dup_label(sib: Node | None, node: Element) -> bool:
1895-
return isinstance(sib, nodes.target) and sib.get('refid') in node['ids']
1896-
18971876
def visit_attribution(self, node: Element) -> None:
18981877
self.body.append(CR + r'\begin{flushright}' + CR)
18991878
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
@@ -1954,9 +1954,11 @@ def test_latex_labels(app: SphinxTestApp) -> None:
19541954
r'\label{\detokenize{otherdoc::doc}}'
19551955
) in result
19561956

1957-
# Embedded standalone hyperlink reference
1957+
# Named hyperlink reference with embedded alias reference
19581958
# See: https://github.com/sphinx-doc/sphinx/issues/5948
19591959
assert result.count(r'\label{\detokenize{index:section1}}') == 1
1960+
# https://github.com/sphinx-doc/sphinx/issues/13609
1961+
assert r'\phantomsection\label{\detokenize{index:id' not in result
19601962

19611963

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

0 commit comments

Comments
 (0)