Ajax Perl syntax highlighting

Banish boring code blocks!

Download

Description
This script provides an easy way to implement syntax highlighting for Perl code examples on your website, i.e. to convert this:

use strict;
use warnings;

my $greeting = "Hello";
print "$greeting, World!\n";

into this:

use strict;
use warnings;

my $greeting = "Hello";
print "$greeting, World!\n";

How it works
After your web page has loaded, a JavaScript function searches your document for tags containing Perl source code. These are sent to a CGI script which performs the syntax highlighting (using Perl::Tidy), then the JavaScript dynamically replaces the original content with the highlighted version.

How to use it
First, install the Perl::Tidy module onto your server. Then download the AjaxPerlSyntax archive and copy the JavaScript, CSS, and CGI script files to the appropriate locations on your web server.

The JavaScript file needs to be configured with the URL to the perlsyntax.pl script - to do this, edit PerlSyntax.js and change the highlightUrl value in the configuration section, e.g:

PerlSyntax.highlightUrl = 'http://yoursite/cgi-bin/perlsyntax.pl';

You'll also need to configure the script to tell it what HTML tags in your web pages contain Perl source code. By default, it looks for <pre> tags which have a class name of perlcode. If you're using something different to this, edit the PerlSyntax.js file and change the following lines in the configuration section:

PerlSyntax.elementType  = 'pre';
PerlSyntax.className    = 'perlcode';

If you want all <pre> sections to be highlighted, irrespective of class, change the className value to ''.

PerlSyntax.className    = '';

When this has been set up, add the following lines to the <head> section of your web pages:

<script src="OpenThought2.js"></script>
<script src="PerlSyntax.js"></script>
<link href="perlsyntax.css" rel="stylesheet" rev="stylesheet" 
type="text/css" media="screen" />

and put an onLoad() handler in your <body> tag to call the highlighting script when the page has been loaded, e.g.

<body onLoad="PerlSyntax.highlight()">

Todo

  • Make highlighted keyword and module names into links to the appropriate pages at Perldoc and CPAN.
  • Include a cache on the server side so that Perl::Tidy is not called for every request.

Copyright and license
This software is Copyright © 2007 Jon Allen, and licensed under the terms of the Artistic License version 2.0.

The AjaxPerlSyntax distribution archive includes components from the OpenThought Ajax library, which is Copyright © Eric Andreychek and licensed under the terms of the GNU General Public License.