#!/usr/bin/env python3 import os.path import json from jinja2 import Environment, FileSystemLoader from utils.reST import reST_to_html def build_entry(jenv, fname, entry, older=None, newer=None): template = jenv.get_template('entry.html') source_file = 'entries/{}.rst'.format(entry['file']) with open(source_file, 'r') as f: source = f.read() rendered_source = reST_to_html(source) rendered = template.render(entry=entry, older=older, newer=newer, content=rendered_source) with open('out/{}.html'.format(fname), 'w+') as f: f.write(rendered) def build_archive(jenv, entries): archive_template = jenv.get_template('archive.html') rendered = archive_template.render(entries=entries) with open('out/archive.html', 'w+') as f: f.write(rendered) def load_metadata(name): with open(name) as f: entries = json.load(f) published = [] unpublished = [] for entry in entries: path = 'entries/{}.rst'.format(entry['file']) if not os.path.isfile(path): print('''Source file '{}' for entry '{}' does not exist. Skipping.''' .format(path, entry['title'])) continue if entry.get('published', "True") == "True": published.append(entry) else: unpublished.append(entry) return (published, unpublished) if __name__ == '__main__': jenv = Environment(loader=FileSystemLoader('templates'), trim_blocks=True, lstrip_blocks=True) published, unpublished = load_metadata('metadata.json') for entry in unpublished: build_entry(jenv, entry['file'], entry) for index, entry in enumerate(published): older = published[index-1:index] newer = published[index+1:index+2] build_entry(jenv, entry['file'], entry, older, newer) build_entry(jenv, 'index', published[-1], older=published[-2:-1]) build_archive(jenv, published)