diff options
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/build.py | 98 | ||||
-rw-r--r-- | scripts/utils/__init__.py | 0 | ||||
-rw-r--r-- | scripts/utils/reST.py | 13 |
3 files changed, 111 insertions, 0 deletions
diff --git a/scripts/build.py b/scripts/build.py new file mode 100644 index 0000000..52f2775 --- /dev/null +++ b/scripts/build.py @@ -0,0 +1,98 @@ +#!/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, 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 split_entries(entries): + 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) + + with open('entries.json') as f: + entries = json.load(f) + + published, unpublished = split_entries(entries) + + for entry in unpublished: + build_entry(jenv, entry) + + for index, entry in enumerate(published): + older = None + newer = None + if index > 0: + older = published[index-1] + if index < len(published) - 1: + newer = published[index+1] + + build_entry(jenv, entry, older, newer) + + if len(published) > 1: + build_index(jenv, published[-1], published[-2]) + else: + build_index(jenv, published[-1], None) + + build_archive(jenv, published) diff --git a/scripts/utils/__init__.py b/scripts/utils/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/scripts/utils/__init__.py diff --git a/scripts/utils/reST.py b/scripts/utils/reST.py new file mode 100644 index 0000000..d99758c --- /dev/null +++ b/scripts/utils/reST.py @@ -0,0 +1,13 @@ +from docutils import core + +def reST_to_html(source): + settings_overrides = { + 'footnote_references': 'superscript', + 'auto_id_prefix': 'id-', + 'initial_header_level': 3, + 'doctitle_xform': False, + } + formatted = core.publish_parts(source, + writer_name='html4css1', + settings_overrides=settings_overrides) + return formatted['body'] |