-
Notifications
You must be signed in to change notification settings - Fork 8
/
render.py
30 lines (25 loc) · 844 Bytes
/
render.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
from jinja2 import Template
from docxtpl import DocxTemplate
from yaml import load, SafeLoader
doc = DocxTemplate('syllabus-template.docx')
md = Template(open('syllabus-template.md', 'rt').read())
context = load(open('syllabus.yaml'), Loader=SafeLoader)
file_name = '-'.join('{} {}'.format(context['code'], context['title']).split())
# replace NoneType nodes with an empty list
def replace_none(node):
if isinstance(node, dict):
for key, value in node.items():
node[key] = replace_none(value)
elif isinstance(node, list):
for i, value in enumerate(node):
node[i] = replace_none(value)
elif node is None:
return []
return node
context = replace_none(context)
# save .md
with open('{}.md'.format(file_name), 'wt') as file:
file.write(md.render(context))
# then .docx
doc.render(context)
doc.save('{}.docx'.format(file_name))