-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathredirect.py
46 lines (33 loc) · 1.05 KB
/
redirect.py
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
44
45
46
from pathlib import Path
from typing import Dict
import yaml
root: Path = Path(__file__).parent
outdir: Path = root / "_site"
with open(root / "_data" / "redirects.yml", "r", encoding="utf-8") as fd:
all_redirects = yaml.safe_load(fd)
template = """
<!DOCTYPE HTML>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="1; url={0}">
<script>
window.location.href = "{0}"
</script>
<title>Page Redirection</title>
If you are not redirected automatically, follow the <a href='{0}'>link</a>.
"""
def build_redirects(redirects: Dict[str, str]) -> None:
"""
Build the redirects for a single language.
Parameters
----------
redirects : Dict[str, str]
Page redirects to build.
"""
for source, target in redirects.items():
source_path = outdir / source
redirect = template.format(target)
if not source_path.parent.exists():
source_path.parent.mkdir(parents=True)
with open(source_path, "w", encoding="utf-8") as fp:
fp.write(redirect)
build_redirects(all_redirects)