-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.py
116 lines (86 loc) · 2.82 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import os
from os import path
from string import Template
from configparser import ConfigParser, ExtendedInterpolation
def get_name(p):
p = path.abspath(p)
absname, ext = path.splitext(p)
dir, name = path.split(absname)
return name
# == Reading templates == #
f = open('templates/page-template.html', 'r')
tmpl_page = Template(f.read())
f.close()
f = open('templates/index-template.html', 'r')
tmpl_index = Template(f.read())
f.close()
f = open('templates/index-item-template.html', 'r')
tmpl_index_item = Template(f.read())
f.close()
f = open('templates/frontpage-template.md', 'r')
tmpl_frontpage = Template(f.read())
f.close()
f = open('templates/frontpage-index-item.md', 'r')
tmpl_frontpage_index_item = Template(f.read())
f.close()
f = open('templates/frontpage-index-item-nohtml.md', 'r')
tmpl_frontpage_index_item_nohtml = Template(f.read())
f.close()
# == Read templates == #
# == Reading meta == #
meta = ConfigParser()
meta._interpolation = ExtendedInterpolation()
meta.read('meta.ini')
# == Read meta == #
index = ''
# == Generating pages == #
for style_name in ['default', 'structure']:
f = open('themes/' + style_name + '.css')
style = f.read()
f.close()
for root, subdirs, files in os.walk('html'):
for f in files:
if not f.endswith('.html') or f == 'index.html':
continue
model = dict()
fo = open('html/' + f, 'r')
model['license_text'] = fo.read()
fo.close()
id = model['license_id'] = get_name(f)
model['license_name'] = meta[id]['name']
model['style'] = style
model['style_name'] = style_name
index += tmpl_index_item.substitute(model)
fo = open('page/' + get_name(f) + ('' if style_name == 'default' else ('_' + style_name)) + '.html', 'w')
fo.write(tmpl_page.substitute(model))
fo.close()
# == Generated pages == #
# == Generating indexes == #
index = tmpl_index.substitute({'items': index})
f = open('page/index.html', 'w')
f.write(index)
f.close()
f = open('html/index.html', 'w')
f.write(index)
f.close()
# == Generated indexes == #
# == Generating frontpage == #
index = ''
for root, subdirs, files in os.walk('txt'):
for f in files:
if not f.endswith('.txt') or f == 'index.txt':
continue
model = dict()
id = model['license_id'] = get_name(f)
model['license_name'] = meta[id]['name']
if path.exists('html/' + id + '.html'):
index += tmpl_frontpage_index_item.substitute(model)
else:
index += tmpl_frontpage_index_item_nohtml.substitute(model)
index = index.split('\n')
index.sort()
index = '\n'.join(index)
f = open('index.md', 'w')
f.write(tmpl_frontpage.substitute({'index_items': index}))
f.close()
# == Generated frontpage == #