-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathadd_html_anchors_to_exercises.py
More file actions
executable file
·38 lines (28 loc) · 1.22 KB
/
add_html_anchors_to_exercises.py
File metadata and controls
executable file
·38 lines (28 loc) · 1.22 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
#!/usr/bin/env python3
from bs4 import BeautifulSoup
import sys
import re
def process(filename):
with open(filename) as f:
soup = BeautifulSoup(f, "lxml")
div = soup.find("div", class_="wy-side-nav-search")
a = BeautifulSoup("""<a href="http://www.helsinki.fi" style="margin-bottom: 0px;"><img src="https://uni.materialbank.net/NiboWEB/uni/getPublicFile.do?uuid=146263&inline=false&ticket=8a2a112700dc87abd2813d55e149bc0c&type=original" style="margin-bottom: 0px;max-width: 60%;height: auto;width: auto;"></a>""", "html.parser")
div.insert(0, a)
divs = soup.find_all("div", class_="admonition")
for d in divs:
if len(d.contents) != 1:
continue
m = re.match(r"\n*(Exercise \d+ \([\w ]+\))", d.contents[0].string)
if m:
exercise = m[1]
a = soup.new_tag("a", id=exercise.replace(" ", "-"))
a.string = exercise
d.string=""
d.append(a)
#d.string = '<a name="%s">%s</a>' % (exercise.replace(" ", "-"), exercise)
#print("\n", d)
with open(filename, "w") as f:
f.write(str(soup))
for filename in sys.argv[1:]:
print("Processing file %s" % filename)
process(filename)