Skip to content

Commit 829c390

Browse files
committed
[manpage] don't emit OSC 8 hyperlinks for anchor references (sphinx-doc#12108)
A reference like ":ref:`Some other page <some-other-page>`" results in a refuri "#some-other-page". This does not seem useful to readers of the man page. It is especially unhelpful when using a terminal that implements a hint mode for selecting links -- the extra links add noise, making it harder to select the interesting ones. Don't emit OSC 8 for those. Also don't emit it for URLs that might be unsafe (see sphinx-doc#12260 (comment)) I don't know one that would be unsafe but I'm sure there are some. OTOH, "man_show_urls" doesn't seem unsafe because it shows the exact URL that will be opened, so enable that for all URLs (except for relative ones). Follow up to sphinx-doc#12108 I also confirmed that even with docutils 0.21 we do not need to override depart_reference because we already skip reference nodes.
1 parent 6fd8b30 commit 829c390

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

sphinx/writers/manpage.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ def visit_image(self, node: Element) -> None:
309309
# overwritten -- don't visit inner marked up nodes
310310
def visit_reference(self, node: Element) -> None:
311311
uri = node.get('refuri', '')
312-
if uri:
312+
is_safe_to_click = uri.startswith(('mailto:', 'http:', 'https:', 'ftp:'))
313+
if is_safe_to_click:
313314
# OSC 8 link start (using groff's device control directive).
314315
self.body.append(fr"\X'tty: link {uri}'")
315316

@@ -319,7 +320,7 @@ def visit_reference(self, node: Element) -> None:
319320
self.visit_Text(node)
320321
self.body.append(self.defs['reference'][1])
321322

322-
if uri.startswith(('mailto:', 'http:', 'https:', 'ftp:')):
323+
if not uri.startswith('#'):
323324
# if configured, put the URL after the link
324325
if self.config.man_show_urls and node.astext() != uri:
325326
if uri.startswith('mailto:'):
@@ -328,7 +329,7 @@ def visit_reference(self, node: Element) -> None:
328329
' <',
329330
self.defs['strong'][0], uri, self.defs['strong'][1],
330331
'>'])
331-
if uri:
332+
if is_safe_to_click:
332333
# OSC 8 link end.
333334
self.body.append(r"\X'tty: link'")
334335
raise nodes.SkipNode

0 commit comments

Comments
 (0)