forked from stan-dev/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_redirects.py
76 lines (66 loc) · 2.03 KB
/
add_redirects.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import glob
import os
import os.path
import sys
"""
Create or update redirect files in unversioned dir
that redirect to latest version page.
"""
contents = """<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Redirecting…</title>
<meta http-equiv="refresh" content="0; url=REDIRECTTO">
<meta name="robots" content="noindex">
</head>
<body>
<h1>Redirecting…</h1>
<a href="REDIRECTTO">Click here if you are not redirected.</a>
</body>
</html>
"""
stan_site = "https://mc-stan.org"
def main():
if len(sys.argv) > 3:
stan_major = int(sys.argv[1])
stan_minor = int(sys.argv[2])
else:
print("Expecting 3 arguments <MAJOR> <MINOR> version numbers, <docset name>")
sys.exit(1)
stan_version = "_".join([str(stan_major), str(stan_minor)])
base_dir = sys.argv[3]
version_dir = "/".join(["docs", stan_version, base_dir])
no_version_dir = "/".join(["docs", base_dir])
files = [x.split("/")[-1] for x in glob.glob(version_dir + "/*.html")]
for file in files:
# create redirect file in no_version_dir
# WSL #
filename = "/".join(
[
no_version_dir,
file.replace("functions-reference\\", "")
.replace("reference-manual\\", "")
.replace("stan-users-guide\\", "")
.replace("cmdstan-guide\\", ""),
]
)
# filename = "/".join([no_version_dir, file])
print(filename)
# WSL #
r_to = "/".join(
[
stan_site,
version_dir,
file.replace("functions-reference\\", "")
.replace("reference-manual\\", "")
.replace("stan-users-guide\\", "")
.replace("cmdstan-guide\\", ""),
]
)
# r_to = "/".join([stan_site,version_dir, file])
print(r_to)
r_contents = contents.replace("REDIRECTTO", r_to)
with open(filename, "w") as f:
f.write(r_contents)
if __name__ == "__main__":
main()