-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathbuild_redirects.py
More file actions
43 lines (35 loc) · 1.2 KB
/
build_redirects.py
File metadata and controls
43 lines (35 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
"""
Generate HTML redirect files from _redirects.json.
This script reads the _redirects.json file and generates HTML redirect pages
for each entry. The JSON file should be an object mapping source names to
target URLs (either absolute paths like "/foo" or full URLs like "https://...").
"""
import json
from pathlib import Path
REDIRECT_TEMPLATE = '''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="0; url={url}">
<title>Redirecting...</title>
</head>
<body>
<p>If you are not redirected automatically, follow this <a href="{url}">link</a>.</p>
</body>
</html>
'''
def build_redirects():
redirects_file = Path("_redirects.json")
if not redirects_file.exists():
print("No _redirects.json found, skipping redirect generation")
return
with open(redirects_file) as f:
redirects = json.load(f)
for source, target in redirects.items():
html_file = Path(f"{source}.html")
html_content = REDIRECT_TEMPLATE.format(url=target)
html_file.write_text(html_content)
print(f"Generated redirect: {source}.html -> {target}")
if __name__ == "__main__":
build_redirects()