forked from stan-dev/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_redirects.py
54 lines (45 loc) · 1.38 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
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 = long(sys.argv[1])
stan_minor = long(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
filename = "/".join([no_version_dir, file])
print filename
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()