-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmake.py
53 lines (40 loc) · 1.49 KB
/
make.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
#!python3
import os, sys, collections
import jinja2, pygments, pygments.lexers, pygments.formatters
def highlight(example):
with open('examples/' + example, 'r', encoding = 'utf-8') as fp:
code = fp.read()
lexer = pygments.lexers.get_lexer_for_filename(example)
formatter = pygments.formatters.HtmlFormatter()
highlighted = pygments.highlight(code, lexer, formatter)
return jinja2.Markup(highlighted)
def add_section(name):
sections[name] = { 'name': name, 'title': name.title() }
def render_part(name, is_section = True):
html_name = name + '.html'
template = env.get_template(html_name)
context = dict(base_context)
if is_section:
context['current_section'] = sections[name]
result = template.render(**context)
if not production:
os.makedirs('tmp', exist_ok = True)
html_name = 'tmp/' + html_name
with open(html_name, 'w', encoding = 'utf-8') as fp:
fp.write(result)
def render():
render_part('index', is_section = False)
for section in sections:
render_part(section)
sections = collections.OrderedDict()
production = len(sys.argv) >= 2 and sys.argv[1].startswith('prod')
base_context = {
'PROJECT': 'Lounge<C++>',
'HTTP': '' if production else 'http:',
'BASE': '' if production else '../',
'SECTIONS': sections,
'highlight': highlight,
}
loader = jinja2.FileSystemLoader('src')
env = jinja2.Environment(loader = loader, autoescape = True)
render()