|
|
@ -36,3 +36,41 @@ def index(folder): |
|
|
|
def compile(template, data): |
|
|
|
t = jinja2.Template(template) |
|
|
|
return t.render(**data) |
|
|
|
|
|
|
|
|
|
|
|
def compile_all(root_path): |
|
|
|
print('Compiling', root_path) |
|
|
|
|
|
|
|
md_path = os.path.join(root_path, 'markdown') |
|
|
|
files = [f |
|
|
|
for f in os.listdir(md_path) |
|
|
|
if f.endswith('.md')] |
|
|
|
|
|
|
|
data = {'pages': {}} |
|
|
|
for fname in files: |
|
|
|
full_path = os.path.join(md_path, fname) |
|
|
|
name, _ = os.path.splitext(fname) |
|
|
|
|
|
|
|
data['pages'][name] = parse_file(full_path) |
|
|
|
|
|
|
|
template_path = os.path.join(root_path, 'templates') |
|
|
|
templates = {} |
|
|
|
|
|
|
|
for file_name in os.listdir(template_path): |
|
|
|
name, ext = os.path.splitext(file_name) |
|
|
|
if ext == '.html': |
|
|
|
templates[name] = os.path.join(template_path, file_name) |
|
|
|
|
|
|
|
target_path = os.path.join(root_path, 'target') |
|
|
|
|
|
|
|
for page_name, page in data['pages'].items(): |
|
|
|
t_name = page.get('template', 'default') |
|
|
|
|
|
|
|
with open(templates[t_name]) as tf: |
|
|
|
t = jinja2.Template(tf.read()) |
|
|
|
|
|
|
|
out_path = os.path.join(target_path, page_name + '.html') |
|
|
|
out = t.render(**data['pages'][page_name]) |
|
|
|
|
|
|
|
with open(out_path, 'w') as f: |
|
|
|
f.write(out) |
|
|
|