LWN.net Logo

Pygments - the Python Syntax Highlighter

By Forrest Cook
May 7, 2008

Pygments is a multi-language syntax highlighter that is written in Python and distributed under the BSD license. The project description states:

It is a generic syntax highlighter for general use in all kinds of software such as forum systems, wikis or other applications that need to prettify source code. Highlights are:
  • a wide range of common languages and markup formats is supported
  • special attention is paid to details that increase highlighting quality
  • support for new languages and formats are added easily; most languages use a simple regex-based lexing mechanism
  • a number of output formats is available, among them HTML, RTF, LaTeX and ANSI sequences
  • it is usable as a command-line tool and as a library
  • ... and it highlights even Brainf*ck!
[Pygments] The project FAQ notes that Pygments supports a long (and expandable) collection of input languages. It can produce output as HTML, LaTeX, RTF and ANSI sequences for console output. The software can be run from the pygmentize command-line tool, or accessed from your own Python code. See the command line reference for details on running pygmentize.

Pygments version 0.10 was recently announced. Changes include the addition of 15 new language lexers, expansion of the Makefile lexer's capabilities, the ability to output in several image formats, a new style and other enhancements and fixes.

Installation of Pygments was straightforward on an Ubuntu 7.04 system. A tar.gz file was downloaded from the Python package site. The file was uncompressed with gunzip and extracted with tar. Running python setup.py install as root on the setup script installed the software and it was ready to run. After a quick read of the Command Line Usage document, your author was able to run pygmentize on some Python code and produce some rather pleasing colorized html output.

The project's demo page has a number of examples of Pygment's output, it also allows you to upload your own code to see how it looks after formatting.

Pygments looks to be a well designed generic tool. It could useful for online and offline documentation, code analysis, education and much more. This list of projects is already putting Pygments to use, perhaps your project could make use of it as well.


(Log in to post comments)

Another syntax highlighter?

Posted May 8, 2008 11:38 UTC (Thu) by epa (subscriber, #39769) [Link]

There must be about a hundred generic syntax highlighters out there, what makes this one
special?

Another syntax highlighter?

Posted May 8, 2008 14:33 UTC (Thu) by jordanb (subscriber, #45668) [Link]

The logo? :P

Seems like there's a particularly large deluge of this stuff for python these days. Maybe the
Python community is finally collapsing in on itself, like when the Lisp community decided in
the 90s that their only collective output from then on was going to be new dialects of lisp. 

From here on out, it's all code review tools.

Another syntax highlighter?

Posted May 8, 2008 14:55 UTC (Thu) by marduk (subscriber, #3831) [Link]

Arguably one could say the same about X and window managers/toolkits in the 90s... or Linux
and distributions since... forever.

Another syntax highlighter?

Posted May 8, 2008 22:30 UTC (Thu) by sepreece (subscriber, #19270) [Link]

Does anyone know of an implementation of the prettyprinting style described in M. Baecker and
A. Marcus, Human Factors in Typography for More Readable Programs? They demonstrated improved
readability for code that was heavily reformatted using font style and size shifts, background
variations, moving he comments to the outside of the page, etc. I once built a tool on top of
vgrind to generate HTML for C code in an approximation of their format, but gave up because
the preprocessor makes so many things impossible...


Pygments - the Python Syntax Highlighter

Posted May 9, 2008 18:42 UTC (Fri) by copsewood (subscriber, #199) [Link]

Just installed it, ran and tested it. Took about 5 minutes and it does what it says on the
tin. I'd been looking for something as convenient as this for the purpose which could generate
output in html format.

Pygments - the Python Syntax Highlighter

Posted May 16, 2008 14:15 UTC (Fri) by Jaffa (guest, #4327) [Link]

So, as an existing user of enscript - which is just an apt-get away - what does it give me?

Copyright © 2008, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds