Blogofile, reStructuredText, and Pygments
Blogofile has support out-of-the-box for reStructuredText and Pygments. Blogofile's syntax_highlight.py filter wants you to mark your code blocks with a token such as $$code(lang=python). I wanted to use the method I am more familiar with, by configuring reStructuredText with a custom directive. Luckily this is very easy. Here is how I did it.
First of all, I checked what version of Pygments I had since I used Ubuntu's package manager to install it. I then visited Pygments on BitBucket, and switched to the tag that matched my version. I then drilled into the external directory. I then saved the rst-directive.py file to my blog's local repository under the name _rst_directive.py. I named it with a leading underscore so that Blogofile would ignore it. If this bothers you, you could also add it to Blogofile's site.file_ignore_patterns setting.
Next, I tweaked the settings in _rst_directive.py by un-commenting the linenos variant.
All we have to do now is to get Blogofile to import this module. This can be accomplished by making use of the pre_build() hook in your _config.py file. This is a convenient place to hang custom code that will run before your blog is built. I added the following code to my _config.py module
def pre_build():
# Register the Pygments Docutils directive
import _rst_directive
This allows me to embed code in my .rst files with the sourcecode directive. For example, here is what I typed to create the source code snippet above:
.. sourcecode:: python
def pre_build():
# Register the Pygments Docutils directive
import _rst_directive
Of course to get it to look nice, we'll need some CSS. I used this Pygments command to generate a .css file for the blog.
$ pygmentize -f html -S monokai -a .highlight > pygments.css
I saved pygments.css in my css directory and updated my site template to link it in. Blogofile will copy this file into my _site directory when I build the blog.
Here is what I added to my blog's main .css file to style the code snippets. The important thing for me was to add an overflow: auto; setting. This will ensure that a scrollbar will appear on long lines instead of the code being truncated.
.highlight {
width: 96%;
padding: 0.5em 0.5em;
border: 1px solid #00ff00;
margin: 1.0em auto;
overflow: auto;
}
That's it!
Blog reboot with Blogofile
Welcome to my new blog. I've been meaning to start blogging again for some time, especially since the new version of SurfGuitar101.com went live almost two months ago. But the idea of dealing with WordPress was putting me off. Don't get me wrong, WordPress really is a nice general purpose blogging platform, but it didn't really suit me anymore.
I considered creating a new blog in Django, but I really want to spend all my time and energy on improving SurfGuitar101 and not tweaking my blog. I started thinking about doing something simpler.
Almost by accident, I discovered Blogofile by seeing it mentioned in my Twitter feed. Blogofile is a static blog generator written in Python. After playing with it for a while, I decided to use it for a blog reboot. It is simple to use, Pythonic, and very configurable. The advantages for me to go with a static blog are:
- No more dealing with WordPress and plugin updates. To be fair, WordPress is very easy to update these days. Plugins are still a pain, and are often needed to display source code.
- I can write my blog posts in Markdown or reStructuredText using my favorite editor instead of some lame Javascript editor. Formatting source code is dead simple now.
- All of my blog content is under version control.
- Easier to work offline.
- Easier to deploy. Very little (if any) server configuration.
- I can use version control with a post-commit hook to deploy the site.
Disadvantages:
- Not as "dynamic". For my blog, this isn't really a problem. Comments can be handled by a service like Disqus.
- Regenerating the entire site can take time. This is only an issue if you have a huge blog with years of content. A fresh blog takes a fraction of a second to build, and I don't anticipate this affecting me for some time, if ever. I suspect Blogofile will be improved to include caching and smarter rebuilds in the future.
It should be noted that Blogofile seems to require Python 2.6 or later. My production server is still running 2.5, and I can't easily change this for a while. This really only means I can't use Mercurial with a changegroup hook to automatically deploy the site. This should only be a temporary issue; I hope to upgrade the server in the future.
Blogofile comes with some scripts for importing WordPress blogs. Looking over my old posts, some of them make me cringe. I think I'll save importing them for a rainy day.
The bottom line is, this style of blogging suits me as a programmer. I get to use all the same tools I use to write code: a good text editor, the same markup I use for documentation, and version control. Deployment is a snap, and I don't have a database or complicated server setup to maintain. Hopefully this means I will blog more.
Finally, I'd like to give a shout-out to my friend Trevor Oke who just switched to a static blog for many of the same reasons.