Many posts on this blog will include source code, and readers will wish to include source code in their comments; in particular, it is desirable to preserve indentation. But WordPress disables the html <pre> … </pre> tag in comments, making it difficult to include source code. There are at least three methods you can use to include source code in your comments.

First, you can write the code in a pastebin, then use an <a href=”…”> tag in the comment to link to the pastebin. A useful pastebin for program source code is at, which provides source code formatting via the GeSHi open-source code formatter. For instance, you can go to to see a pastebin containing a simple function to compute the greatest common divisor of two numbers.

Another useful pastebin is, written by Steven Hazel, which provides an online compiler/interpreter as well as a pastebin. You write your code in an input form, or copy-and-paste it from your editor, then execute your code by clicking the Submit button. If you save your pastebin, you will be given a permanent web link that can be shared with others by pasting the link into a chatroom, or an email, or a comment on this blog. For example, is a pastebin containing the same gcd function shown above. The supported languages are C, C++, D, Haskell, Lua, Ocaml, PHP, Perl, Python, Ruby, Scheme, and Tcl. Programming Praxis often provides links to code stored at, so that readers can execute the code themselves.

Second, you can use a foo-to-html converter, for your language foo, off-line, on your own computer, then copy-and-paste the converted output into your comment between <code> … </code> tags. That’s less bad than it sounds, especially if you write the code in an editor that provides a macro that can call the foo-to-html converter. Compared to the first alternative, it has the advantage that your code appears directly in the comment. That is the method used by Programming Praxis when posting the “official” solution to the exercises. The gcd function described above looks like this:

(define (gcd m n)
  (cond ((< m n) (gcd m (- n m)))
        ((< n m) (gcd (- m n) n))
        (else m)))

As long as all you want to do is maintain indentation and fix a few punctuation characters that are special to html, it is easy to write a simple, language-agnostic foo-to-html program using sed:

$ cat foo-to-html
#! /bin/sed -f
s/^ /\&nbsp;/
s/\&nbsp; /\&nbsp;\&nbsp;/
t again
$ chmod +x foo-to-html
$ ./foo-to-html
(define (gcd m n)
&nbsp;&nbsp;(cond ((&lt; m n) (gcd m (- n m)))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;((&lt; n m) (gcd (- m n) n))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(else m)))

Third, WordPress provides a sourcecode attribute for use in posts and comments. It looks like this:

[sourcecode lang="foo"]
(define (gcd m n)
  (cond ((< m n) (gcd m (- n m)))
        ((< n m) (gcd (- m n) n))
        (else m)))

Foo must be one of the following supported languages: cpp, csharp, css, delphi, html, java, jscript, php, python, ruby, sql, vb, or xml. The output will look like this:

(define (gcd m n)
  (cond ((< m n) (gcd m (- n m)))
        ((< n m) (gcd (- m n) n))
        (else m)))

That code was formatted with the lang=’css’ parameter, which works reasonably well for Scheme. The sourcecode attribute is documented at This is a good choice for supported languages, and may also be useful for other languages, as in the example.

By the way, the gcd function described above is Euclid’s algorithm for finding the greatest common divisor of two numbers m and n. Euclid lived about 300 B.C., and this algorithm is the oldest algorithm still in use today, predating even the grade-school algorithms for simple arithmetic. The computer scientist Donald Knuth, in his book The Art of Computer Programming, describes this algorithm as the “grand-daddy” of all algorithms.


Get every new post delivered to your Inbox.

Join 729 other followers

%d bloggers like this: