-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
45 lines (36 loc) · 1.46 KB
/
Copy pathbuild.py
File metadata and controls
45 lines (36 loc) · 1.46 KB
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
#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
import CommonMark
import jinja2
from codecs import open
from jinja2 import Environment, FileSystemLoader
pages = (
{'input': 'jeu-troll.md', 'output': 'index.html'},
{'input': 're-troll.md', 'output': 're-troll.html', 'subtitle': "re-troll"},
{'input': ['jeu-troll.md', 're-troll.md'], 'output': 'print.html', 'template': 'print.html'},
)
if __name__ == '__main__':
default_template = 'base.html'
for page in pages:
template_name = page.get('template', default_template)
output_file = page.get('output')
input_files = page.get('input')
subtitle = page.get('subtitle', None)
print("{} + {} = {}".format(input_files, template_name, output_file))
parser = CommonMark.DocParser()
renderer = CommonMark.HTMLRenderer()
if isinstance(input_files, (str, unicode)):
input_files = [input_files]
contents = []
for input_file in input_files:
with open(input_file, encoding='utf8') as fd:
contents.append(fd.read())
content = '\n----\n'.join(contents)
ast = parser.parse(content)
html = renderer.render(ast)
env = Environment(loader=FileSystemLoader('templates'))
template = env.get_template(template_name)
output = template.render(content=html, subtitle=subtitle)
with open(output_file, 'w', encoding='utf8') as fd:
fd.write(output)