Articles tagged in syntax hilite

  1. PHP Syntax Hilighting 1.3

    I've just got the chance to start visiting my old WordPress plugins after upgrading this site to WP 1.5. Syntax highlighting with Enscript is broken, so I've made some changes to get it going again. Here's a list of changes:

    • Detect whether we are running under WP 1.5+ to pick the unmangling routine. WP1.5 double quote fix suggested by Oliver.
    • Eliminate the "Generated by GNU Enscript" text (suggested by Alex), which was caused by a regular expression bug.

    Also I found with WP 1.5, you no longer needs to escape backslashes '\' to \.

  2. 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,

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

    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('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.
  3. Source Code Syntax Highlighting in PHP

    They said programming is an art, and syntax highlighting of source code really makes them look artistic. I have some little scripts that I would to publish on the net, and I would like to add some colour before posting in my blog. PHP has this function show_source() that can …