Syntax Highlighting with Enscript in WordPress

One thing that I dislike about WordPress is its default wptexturize filter that messes up with my HTML code. The default behaviour makes code listing very difficult to read, as it attempts to add paragraph tags to <pre /> tags. Here is one of my first attempts in WordPress plugin coding to rectify this issue.

A plugin needs to be written, that

  • Fixes up the mess wptexturize has left after paragraphing the contents inside <pre /> tag.
  • Optionally add syntax highlighting using GNU Enscript, a method that I have previously discussed.
  • Can be used as a module outside WordPress to provide syntax highlighting to source files or code pieces.

Result is an updated version syntax highlighter PHP module.


To use it with WordPress 1.2/1.5.

  • Save the PHP source file into wp-content/plugins/ directory.
  • Go to "Plugin Management" page under your WordPress admin interface.
  • Activate plugin "Syntax Highlighter Enscript".

Inside your HTML code, all your <pre /> tag should now be properly formatted. If you wish to add syntax highlighting to a piece of code, you will need to add lang="..." attribute to your <pre /> tag. For example,

<pre lang="python">
if __name__ == '__main__':
    print "Hello Word"

The final HTML rendering would be:

if __name__ == '__main__':
    print "Hello Word"

"lang" attribute would need to be supported by GNU Enscript backend. Please check what languages Enscript can support.

To use it outside the context of WordPress, directly as a PHP module, you can call:

$code = 'int main(void) { printf("Hello World\n"); }';
SyntaxHilighter::hilight($code, "c");

To highlight a file on the file system:

SyntaxHilighter::hilght_file('', 'perl');

There are still some unresolved problems:

  • Not everyone can run enscript, especially if you are jailed inside PHP safe mode. Well, too bad. Find another host :)
  • Line numbering - it should be my next todo item.
  • Backslashes are stripped after Save, which makes code listing a challenge. Besides escaping characters like '<', '>' and '&', I also need to escape backslashes to '&#92;'. Looks like a WordPress bug to me.