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.

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 ‘&#92;’. Looks like a WordPress bug to me.
Category: General | Sat, 29 May 2004 10:40 pm

Links to This Article

  1. Tue, 15 June 2004 12:30 am
    Open Artifact » Syntax Highlighting in WP
  2. Sat, 19 June 2004 10:19 pm
    Truth, Justice, and the American Way » Fun with XML
  3. Tue, 8 June 2004 10:43 pm
    LcF Weblog
  4. Sun, 11 July 2004 10:31 am
    The Silent Machine
  5. Mon, 9 August 2004 3:07 am
    Corelist.Net > Weblog
  6. Fri, 3 June 2005 9:40 pm
    a ZA boy in the UK » Blog Archive » FriendlyCode
  7. Sun, 12 June 2005 4:05 am
    Li-Jie Cheng’s Blog… » Blog Archive » Syntax Highlighting with Enscript in WordPress
  8. Wed, 20 July 2005 5:40 pm
    got netkrash? » Blog Archive » syntax hilight
  9. Thu, 18 August 2005 8:59 am
    Steven's Life
  10. Sun, 11 September 2005 4:10 pm
    Uwe’s weblog » Blog Archive » Test Syntax Highlighting Pluggin
  11. Wed, 14 September 2005 5:23 am
    Gosinga » Blog Archive » PHP 코드에 색을 입히기
  12. Mon, 3 October 2005 8:59 am
    Ramblings in the rain » Embedded Code Test!
  13. Fri, 7 October 2005 7:36 am
    The Hustad Blog » Blog Archive » Hello world!
  14. Fri, 10 February 2006 8:00 pm
    Mikee’s World » Code Markup problem with WordPress
  15. Fri, 17 March 2006 10:39 am
    Goldfish Personal Blog
  16. Sun, 13 April 2008 4:36 pm
    在WordPress文章中显示代码的两个方法 | Dream-追梦工作室
  17. Sun, 7 December 2008 12:23 am
    Copy/paste php code in Wordpress articles | DownloadWin Blog
  18. Sun, 12 April 2009 4:09 pm
    如何在Wordpress文章中显示Php代码 | 网络2.0新闻
  19. Sat, 22 August 2009 6:37 pm
    在WordPress中正确显示PHP代码 « 灰尘空间

Comments

1.
Avatar for ArmenianGuy
Posted by ArmenianGuy on Wed, 7 July 2004 4:34 am

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


2.
Avatar for dodo
Posted by dodo on Tue, 17 August 2004 3:17 pm

is it possible for this to be validate xhtml under php 4?


3.
Avatar for scotty
Posted by scotty on Tue, 17 August 2004 4:28 pm

Validated under XHTML 1.0? It tries to. (Note the latest fix that correct some non-validate tags used by enscript)


4.
Avatar for iiome
Posted by iiome on Tue, 14 September 2004 3:48 pm

download, copied in plugins directory and worked immediatly :)

Thanks


5.
Avatar for Alex Gittens
Posted by Alex Gittens on Thu, 3 February 2005 2:46 pm

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.


6.
Avatar for mlambie
Posted by mlambie on Mon, 7 March 2005 3:03 pm

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.


7.
Avatar for Richard Torkar
Posted by Richard Torkar on Tue, 24 May 2005 11:37 pm

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/


8.
Avatar for Larry
Posted by Larry on Tue, 31 May 2005 2:11 pm

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. :)


9.
Avatar for Unreal Media
Posted by Unreal Media on Sun, 2 November 2008 5:54 am

Is this a running project because line numbering would be great.


Avatar for Costin
Posted by Costin on Sun, 7 December 2008 12:27 am

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)


Avatar for sobakers
Posted by sobakers on Thu, 30 April 2009 7:50 pm

Thanks
cool a plug-in


Avatar for 河边草
Posted by 河边草 on Wed, 10 June 2009 10:37 pm

I just need it , thanks very much.


Avatar for WordPress Templates
Posted by WordPress Templates on Mon, 31 August 2009 4:53 pm

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.