-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.py
93 lines (68 loc) · 2.64 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
81
82
83
84
85
86
87
88
89
90
91
92
from jinja2 import Environment, FileSystemLoader
from side_menu import MENU_ITEMS
import re
def read_code(file_name):
ret = []
add_line = False
ws_count = -1
with open(file_name, 'r') as f:
for line in f:
if '// END_CODE' in line:
break
if add_line:
line = line.replace(r' {3,8}', '\t')
line = line.replace('\t', ' ')
if ws_count == -1:
ws_count = re.search(r'[\S]', line).start()
ret.append(line[ws_count:] if len(line) >= ws_count else line)
if '// BEGIN_CODE' in line:
add_line = True
if len(ret) == 0:
print "Error did not read any code from %s. Make sure you have a BEGIN_CODE marker" % file_name
return ret
env = Environment(loader=FileSystemLoader('templates'))
env.autoescape = True
# go through and "fix-up" MENU_ITEMS
for item in list(MENU_ITEMS):
if 'name' not in item:
print "Error couldn't find name in item: %s" % str(item)
MENU_ITEMS.remove(item)
if 'page' not in item:
print "Error, no page found in item: %s" % str(item)
MENU_ITEMS.remove(item)
continue
if item['page'] is None:
continue
page = item['page']
if 'template' not in page:
print "Warning, couldn't find template for %s skipping..." % item['name']
page['template'] = 'not_found.html'
continue
# make the not-found page
template = env.get_template('not_found.html')
with open('site/not_found.html', 'w') as f:
f.writelines(template.render(page_title='Not Found', menu_items=MENU_ITEMS))
for item in MENU_ITEMS:
if item['page'] is None:
continue
page = item['page']
if page['template'] == 'not_found.html':
continue
template = env.get_template(page['template'])
if page['cpp'] is None or \
page['java'] is None or \
page['rust'] is None:
cpp_code = ''
java_code = ''
rust_code = ''
else:
cpp_code = read_code('code/cpp/' + page['cpp'])
java_code = read_code('code/java/' + page['java'])
rust_code = read_code('code/rust/' + page['rust'])
with open('site/' + page['template'], 'w') as f:
f.writelines(template.render(page_title=item['name'],
menu_items=MENU_ITEMS,
cpp_code=cpp_code,
java_code=java_code,
rust_code=rust_code))
print "Created: %s\tcpp: %d java: %d rust: %d" % (page['template'], len(cpp_code), len(java_code), len(rust_code))