nixos-render-docs: maybe omit link target files

docbook always emits intra-file links if only a single file is emitted.
it doesn't seem to do this for intra-file links when multiple files are
emitted, so we don't do that yet either.
This commit is contained in:
pennae
2023-07-01 17:31:29 +02:00
parent 8c33134465
commit effbaf0ab4
2 changed files with 21 additions and 2 deletions
@@ -312,7 +312,7 @@ class ManualHTMLRenderer(RendererMixin, HTMLRenderer):
"".join((f'<script src="{html.escape(script, True)}" type="text/javascript"></script>'
for script in self._html_params.scripts)),
f' <meta name="generator" content="{html.escape(self._html_params.generator, True)}" />',
f' <link rel="home" href="{home.target.href()}" title="{home.target.title}" />',
f' <link rel="home" href="{home.target.href()}" title="{home.target.title}" />' if home.target.href() else "",
f' {up_link}{prev_link}{next_link}',
' </head>',
' <body>',
@@ -629,6 +629,22 @@ class HTMLConverter(BaseConverter[ManualHTMLRenderer]):
failed = True # do another round and report the first error
xref_queue = deferred
paths_seen = set()
for t in self._xref_targets.values():
paths_seen.add(t.path)
if len(paths_seen) == 1:
for (k, t) in self._xref_targets.items():
self._xref_targets[k] = XrefTarget(
t.id,
t.title_html,
t.toc_html,
t.title,
t.path,
t.drop_fragment,
drop_target=True
)
TocEntry.collect_and_link(self._xref_targets, tokens)
@@ -110,9 +110,12 @@ class XrefTarget:
path: str
"""whether to drop the `#anchor` from links when expanding xrefs"""
drop_fragment: bool = False
"""whether to drop the `path.html` from links when expanding xrefs.
mostly useful for docbook compatibility"""
drop_target: bool = False
def href(self) -> str:
path = html.escape(self.path, True)
path = "" if self.drop_target else html.escape(self.path, True)
return path if self.drop_fragment else f"{path}#{html.escape(self.id, True)}"
@dc.dataclass