From 5591d977998a189573e8f780a5270ab81bb56ddc Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Sat, 12 Nov 2016 02:06:41 +0100 Subject: Initial commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Johannes Löthberg --- build.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++ entries.json | 7 ++++ entries/new-blog.rst | 10 ++++++ templates/archive.html | 9 +++++ templates/entry.html | 26 +++++++++++++++ templates/index.html | 18 ++++++++++ templates/layout.html | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ utils/__init__.py | 0 utils/reST.py | 13 ++++++++ 9 files changed, 256 insertions(+) create mode 100644 build.py create mode 100644 entries.json create mode 100644 entries/new-blog.rst create mode 100644 templates/archive.html create mode 100644 templates/entry.html create mode 100644 templates/index.html create mode 100644 templates/layout.html create mode 100644 utils/__init__.py create mode 100644 utils/reST.py diff --git a/build.py b/build.py new file mode 100644 index 0000000..a9b328b --- /dev/null +++ b/build.py @@ -0,0 +1,83 @@ +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) diff --git a/entries.json b/entries.json new file mode 100644 index 0000000..0d84ec5 --- /dev/null +++ b/entries.json @@ -0,0 +1,7 @@ +[ + { + "title": "New blog!", + "date": "2016-11-12", + "file": "new-blog" + } +] diff --git a/entries/new-blog.rst b/entries/new-blog.rst new file mode 100644 index 0000000..594b44a --- /dev/null +++ b/entries/new-blog.rst @@ -0,0 +1,10 @@ +After thinking about cleaning up my blog building scripts, and inspired by `chee's blog post`_, I figured it was time to finally rewrite them from scratch! + +The two ugliest parts of of the old system that I managed to get rid of now was the ordering of posts, and specifying entry metadata. Previously I used numbered filenames, and a yaml frontmatter that I split out of the entry before parsing, but I replaced both with a file called ``entries.json``. It contains a list of objects, with the objects containing the metadata for the entry, e.g. title, date, et.c., to be built, and the order of the objects defining the order of the entries. + +One current deficiency is that there's no support for draft entries, but I'm fairly certain that I know how I'm going to implement it. + +Oh, and I nuked all my old posts, since there was nothing interesting in there. They're still accessible through the wayback machine, and my website's git history though. + + +.. _`chee's blog post`: https://blog.snaek.org/post/blogs-a-stupid-blog-thing diff --git a/templates/archive.html b/templates/archive.html new file mode 100644 index 0000000..506e899 --- /dev/null +++ b/templates/archive.html @@ -0,0 +1,9 @@ +{% extends 'layout.html' %} + +{% block content %} + +{% endblock %} diff --git a/templates/entry.html b/templates/entry.html new file mode 100644 index 0000000..25a5f25 --- /dev/null +++ b/templates/entry.html @@ -0,0 +1,26 @@ +{% extends 'layout.html' %} + +{% block content %} +
+
+

{{ entry['title'] }}

+ +
+
+ {{ content }} +
+
+{% if older and newer%} +
+ OlderNewer +
+{% elif older %} +
+ Older +
+{% elif newer %} +
+ Newer +
+{% endif %} +{% endblock %} diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..0cabcdb --- /dev/null +++ b/templates/index.html @@ -0,0 +1,18 @@ +{% extends 'layout.html' %} + +{% block content %} +
+
+

{{ entry['title'] }}

+ +
+
+ {{ content }} +
+
+{% if older %} +
+ Older +
+{% endif %} +{% endblock %} diff --git a/templates/layout.html b/templates/layout.html new file mode 100644 index 0000000..92ec496 --- /dev/null +++ b/templates/layout.html @@ -0,0 +1,90 @@ + + + + +{% block head %}{% endblock %} + + + + +
+ +
+
+{% block content %}{% endblock %} +
+ + + diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils/reST.py b/utils/reST.py new file mode 100644 index 0000000..d99758c --- /dev/null +++ b/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'] -- cgit v1.2.3-54-g00ecf