Fixing code listings

published: Thu, 13-Mar-2008   |   updated: Thu, 13-Mar-2008
Red vine trees

Dustin Campbell manages to display source code in his blog posts with the Visual Studio syntax highlighting. So, go on, how do you do it, I asked. Simple, as any fule kno, he said. When you copy source code with Visual Studio, it creates a data block in rich text format (RTF). Take that clipboard data, parse it, and output HTML. I could see that he felt I wasn't up to writing an RTF parser. Hah! Moi?

So I did. A nice recursive descent parser, like what they taught you at Computer Science school.

It works extremely well and produces code listings that look exactly like they do in my color-tweaked Visual Studio, even down to the yellow highlighter-pen comments I prefer.

I've updated part 1 and part 2 of the red-black tree articles with the new syntax hightlighted listings. In doing so I noticed that in doing it manually last time I'd forgotten to change some generic angle brackets (<) to their entity values (&lt;), so the browser rightfully stripped them. Grrr. Sorry about that.

But now all is well again.

(Now to do all the other code listings on my site...)