-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
80 lines (68 loc) · 3.65 KB
/
build.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
77
78
79
80
import os
import fnmatch
import codecs
import glob
from jinja2 import Environment, FileSystemLoader
from bs4 import BeautifulSoup
def _render_template(template_filename, context):
TEMPLATE_ENVIRONMENT = Environment(autoescape=False,
loader=FileSystemLoader('source'),
trim_blocks=False)
return TEMPLATE_ENVIRONMENT.get_template(template_filename.replace("\\", "/")).render(context)
def delete_html_files():
files = glob.glob("*.html")
for file_ in files:
os.remove(file_)
def create_html_files():
members = []
files = []
for root, dirnames, filenames in os.walk('source'):
for filename in fnmatch.filter(filenames, '*.html'):
files.append(os.path.join(root, filename))
if "lab_members" in root:
with codecs.open(os.path.join(root, filename), 'r', "utf-8") as f:
source_code = f.read()
soup = BeautifulSoup(source_code, features="html.parser")
members.append({"name": soup.h1.text,
"function": soup.p.text, # ** .lower() added by Laura de Nooij, 24-09-2020
"url": "_".join(soup.h1.text.lower().split()) + ".html",
"image": soup.span.img['src']})
# # Sort first by length of function (PI < Postdoc < PhD student < Master student), then by LAST name ** commented out by Laura de Nooij, 24-09-2020
# members.sort(key=lambda x: (len(x["function"]), x["name"].split(" ")[-1]))
# Sort functions ** added by Laura de Nooij, 24-09-2020
members_pi = [d for d in members if d['function'] == 'PI']
members_pi.sort(key=lambda x: x["name"].split(" ")[-1])
members_sr = [d for d in members if d['function'] == 'Senior Researcher']
members_sr.sort(key=lambda x: x["name"].split(" ")[-1])
members_postdoc = [d for d in members if d['function'] == 'Postdoc']
members_postdoc.sort(key=lambda x: x["name"].split(" ")[-1])
members_phd = [d for d in members if d['function'] == 'PhD Candidate']
members_phd.sort(key=lambda x: x["name"].split(" ")[-1])
members_extphd = [d for d in members if d['function'] == 'External PhD Candidate']
members_extphd.sort(key=lambda x: x["name"].split(" ")[-1])
members_visitphd = [d for d in members if d['function'] == 'Visiting PhD Candidate']
members_visitphd.sort(key=lambda x: x["name"].split(" ")[-1])
members_ra = [d for d in members if d['function'] == 'Research Assistant']
members_ra.sort(key=lambda x: x["name"].split(" ")[-1])
members_tr = [d for d in members if d['function'] == 'Trainee']
members_tr.sort(key=lambda x: x["name"].split(" ")[-1])
members_exttr = [d for d in members if d['function'] == 'External Trainee']
members_exttr.sort(key=lambda x: x["name"].split(" ")[-1])
members_former = [d for d in members if d['function'] == 'Former lab members']
members = members_pi + members_sr + members_postdoc + members_phd + members_extphd + members_visitphd + members_ra + members_tr + members_exttr + members_former
context = {
'members': members
}
for file_ in files:
file_ = file_.replace("source" + os.path.sep, "")
if file_ == "base.html":
continue
with codecs.open(os.path.split(file_)[1], 'w', encoding='utf-8') as f:
print(file_)
html = _render_template(file_, context)
f.write(html)
def main():
delete_html_files()
create_html_files()
if __name__ == "__main__":
main()