Skip to content

Commit a0822b5

Browse files
davideketeliborjelinek
authored andcommitted
feat: update default HTML template to preserve url fragments
1 parent 29503e3 commit a0822b5

File tree

3 files changed

+33
-34
lines changed

3 files changed

+33
-34
lines changed

docs/faq.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ Client-side redirects with meta refresh are okay for search engines, even the mo
1111
I know better how to write HTML redirect file
1212
*********************************************
1313

14-
By default, created HTML redirect files contains ``<html><head><meta http-equiv="refresh" content="0; url=${to_uri}"></head></html>``.
14+
By default, created HTML redirect files contains ``<html><head><noscript><meta http-equiv="refresh" content="0; url=${to_uri}" /></noscript><script>var target = "${to_uri}";if (window.location.hash) {window.location.replace(target + window.location.hash);} else {window.location.replace(target);}</script></head></html>``. A little bit of JavaScript is used to handle hash part of the URI (the ``#foo`` in ``https://example.com/docs#foo``).
1515

16-
If you want JavaScript redirection instead, wait longer, or whatever, set ``redirect_html_template`` option. This option should points to file inside source dir (directory containing ``conf.py``). For example::
16+
If you want to change this behavior, you can create your own HTML template. Just set ``redirect_html_template`` option in your ``conf.py`` to point to your template file. The template file should be located in the source directory (the directory containing ``conf.py``). For example::
1717

1818
redirect_html_template_file = "redirect.html.template"
1919

sphinx_reredirects/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
OPTION_TEMPLATE_FILE = "redirect_html_template_file"
1717
OPTION_TEMPLATE_FILE_DEFAULT = None
1818

19-
REDIRECT_FILE_DEFAULT_TEMPLATE = (
20-
'<html><head><meta http-equiv="refresh" content="0; url=${to_uri}"></head></html>' # noqa: E501
21-
)
19+
REDIRECT_FILE_DEFAULT_TEMPLATE = '<html><head><noscript><meta http-equiv="refresh" content="0; url=${to_uri}" /></noscript><script>var target = "${to_uri}";if (window.location.hash) {window.location.replace(target + window.location.hash);} else {window.location.replace(target);}</script></head></html>' # noqa: E501
2220

2321
logger = logging.getLogger(__name__)
2422

tests/test_end2end.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
from io import StringIO
21
import re
2+
from io import StringIO
3+
from string import Template
4+
35
import pytest
46
from sphinx.application import Sphinx
57
from sphinx.errors import ExtensionError
68

9+
from sphinx_reredirects import REDIRECT_FILE_DEFAULT_TEMPLATE
10+
11+
12+
def _build_redirect_html(to_uri: str) -> str:
13+
return Template(REDIRECT_FILE_DEFAULT_TEMPLATE).substitute({"to_uri": to_uri})
14+
715

816
@pytest.mark.sphinx(
917
"html",
@@ -22,47 +30,42 @@ def test_ext(app: Sphinx, status, warning):
2230
app.build()
2331
status = status.getvalue()
2432

25-
assert (
26-
(app.outdir / "faq/one.html").read_text(encoding="utf-8")
27-
== '<html><head><meta http-equiv="refresh" content="0; url=https://new.com/faq/one.html"></head></html>'
28-
) # noqa: E501
33+
assert (app.outdir / "faq/one.html").read_text(
34+
encoding="utf-8"
35+
) == _build_redirect_html("https://new.com/faq/one.html") # noqa: E501
2936

3037
assert (
3138
"""Overwriting 'faq/one.html' with redirect to 'https://new.com/faq/one.html'.""" # noqa: E501
3239
in status
3340
)
3441

35-
assert (
36-
(app.outdir / "faq/two.html").read_text(encoding="utf-8")
37-
== '<html><head><meta http-equiv="refresh" content="0; url=https://new.com/faq/two.html"></head></html>'
38-
) # noqa: E501
42+
assert (app.outdir / "faq/two.html").read_text(
43+
encoding="utf-8"
44+
) == _build_redirect_html("https://new.com/faq/two.html") # noqa: E501
3945

4046
assert (
4147
"""Overwriting 'faq/two.html' with redirect to 'https://new.com/faq/two.html'.""" # noqa: E501
4248
in status
4349
)
4450

45-
assert (
46-
(app.outdir / "install.html").read_text(encoding="utf-8")
47-
== '<html><head><meta http-equiv="refresh" content="0; url=go-to-install"></head></html>'
48-
) # noqa: E501
51+
assert (app.outdir / "install.html").read_text(
52+
encoding="utf-8"
53+
) == _build_redirect_html("go-to-install") # noqa: E501
4954

5055
assert (
5156
"""Overwriting 'install.html' with redirect to 'go-to-install'.""" # noqa: E501
5257
in status
5358
)
5459

55-
assert (
56-
(app.outdir / "setup.html").read_text(encoding="utf-8")
57-
== '<html><head><meta http-equiv="refresh" content="0; url=install.html"></head></html>'
58-
) # noqa: E501
60+
assert (app.outdir / "setup.html").read_text(
61+
encoding="utf-8"
62+
) == _build_redirect_html("install.html") # noqa: E501
5963

6064
assert """Creating redirect 'setup.html' to 'install.html'.""" in status
6165

62-
assert (
63-
(app.outdir / "install/requirements.html").read_text(encoding="utf-8")
64-
== '<html><head><meta http-equiv="refresh" content="0; url=https://web.com/docs/requirements.html"></head></html>'
65-
) # noqa: E501
66+
assert (app.outdir / "install/requirements.html").read_text(
67+
encoding="utf-8"
68+
) == _build_redirect_html("https://web.com/docs/requirements.html") # noqa: E501
6669

6770
assert (
6871
"""Creating redirect 'install/requirements.html' to 'https://web.com/docs/requirements.html'.""" # noqa: E501
@@ -89,10 +92,9 @@ def test_dirhtml(app: Sphinx, status, warning):
8992
app.build()
9093
status = status.getvalue()
9194

92-
assert (
93-
(app.outdir / "index.html").read_text(encoding="utf-8")
94-
== '<html><head><meta http-equiv="refresh" content="0; url=http://new.com/index"></head></html>'
95-
) # noqa: E501
95+
assert (app.outdir / "index.html").read_text(
96+
encoding="utf-8"
97+
) == _build_redirect_html("http://new.com/index") # noqa: E501
9698

9799
assert (
98100
"""Overwriting 'index.html' with redirect to '/newindex/'.""" # noqa: E501
@@ -104,10 +106,9 @@ def test_dirhtml(app: Sphinx, status, warning):
104106
in status
105107
)
106108

107-
assert (
108-
(app.outdir / "install/index.html").read_text(encoding="utf-8")
109-
== '<html><head><meta http-equiv="refresh" content="0; url=/installing.html"></head></html>'
110-
) # noqa: E501
109+
assert (app.outdir / "install/index.html").read_text(
110+
encoding="utf-8"
111+
) == _build_redirect_html("/installing.html") # noqa: E501
111112

112113
assert (
113114
"""Creating redirect 'install/index.html' to '/installing/trailingslash'.""" # noqa: E501

0 commit comments

Comments
 (0)