Add syntax highlighting to your static blog with Python and Pygments
Posted on March 19, 2012 by Paul
Recently I’ve converted my blog from WordPress to a static website generated with Jekyll, my only problem was with the way Jekyll handles syntax highlighting through Pygments, specifically with changing the background of more than a line of code. While doing this for a single line of code works perfectly, I wasn’t able to find a way to change the background of a group of lines, like for example the second and the fourth line from a piece of code.
Changing the background for a specific line of code is the equivalent of using a marker on a paper with printed code, it is especially important to have this kind of fine control over the details when you write about programming.
In the past I’ve used WordPress and a JavaScript syntax highlighter plugin to render my code snippets. So I was accustomed with marking code this way:
[code lang="cpp" highlight="1 9 15"] ... [/code]
Using the above markers a code like this:
//Unicode strings in C++11 #include <iostream> #include <vector> #include <string> using namespace std; int main(){ string aux = u8"Hello, World"; string aux2 = "Hello, World"; cout << aux << endl; ... string tst=u8"您好世界"; cout << tst << endl; return 0; }
can be rendered as:
Obviously a JavaScript solution like the above can be used with Jekyll, you just need to include the relevant JS files in your pages. There is however a problem with using this - it looks really bad on mobile devices and it won’t work if the reader has JS disabled. On the other hand a code highlighted with Pygments looks the same on a desktop and on an iPad for example.
You can use Pygments directly as a stand alone program or as a module in a Python code. I wanted to be able to automatically highlight snippets of code, in different programming languages, in a bunch of Markdown or html files, using a simple syntax like the one I was accustomed to.
In order to be able to do this, I wrote a simple Python script that will parse all my posts and, using Pygments, will highlight any code delimited by [code lang=”___” ] … [/code].
This code will add only html tags to your file. You will also need to generate a css style from Pygments if you want to have colorized syntax, this can be achieved with:
You can pick from more than a dozen styles already defined in Pygments (e.g. borland, vim, emacs …) or you can define your own if you want. For a complete list of programming languages and styles supported, see the Pygments documentation.
Here is the complete Python code used to highlight the syntax for this blog:
The above code will recursively parse all files from the origin folder and will use Pygments to highlight every piece of code will find, the modified files will be saved in the destination folder. You will need to change the names of the origin and destination folder to what you have on your computer. In my case these are named _orig and _posts.
If you need more control you can easily modify the above code to let you pass more Pygments options.
The above code is perfectly integrated in my blogging workflow:
- Use your preferred text editor to write a post in html or Markdown.
- Run the above syntax highlighter Python code.
- Run Jekyll to generate your static blog.
- Minify your site.
- Deploy your changes.
If you are a novice Python programmer, or if you want to learn more about Python, you can read the official tutorial from Python’s website.
If you prefer to read a book, a good introduction is Mark Lutz’s Learning Python