import os.path import json from jinja2 import Environment, FileSystemLoader from utils.reST import reST_to_html def build_entry(jenv, entry, older=None, newer=None): entry_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 = entry_template.render(entry=entry, older=older, newer=newer, content=rendered_source) with open('out/{}.html'.format(entry['file']), 'w+') as f: f.write(rendered) def build_index(jenv, entry, older=None): index_template = jenv.get_template('index.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 = index_template.render(entry=entry, older=older, content=rendered_source) with open('out/index.html', '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 entry_exists(entry): 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'])) return False return True if __name__ == '__main__': jenv = Environment(loader=FileSystemLoader('templates'), trim_blocks=True, lstrip_blocks=True) with open('entries.json') as f: entries = json.load(f) entries = list(filter(entry_exists, entries)) for index, entry in enumerate(entries): older = None newer = None if index > 0: older = entries[index-1] if index < len(entries) - 1: newer = entries[index+1] build_entry(jenv, entry, older, newer) if len(entries) > 1: build_index(jenv, entries[-1], entries[-2]) else: build_index(jenv, entries[-1], None) build_archive(jenv, entries)