-
Notifications
You must be signed in to change notification settings - Fork 1
/
render.py
47 lines (37 loc) · 1.28 KB
/
render.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
import yaml
from jinja2 import Environment
import base64
import hashlib
def template_authors(data):
people = {}
# replace authors with markdown with link to profiles
for person in data["people"]:
assert person["name"] not in people
people[person["name"]] = person["link"]
for publication in data["publications"]:
publication["authors"] = ", ".join(
[people[author] for author in publication["authors"]]
)
def get_hash(name):
hasher = hashlib.sha1(name.encode("utf-8"))
hash = base64.urlsafe_b64encode(hasher.digest()[:5])
hash = [x for x in hash.decode("utf-8") if x.isalnum()]
hash = "".join(hash)
hash = "_" + hash # since function names can't start with a number
return hash
def template_hash(data):
hashes = set()
for publication in data["publications"]:
hash = get_hash(publication["name"])
assert hash not in hashes, "hash collision"
publication["hash"] = hash
data = yaml.load(open("data.yaml"), Loader=yaml.FullLoader)
template_authors(data)
template_hash(data)
env = Environment(extensions=['jinja_markdown.MarkdownExtension'])
template = env.from_string(
open("template.html").read(),
)
output = template.render(**data)
open("index.html", "w").write(output)
print("done")