Code in a blog
No longer current; Zola has good built-in highlighting support
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):
- SyntaxHighlighter line-numbers got easily confused by long wrapping lines
- Prettify which wouldn't show up right
- highlight.js which documents custom initialisation that allowed me to integrate it with my jQuery DOM manipulation
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 easy_install 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.