Skip to content

Commit

Permalink
refactor: decouple client redirects and script substitution
Browse files Browse the repository at this point in the history
  • Loading branch information
GetPsyched committed Nov 4, 2024
1 parent a31a2d3 commit f4960b3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def _file_header(self, toc: TocEntry) -> str:
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)),
f'<script type="text/javascript">{self._redirects.get_client_redirects(toc.target.path)}</script>',
f'<script type="text/javascript">{self._redirects.get_redirect_script(toc.target.path)}</script>',
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}',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ def get_client_redirects(self, redirection_target: str):
if path != redirection_target:
continue
client_redirects[anchor] = f"{locations[0]}#{identifier}"
return client_redirects

def get_redirect_script(self, redirection_target: str) -> str:
client_redirects = self.get_client_redirects(redirection_target)
return self._redirects_script.replace('REDIRECTS_PLACEHOLDER', json.dumps(client_redirects))


Expand Down
28 changes: 11 additions & 17 deletions pkgs/tools/nix/nixos-render-docs/src/tests/test_redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,42 +180,36 @@ def test_client_path_with_server_redirect(self):
class TestGetClientRedirects(unittest.TestCase):
def test_no_client_redirects(self):
"""Test fetching client side redirects and ignore server-side ones."""
redirects = Redirects(
{"foo": ["index.html"], "bar": ["index.html", "foo.html"]},
"const redirects = REDIRECTS_PLACEHOLDER;",
)
self.assertEqual(redirects.get_client_redirects("index.html"), "const redirects = {};")
redirects = Redirects({"foo": ["index.html"], "bar": ["index.html", "foo.html"]}, "")
self.assertEqual(redirects.get_client_redirects("index.html"), {})

def test_basic_redirect_matching(self):
redirects = Redirects(
{
'foo': ['index.html', 'index.html#some-section', 'index.html#another-section'],
'bar': ['index.html'],
},
"const redirects = REDIRECTS_PLACEHOLDER;",
"",
)

result = redirects.get_client_redirects("index.html")
client_redirects = redirects.get_client_redirects("index.html")
expected_redirects = {'some-section': 'index.html#foo', 'another-section': 'index.html#foo'}
expected_script = f"const redirects = {json.dumps(expected_redirects)};"
self.assertEqual(result, expected_script)
self.assertEqual(client_redirects, expected_redirects)

def test_advanced_redirect_matching(self):
redirects = Redirects(
{
'foo': ['foo.html', 'foo.html#some-section', 'bar.html#foo'],
'bar': ['bar.html', 'bar.html#another-section'],
},
"const redirects = REDIRECTS_PLACEHOLDER;",
"",
)
self.assertEqual(redirects.get_client_redirects("index.html"), "const redirects = {};")
self.assertEqual(redirects.get_client_redirects("index.html"), {})

result = redirects.get_client_redirects("foo.html")
client_redirects = redirects.get_client_redirects("foo.html")
expected_redirects = {'some-section': 'foo.html#foo'}
expected_script = f"const redirects = {json.dumps(expected_redirects)};"
self.assertEqual(result, expected_script)
self.assertEqual(client_redirects, expected_redirects)

result = redirects.get_client_redirects("bar.html")
client_redirects = redirects.get_client_redirects("bar.html")
expected_redirects = {'foo': 'foo.html#foo', 'another-section': 'bar.html#bar'}
expected_script = f"const redirects = {json.dumps(expected_redirects)};"
self.assertEqual(result, expected_script)
self.assertEqual(client_redirects, expected_redirects)

0 comments on commit f4960b3

Please sign in to comment.