From 3a703d05efc4536dab7b21fb702253238a01666e Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Tue, 21 Oct 2014 15:19:59 +0200 Subject: bupa: bunch of refactoring... --- scripts/bupa | 123 ++++++++++++++++++++++++--------------------- src/templates/entry.html | 16 +++--- src/templates/journal.html | 2 +- 3 files changed, 76 insertions(+), 65 deletions(-) diff --git a/scripts/bupa b/scripts/bupa index 6770b48..dfb03df 100755 --- a/scripts/bupa +++ b/scripts/bupa @@ -1,22 +1,15 @@ #!/usr/bin/env python -"""Usage: bupa +"""Usage: bupa Options: - -h --help Show this screen. + -h, --help Show this screen. + -v, --version Show the version information. """ from jinja2 import Environment, FileSystemLoader import glob, yaml, docopt, datetime from docutils import core from pyatom import AtomFeed - - -def reST_to_html(s): - return core.publish_parts(s, writer_name='html4css1')['fragment'] - -def filename_to_id(file_name): - return int(file_name.split('/')[-1]. - split('-')[0]. - split('.')[0]) +from os import path class Page(object): def __init__(self, page_dict): @@ -29,11 +22,25 @@ class Page(object): self.author_link = page_dict['author_link'] self.body = page_dict['body'][0] +def reST_to_html(s): + return core.publish_parts(s, writer_name='html4css1')['fragment'] + +def filename_to_id(file_name): + filename = path.split(file_name)[1] + file_id = filename.split('-')[0].split('.')[0] + return int(file_id) + +def filename_to_extless(filename): + filename = path.split(filename)[1] + extless = path.splitext(filename)[0] + return extless + def parse(filename): with open(filename, 'rb') as file: data = file.read().decode('utf-8') frontmatter, *body = data.split('\n\n', 1) + frontmatter = frontmatter.partition('\n')[2] frontmatter = yaml.load(frontmatter) @@ -45,68 +52,72 @@ def parse(filename): page = Page(page_dict) return page -def build_journal(env): - template = env.get_template('journal.html') - entry_tmpl = env.get_template('entry.html') - files = glob.glob('src/journal/*.rst') +def build_entry(filename, atom_feed): + entry = parse(filename) + entry.id = filename_to_id(filename) + entry.page = filename_to_extless(filename) + '.html' + entry.body = reST_to_html(entry.body) + atom_feed.add( + title=entry.title, + content=entry.body, + content_type="html", + author={'name': entry.author, 'uri': entry.author_link}, + url="https://theos.kyriasis.com/~kyrias/"+entry.page, + updated=entry.date + ) + return entry + +def build_journal(jinja_env): + template = jinja_env.get_template('journal.html') + files = glob.glob('src/journal/*.rst') files.sort(key=filename_to_id, reverse=True) - feed = AtomFeed( - title="kyrias’ journal", - subtitle="The lost journal of Kyrias", - feed_url="https://theos.kyriasis.com/~kyrias/journal.xml", - url="https://theos.kyriasis.com/~kyrias/journal.html", - author={'name': 'Johannes Löthberg', - 'uri': 'https://theos.kyriasis.com/~kyrias/about.html'}, - updated=datetime.datetime.utcnow(), - generator=('bupa', '', '0.0.1') + atom_feed = AtomFeed( + title = "kyrias’ journal", + subtitle = "The lost journal of Kyrias", + feed_url = "https://theos.kyriasis.com/~kyrias/journal.xml", + url = "https://theos.kyriasis.com/~kyrias/journal.html", + author = { 'name': 'Johannes Löthberg', + 'uri': 'https://theos.kyriasis.com/~kyrias/about.html' }, + updated = datetime.datetime.utcnow(), + generator = ('bupa', '', '0.0.1') ) entries = [] for file in files: - entry = parse(file) - entry.id = filename_to_id(file) - entry.page = file.split('/', 1)[1].split('.', 1)[0] + '.html' - entry.body = reST_to_html(entry.body) + entry = build_entry(file, atom_feed) + write_page(entry, 'The lost journal', 'journal/'+entry.page, 'entry.html', jinja_env) entries += [entry] - with open('build/'+entry.page, 'wb') as file: - file.write(entry_tmpl.render(entry=entry, title='~/journal', - header='The lost journal').encode('utf-8')) - feed.add( - title=entry.title, - content=entry.body, - content_type="html", - author={'name': entry.author, 'uri': entry.author_link}, - url="https://theos.kyriasis.com/~kyrias/"+entry.page, - updated=entry.date - ) with open('build/journal.html', 'wb') as file: - file.write(template.render(entries=entries, title='~/journal', - header='The lost journal').encode('utf-8')) - with open('build/journal.xml', 'wb') as file: - file.write(feed.to_string().encode('utf-8')) + journal = template.render(entries=entries, title='~/journal', header='The lost journal') + file.write(journal.encode('utf-8')) -def build_page(env, templ_name, pagename): - template = env.get_template(templ_name) + with open('build/journal.xml', 'wb') as file: + file.write(atom_feed.to_string().encode('utf-8')) +def build_page(jinja_env, template, pagename): page = parse('src/'+pagename+'.rst') page.body = reST_to_html(page.body) page.article_id = pagename + write_page(page, page.header, pagename+'html', template, jinja_env) - with open('build/'+pagename+'.html', 'wb') as file: - file.write(template.render(page=page, title=page.title, header=page.header).encode('utf-8')) +def write_page(page, header, filename, template, jinja_env): + template = jinja_env.get_template(template) + with open('build/'+filename, 'wb') as file: + rendered_page = template.render(page=page, title=page.title, header=header) + file.write(rendered_page.encode('utf-8')) def main(): - arguments = docopt.docopt(__doc__) - env = Environment(loader=FileSystemLoader('src/templates')) - - if arguments[''] == 'journal.html': - build_journal(env) - elif arguments[''] == 'index.html': - build_page(env, 'page.html', 'index') - elif arguments[''] == 'about.html': - build_page(env, 'page.html', 'about') + arguments = docopt.docopt(__doc__, version='bupa 0.0.1') + jinja_env = Environment(loader=FileSystemLoader('src/templates')) # something something jinja2 templates + + if arguments[''] == 'journal.html': + build_journal(jinja_env) + elif arguments[''] == 'index.html': + build_page(jinja_env, 'page.html', 'index') + elif arguments[''] == 'about.html': + build_page(jinja_env, 'page.html', 'about') if __name__ == '__main__': main() diff --git a/src/templates/entry.html b/src/templates/entry.html index 1890104..4335bbf 100644 --- a/src/templates/entry.html +++ b/src/templates/entry.html @@ -7,26 +7,26 @@ {% block content %}
+ class="entry" id="entry:{{ page.id }}"> -

{{ entry.title }}

+

{{ page.title }}

Published on -
- {{ entry.body }} + {{ page.body }}