summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--build.py83
-rw-r--r--entries.json7
-rw-r--r--entries/new-blog.rst10
-rw-r--r--templates/archive.html9
-rw-r--r--templates/entry.html26
-rw-r--r--templates/index.html18
-rw-r--r--templates/layout.html90
-rw-r--r--utils/__init__.py0
-rw-r--r--utils/reST.py13
9 files changed, 256 insertions, 0 deletions
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 %}
+<ul id="archive-list">
+{% for entry in entries %}
+<li>{{ entry['date'] }} — <a href="{{ entry['file'] }}.html">{{ entry['title'] }}</a></li>
+{% endfor %}
+</ul>
+{% 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 %}
+<article itemscope itemtype="http://schema.org/Article">
+ <div>
+ <h1>{{ entry['title'] }}</h1>
+ <time itemprop="datePublished" datetime="{{ entry['date'] }}">{{ entry['date'] }}</time>
+ </div>
+ <div>
+ {{ content }}
+ </div>
+</article>
+{% if older and newer%}
+<div>
+ <a href="{{ older['slug'] }}.html">Older</a> — <a href="{{ newer['slug'] }}.html">Newer</a>
+</div>
+{% elif older %}
+<div>
+ <a href="{{ older['slug'] }}.html">Older</a>
+</div>
+{% elif newer %}
+<div>
+ <a href="{{ newer['slug'] }}.html">Newer</a>
+</div>
+{% 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 %}
+<article itemscope itemtype="http://schema.org/Article">
+ <div>
+ <h1>{{ entry['title'] }}</h1>
+ <time itemprop="datePublished" datetime="{{ entry['date'] }}">{{ entry['date'] }}</time>
+ </div>
+ <div>
+ {{ content }}
+ </div>
+</article>
+{% if older %}
+<div id="post-nav">
+ <a class="older" href="{{ older['slug'] }}.html">Older</a>
+</div>
+{% 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 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <meta charset="UTF-8">
+{% block head %}{% endblock %}
+ <style>
+body {
+ max-width: 42rem;
+}
+a, a:visited {
+ text-decoration: none;
+ color: #32609C;
+}
+a:hover {
+ color: #339;
+}
+header nav ul {
+ padding: 0em;
+ list-style-type: none;
+}
+header nav ul li {
+ display: inline;
+ margin-right: 0.25rem;
+}
+header nav a, header nav a:visited {
+ color: #444;
+}
+header nav a:hover {
+ color: #000;
+}
+article {
+ padding-left: 0.5rem;
+}
+article p {
+ white-space: pre-wrap;
+}
+#post-nav {
+ padding-bottom: 0.25em;
+}
+ul#archive-list {
+ list-style-type: none;
+}
+footer {
+ height: 55px;
+}
+footer p#copy {
+ margin: 0em;
+ padding-top: 0.25em;
+ padding-bottom: 0.25em;
+}
+footer ul#contact-info {
+ margin: 0;
+ padding: 0em;
+ padding-top: 0.25em;
+ padding-bottom: 0.25em;
+ list-style-type: none;
+}
+footer ul#contact-info li {
+ display: inline;
+ margin-right: 0.25rem;
+}
+ </style>
+ </head>
+
+ <body>
+ <header>
+ <nav>
+ <ul>
+ <li><a href="index.html">index</a></li>
+ <li><a href="archive.html">archive</a></li>
+ </ul>
+ </nav>
+ </header>
+ <hr />
+{% block content %}{% endblock %}
+ <hr />
+ <footer>
+ <div>
+ <p id="copy">© 2016 Johannes Löthberg</p>
+ </div>
+
+ <div>
+ <ul id="contact-info">
+ <li><a rel="me" href="mailto:johannes@kyriasis.com">Email</a></li>
+ <li><a rel="me" href="https://twitter.com/kyriasis">Twitter</a></li>
+ </ul>
+ </div>
+ </footer>
+ </body>
+</html>
diff --git a/utils/__init__.py b/utils/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/utils/__init__.py
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']