Blog Tech, Part 6 - Syntax Highlighting with Pygments

November 17, 2013

Python and Pygments.

Technology 13: Pygments

One of my previous posts included some code, but it was simply marked up with a <pre> </pre> block. No good - I wanted some, any, syntax highlighting ASAP. One quick search later, and I was looking into the Python-based syntax highlighter Pygments (Get it? Python? Pygments?) Jekyll supports Pygments via its Liquid templates, and is now enabled by default.

Just include something like this in your Markdown:

{% highlight ruby %}
def foo
  puts 'foo'
{% endhighlight %}

Doing this tells Jekyll to generate HTML with a bunch of classes to colorize and otherwise handle the code according to the language of choice. Then, including the Pygments CSS file will render the colors and fonts appropriately.

(To see how I rendered that code snippet up above, complete with the highlight liquid markup in a .md file, check out the project's source code. Or, check out this Stackoverflow question)

Technology 14: Python

Notice that I said that you need Python for Pygments. That’s fine locally (on my Mountain Lion Mac), but what do I do in Heroku to enable Python?

I followed Heroku's advice and it was pretty easy. Essentially:

  • Create an empty requirements.txt in the root director of the project, to tell Heroku that I expect Python to be activated in the Cedar stack.
  • Optionally, create a runtime.txt to specify the version of Python that I want. I don't really care, so I didn't bother.
Tags: blogging, sass, heroku
comments powered by Disqus