Xah Lee, 2009-06-20
This page gives some examples using Google-code-prettify technology, and evaluates its quality.
Google-code-prettify (GCP) is a javascript library that syntax color computer lang source code in html. It does the job on the fly. Project page at: http://code.google.com/p/google-code-prettify/.
It is very easy to use. All you have to do is to download the javascript files. Then, in your web page, add these lines:
<link rel="stylesheet" type="text/css" href="gcp/prettify.css"> <script type="text/javascript" src="gcp/prettify.js"></script> <body onload="prettyPrint()">
Then, in places you want your source code to be colored, wrap it with a “pre” tag like this:
<pre class="prettyprint"> x = 1+1; # something something </pre>
Advantages:
Disadvantages:
Here are some examples with different languages. For comparison, each example has a version using html span, done by htmlize elisp package with emacs.
Java:
Python:
Perl:
Emacs Lisp:
Google-code-prettify is suitable for small number of lines or for non-critical writing such as on wiki or blogs. It is probably not the right tool for any sizable need for publishing source code, such as online documentation of a computer language or a book length tutorial.
For code over 2 hundred lines, it also takes a second to load. In comparison, bulky html with span wraps that has more coloring and correct syntax is still instaneous.
Mike Samuel (author of Google-Code-Prettify) offers this advice on avoiding the need to first change “< > &” chars.
Related essays: