Django, Textile and syntax highlighting filters

Posted July 2009

EDIT: Note this refers to the old blog, not the > 2016 version…

There are already a fair few posts on this subject - I’m just collecting my thoughts really. The difficulty I’ve just had is wading through the options and their pros & cons. Points I had to consider:

  • must be able to support at least PHP, Python, Java, XHTML, XML, CSS, JS and BASH.
  • the site has to be put on shared hosting (ie without root access to install python modules)
  • fewer and smaller libraries / extra code is better - this is just for the occasional code snippet after all
  • but trickiest of all, perhaps, is that it had to integrate / not interfere with the existing textile markup processing (which I love, incidentally, and consider probably the most natural of all the various simple mark up languages.

This post at The Dig had some useful information (with its own nice code colouring - nothing like eating your own dog food). It uses Pygments for highlighting and Beautiful Soup for the HTML re-writing.

However, as the first commenter said (almost), you need to mark the returned code as safe ie no need to escape. This can be done by calling mark_safe() on the output code or, neater perhaps, pygmentize.is_safe = True.

Some gotchas I found integrating with Textile was:

  • <code>...</code> was unreliable, and pre. block modifier came out better.
  • bc. does what you want in the context of Textile alone.
  • The BeautifulSoup class will HTML-escape all its output (already escaped by textile’s block processor) before it even gets back to Django, so no amount of mark_safe() will save you there. After a lot of head-bashing, I found some mention of entities hidden away in the Beautiful Soup documentation which led me to use the BeautifulStoneSoup class[1] with convertEntities=HTML_ENTITIES. Voilà!

[1] Where do they get these names from?