I’ve recently been looking at syntax highlighters, both for this blog and for Instahero, the new product my company is developing. I used to use a JS-based solution for the blog, but it broke with many inputs, it was fiddly to work with, didn’t parse correctly sometimes, and slowed the site down considerably. I especially disliked the fact that you had to include one JS and one CSS file per language you wanted to highlight, which either significantly constrained the highlighting that could be done easily, or increased the size of the page (and, consequently, the loading time) by a lot.

I didn’t know that a better solution existed, until I bumped into Pygments by mistake. I had heard of it, but I didn’t really know how it worked until a library I needed to use required it, so I had to install it. Pygments works by highlighting code server-side, producing a variety of output formats. For the format I was interested in, HTML, it also exports a (very small) CSS file with all the colors for the highlighting, so the amount the client needs to download is negligible, and it supports tens of languages with no overhead.

If you’re doing any sort of server-side processing and need to highlight code, there’s absolutely no reason not to use Pygments if you aren’t doing anything overly exotic. For an example of how Pygments works, the output it generates and what it looks like on a page, here’s an actual Django template tag you can use for your projects (just call it pygmentize.py and put it in your templatetags directory):

# Usage {{ code_string|pygmentize:"language" }}

import pygments
from django import template
from django.utils.safestring import mark_safe
from pygments import lexers
from pygments import formatters

register = template.Library()

@register.filter(name='pygmentize', is_safe=True)
def pygmentize(value, language):
    lexer = lexers.get_lexer_by_name(language)
    output = pygments.highlight(value, lexer, formatters.HtmlFormatter(cssclass="syntax"))
    return mark_safe(output)