Skip to content

Commit c617999

Browse files
committed
Minor fixes to "redirect" page generator
1 parent 93ca619 commit c617999

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

_ext/redirects.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<!DOCTYPE html>
2222
<html lang="en-US">
2323
<head>
24-
<title>Redirecting&hellip;</title>
24+
<title>Redirecting...</title>
2525
<script>
2626
var redirect_url = "$redirect_url";
2727
if (window.location.search) {
@@ -38,14 +38,20 @@
3838
<meta name="robots" content="noindex" />
3939
</head>
4040
<body>
41-
<h1>Redirecting&hellip;</h1>
41+
<h1>Redirecting...</h1>
4242
<p><a href="$redirect_url">Click here if you are not redirected.</a></p>
4343
</body>
4444
</html>
4545
"""
4646
)
4747

4848

49+
def build_redirect_page(html_path, redirect_url):
50+
os.makedirs(os.path.dirname(html_path), exist_ok=True)
51+
with open(html_path, "w") as fp:
52+
fp.write(REDIRECT_TEMPLATE.substitute(redirect_url=redirect_url).strip())
53+
54+
4955
def load_redirects(src_dir):
5056
redirects_txt = os.path.join(src_dir, "redirects.txt")
5157
assert os.path.isfile(redirects_txt)
@@ -78,18 +84,16 @@ def build_redirect_pages(app, exception):
7884
out_dir = app.outdir
7985
if os.path.dirname(from_path) != "/":
8086
out_dir = os.path.join(out_dir, os.path.dirname(from_path)[1:])
81-
os.makedirs(out_dir, exist_ok=True)
82-
with open(os.path.join(out_dir, os.path.basename(from_path)), "w") as fp:
83-
fp.write(
84-
REDIRECT_TEMPLATE.substitute(
85-
redirect_url="%s/en/%s%s"
86-
% (
87-
app.config.html_baseurl,
88-
("latest" if is_latest else "stable"),
89-
to_path,
90-
)
91-
).strip()
92-
)
87+
build_redirect_page(
88+
os.path.join(out_dir, os.path.basename(from_path)),
89+
"%s/en/%s%s"
90+
% (
91+
app.config.html_baseurl,
92+
("latest" if is_latest else "stable"),
93+
to_path,
94+
),
95+
)
96+
9397
print("Built %d redirect pages" % len(redirects))
9498

9599

@@ -104,18 +108,15 @@ def build_legacy_rtd_pages(app, exception):
104108
relative_dir = root[len(os.path.commonpath([app.outdir, root])) :] or "/"
105109
if relative_dir != "/":
106110
out_dir = os.path.join(out_dir, relative_dir[1:])
107-
os.makedirs(out_dir, exist_ok=True)
108-
with open(os.path.join(out_dir, name), "w") as fp:
109-
fp.write(
110-
REDIRECT_TEMPLATE.substitute(
111-
redirect_url="%s/en/latest%s%s"
112-
% (
113-
app.config.html_baseurl,
114-
relative_dir,
115-
name,
116-
)
117-
).strip()
118-
)
111+
build_redirect_page(
112+
os.path.join(out_dir, name),
113+
"%s/en/latest%s%s"
114+
% (
115+
app.config.html_baseurl,
116+
relative_dir,
117+
name,
118+
),
119+
)
119120

120121

121122
def setup(app):

conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
# Add any Sphinx extension module names here, as strings. They can be
3333
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
3434
# ones.
35-
extensions = ["redirects", "sphinx.ext.githubpages", "notfound.extension"]
35+
extensions = ["redirects", "notfound.extension"]
3636

3737
# Add any paths that contain templates here, relative to this directory.
3838
templates_path = ["_templates"]

0 commit comments

Comments
 (0)