james mckay dot net
because there are few things that are less logical than business logic

Code syntax highlighting in WordPress – take two

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.

2 comments:

  • # Reply from Sam McGeown at 11:51 on 12 May 2008

    Fancy writing a C# version for BlogEngine.Net?

  • # Reply from Onur Safak at 10:44 on 15 Jul 2008

    Hi,

    Please check the xhtml mod for syntaxhighlighter, which uses the CSS tags instead of name tags, this way it frees name tags, conforms to xhtml, prevents having duplicate names and allows space seperated class values. Supporting xhtml, it also can be used with blogger.com

    The mod can be found at :
    http://blog.onursafak.com/2008/07/syntaxhighlighter-mod-to-xhtml-css-and.html

Comments are closed.