nixos-render-docs: don't validate redirects if none were given
This commit is contained in:
parent
6f7c18e904
commit
eb2872ea67
@ -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):
|
||||
' </div>',
|
||||
])
|
||||
|
||||
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([
|
||||
'<?xml version="1.0" encoding="utf-8" standalone="no"?>',
|
||||
@ -325,7 +328,7 @@ class ManualHTMLRenderer(RendererMixin, HTMLRenderer):
|
||||
"".join((f'<link rel="stylesheet" type="text/css" href="{html.escape(style, True)}" />'
|
||||
for style in self._html_params.stylesheets)),
|
||||
"".join((f'<script src="{html.escape(script, True)}" type="text/javascript"></script>'
|
||||
for script in [*self._html_params.scripts, redirects_path])),
|
||||
for script in 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}" />' 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:
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user