ciclostile/ciclostile.py

66 lines
1.3 KiB
Python
Raw Normal View History

2017-10-30 20:58:15 +01:00
#!/usr/bin/env python3
import os
import markdown
import jinja2
template_file = 'template.html'
md_path = './markdown'
html_path = './html'
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):
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()