Introducing Kelvin for Static Websites
21 Apr 2010
About a year ago when I was blogging more, I discovered a neat static website generation tool called Jekyll. I really liked being able to edit my files in my favorite text editor. Unfortunately, it was difficult to add a few of the features I wanted, and I wasn't crazy about the Liquid template language Jekyll used, so I decided to spin off a Python version. The result of this work was Kelvin, the engine powering this blog. In this post, I'll describe Kelvin and how I use it.
Features
Even though Kelvin is a small python program, it packs enough features to make it suitable for people writing a static blog. Kelvin has the following features:
- Template framework (currently supporting jinja2 templates)
- A blog model that can be accessed within any template
- Small dependency list: pyyaml, jinja2
- Simple, but powerful extension mechanism for adding custom features
- Flexible metadata in posts so you can add data to your posts and retrieve them in your templates.
My own blog takes advantages of all these features and handles comments using disqus.
Usage
Kelvin steals a bunch of ideas from Jekyll. Kelvin operates on an input directory structure that looks similar to the following and produces a rendered website:
input:
- _layouts
- _posts
- python
- YYYY-MM-DD-article-title.html
- category2
- python
- _extensions
- about
- index.html
- index.html
output
- YYYY
- MM
- DD
- article-title.html
- DD
- MM
- about
- index.html
- index.html
The directories beginning with an underscore (”_”) all have special meaning, much like they do with Jekyll. The _layouts directory holds all of the templates. The _posts directory holds all of the categories and blog posts under each category. The _extensions directory allows for customization. You can do pretty much anything you want with extensions. On my site, I made a simple extension for creating a category page generator and a textile filter for my jinja2 templates.
The Kelvin engine will traverse through all folders/files on the site. For all posts and other regular files, it’ll look for a special metadata block beginning and ending with three dashes. Each of these files (pages) will be sent through the template engine. All other files, such as CSS, JavaScripts, or even non-template HTML files, will be copied to the output directory.
A sample Kelvin website that contains two blog posts, a home page and a simple Atom feed file is included with the project is.
Invoking Kelvin
Invoking Kelvin is very simple; it expects a source directory and an output directory. It’ll overwrite files as necessary in the output directory.
myserver% kelvin.py [path to source] [path to output]
Page File Format
Below is an example page for Kelvin. It’s broken into two blocks: the YAML descriptor block (at the top) and the content block. The YAML descriptor block contains all the metadata for the page. Kelvin exposes all of these metadata attributes to the page template. The engine only interprets the layout attribute. If Kelvin encounters a page, but doesn’t find a layout property, it assumes the page itself IS the template. This is handy for producing an RSS template for example.
---
layout: post.html
title: My Cool Story
blurb: "Here is a blurb that I've written."
---
<h1>HTML Header</h1>
<p>A simple paragraphy</p>
etc.
Page Properties
The following properties are available to all pages.
- layout: Kelvin will look for a template under the _layouts directory matching this name
- body: This is the translated body after. It’s useful when using template inheritance.
Post Properties
Blog posts expose several other properties to templates in addition to the page properties:
- url: The rendered URL relative to site root
- date: The date of the post
Developing Against
It’s fairly straightforward to get setup using Kelvin. You’ll just need to grab the source from github and install its dependencies. Kelvin requires the following packages: pyyaml 3.09, Jinja2 2.4.1. You should be able to install both of these using pip.
myserver% pip install pyyaml jinja2
What’s Next
I’m using Kelvin for a larger project I’m working on, so I’ll be adding a few features as I go:
- A proper python setup script
- Support for multiple python template engines
- Category (and possibly tag) support in the main engine (it’s currently an extension on my site).
- More examples, including the source to my site.