-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_static_site.py
62 lines (50 loc) · 1.82 KB
/
generate_static_site.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import codecs
from jinja2 import Environment, FileSystemLoader, select_autoescape
from data_munging import parseFile
import shutil
import os
'''
make directories recursively if they don't already exist, else do nothing
copied from https://stackoverflow.com/a/600612/1925961
'''
def mkdir_p(path):
try:
os.makedirs(path)
except OSError, exc:
if exc.errno == 17 and os.path.isdir(path): #EEXIST
pass
else:
raise
def generate_sections(environment, vocab, filepath):
template = environment.get_template('section.html')
mkdir_p(filepath)
for index, section in enumerate(vocab):
page = template.render(
title=section.title,
entries=section.entries,
prev_href='./{0}.html'.format((index - 1) % len(vocab)),
next_href='./{0}.html'.format((index + 1) % len(vocab)))
filename = filepath + '/{}.html'.format(index)
with codecs.open(filename, 'w', 'utf-8') as destination:
print(page, file=destination)
def generate_overview(environment, vocab, filepath):
template = environment.get_template('overview.html')
mkdir_p(filepath)
page = template.render(
title='Revocab',
entries=[entry for section in vocab for entry in section.entries])
filename = filepath + '/overview.html'
with codecs.open(filename, 'w', 'utf-8') as destination:
print(page, file=destination)
def main():
environment = Environment(
loader=FileSystemLoader('./templates'),
autoescape=select_autoescape(['html', 'xml']))
vocab = parseFile()
#generate_sections(environment, vocab, './static')
generate_overview(environment, vocab, './static')
if __name__ == '__main__':
main()