-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtmlize.py
86 lines (65 loc) · 2.24 KB
/
htmlize.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
81
82
83
84
85
86
import pystache
import json
import os
import re
ROOT = os.path.dirname(os.path.realpath(__file__))
IN_DIR = os.path.join(ROOT, 'content')
PROJECTS_FILENAME = os.path.join(IN_DIR, 'projects.json')
OUT_DIR = os.path.join(ROOT, 'build')
RENDERER = pystache.Renderer(search_dirs=[IN_DIR])
TEMPLATE_CACHE = {}
def render(template_name, data):
if template_name not in TEMPLATE_CACHE:
template_path = os.path.join(IN_DIR, template_name + '.mustache')
with open(template_path) as template_file:
template_str = template_file.read()
TEMPLATE_CACHE[template_name] = pystache.parse(template_str)
return RENDERER.render(TEMPLATE_CACHE[template_name], data)
def make_page(filename, html):
path = os.path.join(OUT_DIR, filename)
print('Generating {0}...'.format(path))
with open(path, 'w') as f:
f.write(html)
def make_index_page(projects):
html = render('index', {
'pageTitle': 'Savage Internet',
'projects': projects
})
make_page('index.html', html)
def make_project_pages(projects):
for project in projects:
html = render('projectPage', {
'pageTitle': project['title'] + ' - Savage Internet',
'project': project
})
make_page(project['filename'], html)
def load_json(fname):
with open(fname) as jsonFile:
return json.load(jsonFile)
def get_project_filename(project):
title = project.get('titleShort', project['title'])
title = title.lower()
title = re.sub('[!@#$\'\. ,:&]+', '-', title)
title = title.strip('-')
return title + '.html'
def add_tag_longnames(tags):
longnames = {
'ed': 'education',
'el': 'electronics',
'fab': 'fabrication',
'g': 'games',
'rw': 'real-world',
'sw': 'software',
'viz': 'visualization'
}
tags = list(map(lambda x: {'short': x, 'long': longnames[x]}, tags))
return sorted(tags, key=lambda x: x['long'])
def main():
projects = load_json(PROJECTS_FILENAME)
for project in projects:
project['filename'] = get_project_filename(project)
project['tags'] = add_tag_longnames(project['tags'])
make_index_page(projects)
make_project_pages(projects)
if __name__ == '__main__':
main()