From eb2872ea67f6633d9681c511063b41303f908d9a Mon Sep 17 00:00:00 2001 From: GetPsyched Date: Thu, 21 Nov 2024 17:03:57 +0400 Subject: [PATCH] nixos-render-docs: don't validate redirects if none were given --- .../src/nixos_render_docs/manual.py | 40 ++++++++++--------- .../src/tests/test_auto_id_prefix.py | 6 +-- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/pkgs/by-name/ni/nixos-render-docs/src/nixos_render_docs/manual.py b/pkgs/by-name/ni/nixos-render-docs/src/nixos_render_docs/manual.py index 64b3740c8352..52b51f63f4d8 100644 --- a/pkgs/by-name/ni/nixos-render-docs/src/nixos_render_docs/manual.py +++ b/pkgs/by-name/ni/nixos-render-docs/src/nixos_render_docs/manual.py @@ -217,11 +217,11 @@ class ManualHTMLRenderer(RendererMixin, HTMLRenderer): _base_path: Path _in_dir: Path _html_params: HTMLParameters - _redirects: Redirects + _redirects: Redirects | None def __init__(self, toplevel_tag: str, revision: str, html_params: HTMLParameters, manpage_urls: Mapping[str, str], xref_targets: dict[str, XrefTarget], - redirects: Redirects, in_dir: Path, base_path: Path): + redirects: Redirects | None, in_dir: Path, base_path: Path): super().__init__(toplevel_tag, revision, manpage_urls, xref_targets) self._in_dir = in_dir self._base_path = base_path.absolute() @@ -310,9 +310,12 @@ class ManualHTMLRenderer(RendererMixin, HTMLRenderer): ' ', ]) - redirects_path = f'{self._base_path}/{toc.target.path.split('.html')[0]}-redirects.js' - with open(redirects_path, 'w') as file: - file.write(self._redirects.get_redirect_script(toc.target.path)) + scripts = self._html_params.scripts + if self._redirects: + redirects_path = f'{self._base_path}/{toc.target.path.split('.html')[0]}-redirects.js' + with open(redirects_path, 'w') as file: + file.write(self._redirects.get_redirect_script(toc.target.path)) + scripts.append(redirects_path) return "\n".join([ '', @@ -325,7 +328,7 @@ class ManualHTMLRenderer(RendererMixin, HTMLRenderer): "".join((f'' for style in self._html_params.stylesheets)), "".join((f'' - for script in [*self._html_params.scripts, redirects_path])), + for script in scripts)), f' ', f' ' if home.target.href() else "", f' {up_link}{prev_link}{next_link}', @@ -509,7 +512,7 @@ class HTMLConverter(BaseConverter[ManualHTMLRenderer]): _revision: str _html_params: HTMLParameters _manpage_urls: Mapping[str, str] - _redirects: Redirects + _redirects: Redirects | None _xref_targets: dict[str, XrefTarget] _redirection_targets: set[str] _appendix_count: int = 0 @@ -518,7 +521,7 @@ class HTMLConverter(BaseConverter[ManualHTMLRenderer]): self._appendix_count += 1 return _to_base26(self._appendix_count - 1) - def __init__(self, revision: str, html_params: HTMLParameters, manpage_urls: Mapping[str, str], redirects: Redirects): + def __init__(self, revision: str, html_params: HTMLParameters, manpage_urls: Mapping[str, str], redirects: Redirects | None = None): super().__init__() self._revision, self._html_params, self._manpage_urls, self._redirects = revision, html_params, manpage_urls, redirects self._xref_targets = {} @@ -679,13 +682,14 @@ class HTMLConverter(BaseConverter[ManualHTMLRenderer]): ) TocEntry.collect_and_link(self._xref_targets, tokens) - self._redirects.validate(self._xref_targets) - server_redirects = self._redirects.get_server_redirects() - with open(outfile.parent / '_redirects', 'w') as server_redirects_file: - formatted_server_redirects = [] - for from_path, to_path in server_redirects.items(): - formatted_server_redirects.append(f"{from_path} {to_path} 301") - server_redirects_file.write("\n".join(formatted_server_redirects)) + if self._redirects: + self._redirects.validate(self._xref_targets) + server_redirects = self._redirects.get_server_redirects() + with open(outfile.parent / '_redirects', 'w') as server_redirects_file: + formatted_server_redirects = [] + for from_path, to_path in server_redirects.items(): + formatted_server_redirects.append(f"{from_path} {to_path} 301") + server_redirects_file.write("\n".join(formatted_server_redirects)) def _build_cli_html(p: argparse.ArgumentParser) -> None: @@ -704,16 +708,16 @@ def _build_cli_html(p: argparse.ArgumentParser) -> None: def _run_cli_html(args: argparse.Namespace) -> None: with open(args.manpage_urls) as manpage_urls, open(Path(__file__).parent / "redirects.js") as redirects_script: - redirects = {} + redirects = None if args.redirects: with open(args.redirects) as raw_redirects: - redirects = json.load(raw_redirects) + redirects = Redirects(json.load(raw_redirects), redirects_script.read()) md = HTMLConverter( args.revision, HTMLParameters(args.generator, args.stylesheet, args.script, args.toc_depth, args.chunk_toc_depth, args.section_toc_depth, args.media_dir), - json.load(manpage_urls), Redirects(redirects, redirects_script.read())) + json.load(manpage_urls), redirects) md.convert(args.infile, args.outfile) def build_cli(p: argparse.ArgumentParser) -> None: diff --git a/pkgs/by-name/ni/nixos-render-docs/src/tests/test_auto_id_prefix.py b/pkgs/by-name/ni/nixos-render-docs/src/tests/test_auto_id_prefix.py index a3d33930b303..ae847de6b8a8 100644 --- a/pkgs/by-name/ni/nixos-render-docs/src/tests/test_auto_id_prefix.py +++ b/pkgs/by-name/ni/nixos-render-docs/src/tests/test_auto_id_prefix.py @@ -11,7 +11,7 @@ def set_prefix(token: Token, ident: str) -> None: def test_auto_id_prefix_simple() -> None: - md = HTMLConverter("1.0.0", HTMLParameters("", [], [], 2, 2, 2, Path("")), {}, Redirects({}, '')) + md = HTMLConverter("1.0.0", HTMLParameters("", [], [], 2, 2, 2, Path("")), {}) src = f""" # title @@ -32,7 +32,7 @@ def test_auto_id_prefix_simple() -> None: def test_auto_id_prefix_repeated() -> None: - md = HTMLConverter("1.0.0", HTMLParameters("", [], [], 2, 2, 2, Path("")), {}, Redirects({}, '')) + md = HTMLConverter("1.0.0", HTMLParameters("", [], [], 2, 2, 2, Path("")), {}) src = f""" # title @@ -58,7 +58,7 @@ def test_auto_id_prefix_repeated() -> None: ] def test_auto_id_prefix_maximum_nested() -> None: - md = HTMLConverter("1.0.0", HTMLParameters("", [], [], 2, 2, 2, Path("")), {}, Redirects({}, '')) + md = HTMLConverter("1.0.0", HTMLParameters("", [], [], 2, 2, 2, Path("")), {}) src = f""" # h1