Syntax Highlighting in Django with Markdown and Pygments

I find Markdown to be a more readable and usable alternative to XHTML/CSS for formatting text, and I use it to format my articles at this Django-powered blog. When implementing syntax highlighting for code blocks within text, I searched for existing solutions and found many approaches that were too complicated and had shortcomings. After more research, I realized that syntax highlighting works out of the box in Django if you have a recent version of Markdown.

Here are the required steps to enable syntax highlighting in your Django application. First, install python-markdown version 2.0+ and python-pygments. Pygments is a syntax highlighter written in Python. Markdown 2.0+ has an extension system and comes with a syntax highlighting extension that uses Pygments. This extension is called CodeHilite. To use it, add the following to a Django template:

{% load markup %}
{{ text|markdown:'codehilite' }}

Next, you need to create a stylesheet that defines colors for syntax highlighting. To do so, run the following command:

$ pygmentize -S default -f html -a .codehilite > code.css

Include code.css in your template.

Now, to create a syntax-highlighted code block, indent the block by 4 spaces and declare the language of the block at the first line, prefixed by ::: (3 colons). This is better explained by example. The following text:

:::python
print 'Hello, World.'

Produces the following syntax-highlighted code block:

print 'Hello, World.'

Keep in mind that Markdown allows embedded HTML elements by default. You shouldn't enable this if the source of the text is untrusted. To disable HTML elements, use the following in your Django template instead:

{% load markup %}
{{ text|markdown:'safe,codehilite' }}

Pygments supports a long list of languages and styles. Be sure to check the demos too.

Blog Migrated to Django

I've been meaning to migrate my website from Drupal to Django for a very long time. Although Drupal is an excellent content management system, I got tired of working with PHP every time I wanted to add a feature or make a change. My previous web host didn't support Python so I had to stick with PHP. Recently however, I moved the website to a VPS at Linode and decided to migrate to Django as well.

Writing a blog application in Django took very little time thanks to the reusable apps that come with Django, like syndication, comments and admin. I also had to port the blog design to Django templates and migrate the content from HTML to Markdown. I've been using Markdown at StackOverflow and I really like it. It's concise, readable and much easier to work with in a text editor than HTML. I wrote a small script to convert existing articles from the subset of HTML that I was using to Markdown.

To run the website, I'm using Apache2/mod_wsgi for the backend, and nginx as a frontend. I chose mod_wsgi because it's very flexible. As for nginx, I chose it because it integrates nicely with StaticGenerator, a Django middleware that caches pages as files on local disk. StaticGenerator has an important advantage over using Memcached with Django: cached pages are served by nginx without hitting Django at all, so it's much faster. A quick benchmark on my setup showed that it was 8 times faster. StaticGenerator can only cache full pages, but this is fine for my needs.

The blog feed now contains full articles (as opposed to short summaries). This should be more convenient to those who read the blog via the feed.

PyCon 2010 Lightning Talk, Python Debugging Techniques

I did a lightning talk at PyCon 2010 based on my Python debugging techniques article. Here is the video. My talk starts around 7:30:

And here are the slides:

Python Debugging Techniques

This article covers several techniques for debugging Python programs. The applicability of these techniques ranges from simple scripts to complex applications. The topics that are covered include launching an interactive console from within your program, using the Python debugger, and implementing robust logging. Various tips are included along the way to help you debug and fix problems quickly and efficiently.

Read more »

How to Debug Bash Scripts

Bash is the default scripting language in most Linux systems. Its usage ranges from an interactive command interpreter to a scripting language for writing complex programs. Debugging facilities are a standard feature of compilers and interpreters, and bash is no different in this regard. In this article, I will explain various techniques and tips for debugging Bash scripts.

Read more »

Version Control for Linux Configuration (/etc) with etckeeper

Keeping a version history of files under /etc is essential for maintaining a healthy system. The benefits of tracking changes to /etc include:

  • Documentation: The log messages that are attached to configuration changes serve as documentation. These log messages record who made the change, when and why. Understanding the contents of a config file becomes much easier if you have a full history of the changes that were made to this file.
  • Troubleshooting: Misconfiguration can result in a variety of problems. When a service starts to misbehave, one of the things you can do to troubleshoot the issue is to check the version history of its config file. There, you can see if any changes were made around the time frame in which the problem happened. If you spot a change that may be causing the issue, you can easily revert it to fix the problem.

You can set up your own repository to track changes to /etc, or you can use a tool called etckeeper to handle the setup for you. This tool supports multiple version control systems, including Git, Mercurial and Bazaar. It integrates with the package management systems of a number of Linux distros, including APT (used by Debian, Ubuntu), YUM (RedHat, CentOS, Fedora), Pacman (Arch Linux). Using etckeeper instead of rolling your own has some advantages:

  • etckeeper integration with package managers means than you don't need to manually commit changes in /etc after installing packages.
  • etckeeper comes pre-configured with a list of files that live in /etc but usually do not benefit from version control (like some cache files).

Read on to learn how to install, configure and use etckeeper.

Read more »

"Django 1.0 Website Development" Released

My author copies of "Django 1.0 Website Development" have arrived. This is the second edition of my Django book. Django is a framework for building web applications in Python. This book explains how to assemble Django's features and take advantage of its power to design, develop, and deploy a fully-featured web site.

Django 1.0 Website Development

The new edition has been updated to Django 1.0. The key topics that the reader will learn from the book are:

  • Register users through a user authentication system and manage them efficiently.
  • Restrict user access to certain pages and protect against malicious input.
  • Create tags to allow site visitors to classify, view, and share content easily.
  • Create own administration interface for proper monitoring of the web site.
  • Enhance user interface with AJAX.
  • Enable voting and commenting on content, and display popular content to site visitors.
  • Build user networks; add friend management and invitation features for social networking.
  • Create unit tests to automate the testing of code.

The full table of contents is available.

The book is available in paper and PDF formats at Packt Publishing. It is also available from all major book sellers like Amazon.

Writing the book and revising it have been an enjoyable experience for me. The feeling of accomplishment when my copies arrived is satisfying. I sincerely hope that readers find the book interesting and useful. If you have questions or comments, don't hesitate to email me!

More photos of the book are available at my Picasa web albums.

Firefox Summit 2008, Day 2

The main news piece for day 2 in the Firefox Summit 2008 is that everyone is now trapped in the small town of Whistler after that a rock slide cut off the highway that connects Whistler with Vancouver. Fortunately, nobody was injured because of this. However, clearing the massive boulders that are blocking the highway will take 5 days according to official sources. Since the summit ends this Thursday, most attendants need to go to the Vancouver Airport on Friday to catch flights to their home countries. The cause of this rock slide is unclear at the moment, but there are people in the summit who are speculating whether a company whose name starts with an 'M' is behind all of this. A bug was filed in Bugzilla to track the issue, and some of the currently-proposed solutions involve riding bears, taking boats, or taking helicopters. In reality however, we will most likely end up going through a different route that takes around 8 hours in a bus.

Read more »