cabinet/generate.py

46 lines
1.1 KiB
Python
Raw Normal View History

2022-01-14 18:07:31 -05:00
#!python
import os
import shutil
from markdown2 import markdown
input_ = 'files'
output = 'out'
templates = 'templates'
def render_template(**kwargs):
expanded = template[:]
for var, val in kwargs.items():
expanded = expanded.replace('${%s}' % var, val)
return expanded
with open(templates + '/base.html', 'r') as template:
template = template.read()
for root, dirs, files in os.walk(input_, topdown=True):
outroot = output + root[len(input_):]
os.makedirs(outroot, exist_ok=True)
shutil.copy(templates + '/stylesheet.css', outroot + '/stylesheet.css')
for file in files:
if file.endswith('.md'):
basename = file[:-3]
outfile = outroot + '/' + basename + '.html'
infile = root + '/' + file
with open(infile, 'r') as f:
content = f.read()
with open(outfile, 'w') as f:
content = markdown(content)
new_file = render_template(title=basename,
content=content)
f.write(new_file)