Skip to content

Commit 7cc4c09

Browse files
James-A-ClarkJonathan Corbet
authored andcommitted
docs: automarkup.py: Fix invalid HTML link output and broken URI fragments
Since commit d18b017 ("docs: Add automatic cross-reference for documentation pages"), references that were already explicitly defined with "ref:" and referred to other pages with a path have been doubled. This is reported as the following error by Firefox: Start tag "a" seen but an element of the same type was already open. End tag "a" violates nesting rules. As well as the invalid HTML, this also obscures the URI fragment links to subsections because the second link overrides the first. For example on the page admin-guide/hw-vuln/mds.html the last link should be to the "Default Mitigations" subsection using a # URI fragment: admin-guide/hw-vuln/l1tf.html#default-mitigations But it is obsured by a second link to the whole page: admin-guide/hw-vuln/l1tf.html The full HTML with the double <a> tags looks like this: <a class="reference internal" href="l1tf.html#default-mitigations"> <span class="std std-ref"> <a class="reference internal" href="l1tf.html"> <span class="doc">L1TF - L1 Terminal Fault</span> </a> </span> </a> After this commit, there is only a single link: <a class="reference internal" href="l1tf.html#default-mitigations"> <span class="std std-ref">Documentation/admin-guide/hw-vuln//l1tf.rst</span> </a> Now that the second link is removed, the browser correctly jumps to the default-mitigations subsection when clicking the link. The fix is to check that nodes in the document to be modified are not already references. A reference is counted as any text that is a descendant of a reference type node. Only plain text should be converted to new references, otherwise the doubling occurs. Testing ======= * Test that the build stdout is the same (ignoring ordering), and that no new warnings are printed. * Diff all .html files and check that the only modifications occur to the bad double links. * The auto linking of bare references to pages without "ref:" is still working. Fixes: d18b017 ("docs: Add automatic cross-reference for documentation pages") Reviewed-by: Nícolas F. R. A. Prado <[email protected]> Signed-off-by: James Clark <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jonathan Corbet <[email protected]>
1 parent 82ca673 commit 7cc4c09

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

Documentation/sphinx/automarkup.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,19 +271,30 @@ def get_c_namespace(app, docname):
271271
def auto_markup(app, doctree, name):
272272
global c_namespace
273273
c_namespace = get_c_namespace(app, name)
274+
def text_but_not_a_reference(node):
275+
# The nodes.literal test catches ``literal text``, its purpose is to
276+
# avoid adding cross-references to functions that have been explicitly
277+
# marked with cc:func:.
278+
if not isinstance(node, nodes.Text) or isinstance(node.parent, nodes.literal):
279+
return False
280+
281+
child_of_reference = False
282+
parent = node.parent
283+
while parent:
284+
if isinstance(parent, nodes.Referential):
285+
child_of_reference = True
286+
break
287+
parent = parent.parent
288+
return not child_of_reference
289+
274290
#
275291
# This loop could eventually be improved on. Someday maybe we
276292
# want a proper tree traversal with a lot of awareness of which
277293
# kinds of nodes to prune. But this works well for now.
278294
#
279-
# The nodes.literal test catches ``literal text``, its purpose is to
280-
# avoid adding cross-references to functions that have been explicitly
281-
# marked with cc:func:.
282-
#
283295
for para in doctree.traverse(nodes.paragraph):
284-
for node in para.traverse(nodes.Text):
285-
if not isinstance(node.parent, nodes.literal):
286-
node.parent.replace(node, markup_refs(name, app, node))
296+
for node in para.traverse(condition=text_but_not_a_reference):
297+
node.parent.replace(node, markup_refs(name, app, node))
287298

288299
def setup(app):
289300
app.connect('doctree-resolved', auto_markup)

0 commit comments

Comments
 (0)