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
wptexturizehas 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.
- Download the latest version: syntax_hilight.php (1.3 updated on 9 March 2005)
Usage
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,
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:
<?php $code = 'int main(void) { printf("Hello World\n"); }'; SyntaxHilighter::hilight($code, "c"); ?>To highlight a file on the file system:
<?php SyntaxHilighter::hilght_file('foobar.pl', '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 ‘\’. Looks like a WordPress bug to me.
Links to This Article
- Open Artifact » Syntax Highlighting in WP
- Truth, Justice, and the American Way » Fun with XML
- LcF Weblog
- The Silent Machine
- Corelist.Net > Weblog
- a ZA boy in the UK » Blog Archive » FriendlyCode
- Li-Jie Cheng’s Blog… » Blog Archive » Syntax Highlighting with Enscript in WordPress
- got netkrash? » Blog Archive » syntax hilight
- Steven's Life
- Uwe’s weblog » Blog Archive » Test Syntax Highlighting Pluggin
- Gosinga » Blog Archive » PHP 코드에 색을 입히기
- Ramblings in the rain » Embedded Code Test!
- The Hustad Blog » Blog Archive » Hello world!
- Mikee’s World » Code Markup problem with WordPress
- Goldfish Personal Blog
- 在WordPress文章中显示代码的两个方法 | Dream-追梦工作室
- Copy/paste php code in Wordpress articles | DownloadWin Blog
- 如何在Wordpress文章中显示Php代码 | 网络2.0新闻
- 在WordPress中正确显示PHP代码 « 灰尘空间
Comments
Validated under XHTML 1.0? It tries to. (Note the latest fix that correct some non-validate tags used by enscript)
Nice plugin. I like the idea, and love the easy installation. But two things you could do would make this an even better plugin: 1) cache the highlighted code, so that enscript doesn’t run each time the entry is visited, 2) eliminate the “Generated by GNU enscript ” footer that I see after code snippets.
For what it’s worth, I find the “:TOhtml” command in vim to be very handy. I then copy the code into pre tags that have a style set with a black background and a system font.
Works beautifully with WP 1.51!
I found a ruby.st syntax file which I placed in /usr/share/enscript/hl/ and now I have nice Ruby source code on my blog :)
If someone wants the ruby syntax file you can get it here:
http://java.thn.htu.se/~toor/blog-ng/2005/05/24/mixin-modules/
It looks nice but it, like codefilter, still escapes double quotes in the code. All of your examples above have a \ before the double quotes. I’m looking for something that DOES NOT DO THIS. :)
I have found a small bug. :-)
I pasted into my blog a php code and there was a line containing the title tag. I need to put a space to appear in the post. You may check it out (the post is: Copy/paste php code in Wordpress articles)
Thank you for your script. I will try to install it on my website. I hope it will still work on the new wordpress :)
Add a comment
Gravatar is used. Email address is required but will not be displayed. Please keep your comment on topic. No spamming and/or bad language. First time poster will be moderated. Scott reserves the right to delete/edit your comments.

I tried that plugin for wordpress but it never ends up working for me =(