Skip to content

Commit 8143368

Browse files
committed
docs: improve redirects
1 parent 0a48ab6 commit 8143368

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

docsrc/generate-redirects.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import os
2+
3+
SWITCH_VERSION = "v1.1.6"
4+
5+
TEMPLATE_LATEST = """<!doctype html>
6+
<html>
7+
<head>
8+
<title>Redirecting to {{{{ latest.name }}}} branch/version</title>
9+
<meta charset="utf-8" />
10+
<meta
11+
http-equiv="refresh"
12+
content="0; url=/{{{{ latest.name }}}}/{path}"
13+
/>
14+
<link rel="canonical" href="/{{{{ latest.name }}}}/{path}" />
15+
</head>
16+
</html>
17+
"""
18+
19+
TEMPLATE_V1 = f"""<!doctype html>
20+
<html>
21+
<head>
22+
<title>Redirecting to {SWITCH_VERSION}</title>
23+
<meta charset="utf-8" />
24+
<meta
25+
http-equiv="refresh"
26+
content="0; url=/{SWITCH_VERSION}/{{path}}"
27+
/>
28+
<link rel="canonical" href="/{SWITCH_VERSION}/{{path}}" />
29+
</head>
30+
</html>
31+
"""
32+
33+
34+
def generate_templates(old_doc_dir, include_dirs, latest_files, template_dir):
35+
os.makedirs(template_dir, exist_ok=True)
36+
37+
for curdir, dirs, files in os.walk(old_doc_dir):
38+
if any([curdir.startswith(old_doc_dir + "/" + d) for d in include_dirs]) or curdir == old_doc_dir:
39+
html_files = [f for f in files if f.endswith("html")]
40+
if html_files:
41+
os.makedirs(os.path.join(template_dir, curdir), exist_ok=True)
42+
for file in html_files:
43+
file_path = os.path.join(curdir[(len(old_doc_dir) + 1) :], file)
44+
print("Writing", os.path.join(curdir[(len(old_doc_dir) + 1) :], file))
45+
with open(os.path.join(template_dir, file_path), "w") as f:
46+
template = TEMPLATE_LATEST if file in latest_files else TEMPLATE_V1
47+
content = template.format(path=file_path)
48+
f.write(content)
49+
50+
51+
if __name__ == "__main__":
52+
old_doc_dir = "../docs/v1.1.6"
53+
include_dirs = ["_examples", "_images", "_modules", "api"]
54+
latest_files = ["index.html", "about.html"]
55+
template_dir = "polyversion/templates"
56+
57+
generate_templates(old_doc_dir, include_dirs, latest_files, template_dir)

docsrc/poly.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
#: Whether to build the docs in parallel
2828
PARALLEL_BUILDS = os.environ.get("BF_DOCS_SEQUENTIAL_BUILDS", "0") != "1"
29-
print(PARALLEL_BUILDS, "parallel")
3029

3130
#: Determine repository root directory
3231
root = Git.root(Path(__file__).parent)
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<!doctype html>
2-
32
<html>
43
<head>
54
<title>Redirecting to {{ latest.name }} branch/version</title>
65
<meta charset="utf-8" />
76
<meta
87
http-equiv="refresh"
9-
content="0; url=./{{ latest.name }}/index.html"
8+
content="0; url=/{{ latest.name }}/index.html"
109
/>
11-
<link rel="canonical" href="./{{ latest.name }}/index.html" />
10+
<link rel="canonical" href="/{{ latest.name }}/index.html" />
1211
</head>
1312
</html>

docsrc/source/_templates/versioning.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
<p class="caption" aria-level="2" role="heading"><span class="caption-text">{{ _('Tags') }}</span></p>
55
<ul>
66
{%- for item in tags %}
7-
<li><a href="/{{ item.name }}/index.html" {% if current and current.name == item.name %}class="current"{% endif %}>{{ item.name }}</a></li>
7+
<li><a href="/{{ item.name }}/{{ pagename }}.html" {% if current and current.name == item.name %}class="current"{% endif %}>{{ item.name }}</a></li>
88
{%- endfor %}
99
</ul>
1010
{% endif %}
1111
{% if branches %} {# List of branches #}
1212
<p class="caption" aria-level="2" role="heading"><span class="caption-text">{{ _('Branches') }}</span></p>
1313
<ul>
1414
{%- for item in branches %}
15-
<li><a href="/{{ item.name }}/index.html" {% if current and current.name == item.name %}class="current"{% endif %}>{{ item.name }}</a></li>
15+
<li><a href="/{{ item.name }}/{{ pagename}}.html" {% if current and current.name == item.name %}class="current"{% endif %}>{{ item.name }}</a></li>
1616
{%- endfor %}
1717
</ul>
1818
{% endif %}

0 commit comments

Comments
 (0)