2017-10-30 20:58:15 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import os
|
|
|
|
import markdown
|
|
|
|
import jinja2
|
|
|
|
|
2017-11-17 15:08:02 +01:00
|
|
|
template_path = 'templates'
|
2017-10-30 20:58:15 +01:00
|
|
|
md_path = './markdown'
|
2017-11-16 23:36:42 +01:00
|
|
|
html_path = './target'
|
2017-10-30 20:58:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
def write_page(page, out_name):
|
|
|
|
out_path = os.path.join(html_path, out_name)
|
|
|
|
with open(out_path, 'w') as f:
|
|
|
|
f.write(page)
|
|
|
|
|
|
|
|
|
|
|
|
def render_page(data):
|
2017-11-17 15:08:02 +01:00
|
|
|
template_name = data.get('template', 'default')
|
|
|
|
template_file = os.path.join(template_path, template_name + '.html')
|
|
|
|
|
2017-10-30 20:58:15 +01:00
|
|
|
with open(template_file) as f:
|
|
|
|
template = f.read()
|
|
|
|
|
|
|
|
page = jinja2.Template(template).render(**data)
|
|
|
|
write_page(page, data['file_name'])
|
|
|
|
|
|
|
|
|
|
|
|
def read_md(md_file):
|
|
|
|
full_name = os.path.join(md_path, md_file)
|
|
|
|
with open(full_name) as f:
|
|
|
|
md_text = f.read()
|
|
|
|
return md_text
|
|
|
|
|
|
|
|
|
|
|
|
def make_page(md_file):
|
|
|
|
md_text = read_md(md_file)
|
|
|
|
|
|
|
|
md = markdown.Markdown(extensions=['meta'], output_format='html5')
|
|
|
|
html = md.convert(md_text)
|
|
|
|
|
|
|
|
# get markdown metadata
|
|
|
|
data = {key: md.Meta[key][0]
|
|
|
|
for key in md.Meta.keys()}
|
|
|
|
|
|
|
|
out_name = os.path.splitext(md_file)[0] + '.html'
|
|
|
|
|
|
|
|
data.update({
|
|
|
|
'content': html,
|
|
|
|
'file_name': out_name,
|
|
|
|
})
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def valid_md(md_file):
|
|
|
|
full_name = os.path.join(md_path, md_file)
|
|
|
|
return os.path.isfile(full_name) and full_name.endswith('.md')
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
for md_file in os.listdir(md_path):
|
|
|
|
if valid_md(md_file):
|
|
|
|
page = make_page(md_file)
|
|
|
|
render_page(page)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|