Friday 16 May 2008

Blogging with docutils

I've always liked using reStructuredText to write documents, it seems to fit with my way of thinking, and it is really quick to produce quite a pretty but clean looking document.

So far for blogging I've been using Windows Live Writer to compose my posts, and to get the formatting and layout I want I usually have to drop down to HTML view and hack it around a bit, but for this blog I'm using reST along with a slightly hacked front end so that it supports syntax highlighting (no way am I going to do multi-coloured Python code manually).

I grabbed the latest docutils from subversion. There seem to be several alternative versions of syntax highlighting in the docutils sandbox (and elsewhere on the web) but not yet a standard one. Eventually I settled on rst2html-highlight.py in the sandbox. Sadly it didn't do exactly what I wanted, so I had to modify it a bit. With the original I can write something like:

Now try printing some queries:

.. code-block:: pycon

>>> q = Person.all()
>>> print showQuery(q)
Person.all()
>>> print showQuery(q.filter("last_name =", "Smith"))
Person.all().filter('last_name =', 'Smith')

If we have more than one filter, showQuery will always output them in


Which gives me:




Now try printing some queries:



>>> q = Person.all()
>>> print showQuery(q)
Person.all()
>>> print showQuery(q.filter("last_name =", "Smith"))
Person.all().filter('last_name =', 'Smith')


If we have more than one filter, showQuery will always output them in




but I added in the ability to format an included code block:



In the code samples which follow the test class is:

.. code-block:: python
:include: ../Person.py
:start-after: from google.appengine.api import users

First some setup code for the tests:


which results in:




In the code samples which follow the test class is:



class Person(db.Model):
"""Dummy class for testing"""
first_name = db.StringProperty()
last_name = db.StringProperty()
city = db.StringProperty()
birth_year = db.IntegerProperty()
height = db.IntegerProperty()


First some setup code for the tests:




Ok, I like that: I just wrote 4 slightly different includes to get those examples and absolutely no copy/paste.



The highlighting is done using pygments which has a long list of supported languages. It also claims to come with a directive for docutils, but so far as I can see that isn't included in the egg so for now I'll stick with the one in docutils sandbox (of course if I installed docutils from an egg then I wouldn't have that one either).



Once I've formatted the document then I cut and paste into Windows Live Writer and upload it to the blog. I updated my blog skin to include the appropriate css in the skin so any changes to formatting will then apply consistently throughout the site. Eventually I might try to shortcut the cut/paste/upload step but for now it gives me an easy way to preview the post in the final skin.



You can find the code for all of this at http://code.google.com/p/kupuguy/source/browse/trunk/appengine-doctests

0 comments: