Code syntax highlighting in WordPress - take two
Posted at 12:41 on 05 May 2008
It turns out that the WordPress Syntax Highlighter plugin that I mentioned a couple of weeks ago has some rather nasty artifacts. One in particular is that when you try to display HTML -- or any code containing HTML -- WordPress tries to "fix" this by adding extra tags that you may not want to make it valid XHTML, and it b0rks your nice tidily formatted source code in the process. Another problem was that it converted emoticons to images -- a big no-no when you're writing source code.
Yeah, maybe I should have been using the rich text editor, but that caused other problems. Rich text editors generally wreak havoc with source code, so it's best to turn them off when you're doing anything of that nature.
This was the reason why I stuck with Code Auto Escape for so long. Entering source code is awkward, to be sure, but nevertheless the plugin is pretty robust and does a good job. I did try a few proper syntax highlighter plugins way back, but I didn't find any of them all that satisfactory. However, Code Auto Escape is a plugin that Just Works™.
It also turns out that Alex Gorbatchev's Syntax Highlighter JavaScript code allows you to set various options for your code blocks, such as hiding the toolbar or the line numbers, or starting numbering at a number other than 1. The WordPress Syntax Highlighter plugin does not expose these options.
Sooo... why not combine the two approaches?
Over the Bank Holiday I've spent a few hours writing a new plugin that does precisely that. I took Code Auto Escape as the baseline, and added a whole bunch of extra code to plug in the syntax highlighter scripts.
Interested? Get Coder 1.0 alpha 1 here.