-
Notifications
You must be signed in to change notification settings - Fork 512
/
Copy pathupdate_versions_html.py
75 lines (59 loc) · 2.21 KB
/
update_versions_html.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
#!/usr/bin/env python3
import argparse
import json
from bs4 import BeautifulSoup
BASE_URL = "/"
def updateVersionHTML(base_path, base_url=BASE_URL):
with open(base_path + "/captum-master/website/_versions.json", "rb") as infile:
versions = json.loads(infile.read())
with open(base_path + "/new-site/versions.html", "rb") as infile:
html = infile.read()
versions.append("latest")
def prepend_url(a_tag, base_url, version):
href = a_tag.attrs["href"]
if href.startswith("https://") or href.startswith("http://"):
return href
else:
return "{base_url}versions/{version}{original_url}".format(
base_url=base_url, version=version, original_url=href
)
for v in versions:
soup = BeautifulSoup(html, "html.parser")
# title
title_link = soup.find("header").find("a")
title_link.attrs["href"] = prepend_url(title_link, base_url, v)
# nav
nav_links = soup.find("nav").findAll("a")
for l in nav_links:
l.attrs["href"] = prepend_url(l, base_url, v)
# version link
t = soup.find("h2", {"class": "headerTitleWithLogo"}).find_next("a")
t.string = v
t.attrs["href"] = prepend_url(t, base_url, v)
# output files
with open(
base_path + "/new-site/versions/{}/versions.html".format(v), "w"
) as outfile:
outfile.write(str(soup))
with open(
base_path + "/new-site/versions/{}/en/versions.html".format(v), "w"
) as outfile:
outfile.write(str(soup))
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=(
"Fix links in version.html files for Docusaurus site."
"This is used to ensure that the versions.js for older "
"versions in versions subdirectory are up-to-date and "
"will have a way to navigate back to newer versions."
)
)
parser.add_argument(
"-p",
"--base_path",
metavar="path",
required=True,
help="Input directory for rolling out new version of site.",
)
args = parser.parse_args()
updateVersionHTML(args.base_path)