diff options
author | Johannes Löthberg <johannes@kyriasis.com> | 2014-10-14 01:22:31 +0200 |
---|---|---|
committer | Johannes Löthberg <johannes@kyriasis.com> | 2014-10-14 01:27:05 +0200 |
commit | d9ae9febd9668f89c8694b66d7a0323027d416b2 (patch) | |
tree | 20d40d7b9e939c90eb7cd8b09fb7f076722c3443 /scripts/bupa | |
parent | fd1c56a9763ab2da0edcfdfbafc51db369c3950f (diff) | |
download | website-d9ae9febd9668f89c8694b66d7a0323027d416b2.tar.xz |
redo website using bupa, ReST, and jinja templates
Diffstat (limited to 'scripts/bupa')
-rwxr-xr-x | scripts/bupa | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/scripts/bupa b/scripts/bupa new file mode 100755 index 0000000..eca289a --- /dev/null +++ b/scripts/bupa @@ -0,0 +1,93 @@ +#!/usr/bin/env python +"""Usage: bupa <filename> + +Options: + -h --help Show this screen. +""" +from jinja2 import Environment, FileSystemLoader +import os, yaml, docopt + +from docutils import core +from docutils.writers.html4css1 import Writer + +def reST_to_html(s): + return core.publish_parts(s, writer_name='html4css1')['html_body'] + +def filename_to_id(file_name): + return int(file_name.split('/')[-1]. + split('-')[0]. + split('.')[0]) + +class Entry(object): + def __init__(self, entry_dict): + self.id = None + self.title = entry_dict['title'] + self.date = entry_dict['date'] + self.author = entry_dict['author'] + if 'author_link' in entry_dict: + self.author_link = entry_dict['author_link'] + self.body = entry_dict['body'][0] + +def parse(filename): + with open(filename, 'r') as file: + data = file.read() + + frontmatter, *body = data.split('\n\n', 1) + frontmatter = frontmatter.partition('\n')[2] + frontmatter = yaml.load(frontmatter) + + entry_dict = frontmatter.copy() + entry_dict['body'] = body + + entry = Entry(entry_dict) + return entry + +def build_journal(env): + template = env.get_template('journal.html') + + files = os.listdir('src/journal') + files.sort(key=filename_to_id, reverse=True) + + entries = [] + for file in files: + entry = parse('src/journal/' + file) + entry.body = reST_to_html(entry.body) + entry.id = filename_to_id(file) + entries += [entry] + + with open('build/journal.html', 'w') as file: + file.write(template.render(entries=entries, title='~/journal')) + +def build_index(env): + template = env.get_template('page.html') + + page = parse('src/index.rst') + page.body = reST_to_html(page.body) + page.article_id = 'index' + + with open('build/index.html', 'w') as file: + file.write(template.render(page=page, title=page.title)) + +def build_about(env): + template = env.get_template('page.html') + + page = parse('src/about.rst') + page.body = reST_to_html(page.body) + page.article_id = 'about' + + with open('build/about.html', 'w') as file: + file.write(template.render(page=page, title='~/about')) + +def main(): + arguments = docopt.docopt(__doc__) + env = Environment(loader=FileSystemLoader('src/templates')) + + if arguments['<filename>'] == 'journal.html': + build_journal(env) + elif arguments['<filename>'] == 'index.html': + build_index(env) + elif arguments['<filename>'] == 'about.html': + build_about(env) + +if __name__ == '__main__': + main() |