commit 7b41841dd25aa2a7fb6381f007f914152cc601ba Author: User Identifier Date: Mon Oct 30 20:58:15 2017 +0100 First commit diff --git a/ciclostile.py b/ciclostile.py new file mode 100755 index 0000000..af196fb --- /dev/null +++ b/ciclostile.py @@ -0,0 +1,65 @@ +#!/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() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f27036f --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +markdown +jinja2