Portrait of Martijn

Code in a blog

06 Aug 2011

My blog posts occasionally have some code, and I've been wondering for a while what the current/best way is to do that.

I was using embedded gists, which look good, have line-numbers, let you select and copy code without including the line numbers, let you later modify the code using git goodness, and have handy view/raw links. But, I ran into a few problems:

  • it only works with JavaScript enabled
  • the script tags didn't get executed with my fancy DOM-modifying JavaScript
  • sometimes not all embedded snippets would turn up (I've not tried to debug that)
  • I don't really want to depend on inline content hosted by third parties
  • I'm not wild about the "This Gist brought to you by GitHub" advertising

So for grins I spent some time evaluating some JavaScript highlighters (from this list of 9):

But in the end I gave up in disgust with JavaScript solutions, and went with a solution that was right under my nose all along: Jekyll supports Pygments syntax highlighting (see docs and this blog). The easyinstall is trivial, I used the github syntax.css, enabled pygments in config.yml, and that was that. It doesn't do non-selecting line-numbers, and there's no raw/view/download etc functionality, but that's fine; I can always link to a gist.