Quick and Dirty GeSHi Output

The Generic Syntax Highlighter is a PHP class that uses CSS formatting and creates valid XHTML of the parsed file. GeSHi is an amazingly powerful tool, but the documentation is anything but user friendly. This probably isn’t an issue for anyone with a solid PHP background, but I’m not that guy. After a whole lot of wailing and gnashing of teeth (to say nothing of ranting and cursing), I managed to get the thing running enough to do what I want. Here’s a rundown. (It presumes that you’ve already managed to get GeSHi installed on your server.)

Static Linking:

The first part of getting everything running is to use PHP to create a GeSHi object before the document <head>:

include_once('./path-to-geshi/geshi.php');

You also need to define a couple of necessary variables. Tell GeSHi where to find itself, and also where its variable file lives:

$path = './path-to-geshi/geshi';

include dirname(__FILE__).'/path-to-geshi/geshi_vars.php';

Once you’ve done this, the next order of business is to feed it something and tell it how you want that thing marked up. There are almost 100 different language styles (in separate files), so chances are you’ll find something suitable.

In the following example, I’ve pointed GeSHi to the site’s main CSS file. I’ve also added a comment at the end of the language line listing the names of the definition files installed on the server.

$source = file_get_contents(dirname(__FILE__).'/css/css-rgo-main.css');

$language = 'css'; // site languages: css html4strict php

To specify any other file on the server, you need only to change the path between the single quotes, and set the language to match the file type. I’ve only installed the languages necessary to markup the code used on this site. If you were using GeSHi in what seems to be its more traditional context – with an interface to select files, their locations and languages – you would obviously want to upload the entire library of language definitions.

Now that the code source has been defined, the only thing that remains is to get GeSHi to mark it up and output it in the page. Put this block in the body of your document:

<div class="css">

<?php

$geshi->set_header_content('css-rgo-main.css');

echo $geshi->parse_code();

?>

</div>

The DIV class has nothing to do with the language selected. It is defined in the GeSHi style sheet that you need to import into the page you’re creating. The file specified in set_header_content is simply the title of the formatted code box. You could also do this by previously defining the file name as a variable.

When you load up the page, voilĂ , there you have some GeSHi output.

All in one block, the code looks like this:

<?php include_once('./path-to-geshi/geshi.php');

$source = file_get_contents(dirname(__FILE__).'/css/css-rgo-main.css');

$language = 'css'; // site languages: css html4strict php

$path = './path-to-geshi/geshi';

include dirname(__FILE__).'/path-to-geshi/geshi_vars.php';

?>

Dynamic Linking:

In order to dynamically send files GeSHi’s way – such as would be necessary to do a view source on any given page – things get a bit more complex.

A different block of code will be used before the <head>:

<?php include_once('./path-to-geshi/geshi.php');

$url = $_SERVER['HTTP_REFERER'];

$file = substr($url,strrpos($url,'/')+1,strlen($url));

ob_start();

include_once($file);

$source = ob_get_contents();

ob_end_clean();

$language = 'html4strict'; // site languages: css html4strict php

$path = './path-to-geshi/geshi';

include dirname(__FILE__).'/path-to-geshi/geshi_vars.php';

?>

There are two new variables: $url, which is the URL of the referring page (fed to the browser when the link was clicked), and $file, which is the file name, parsed off of the complete URL string.

When dealing with local files, you need to use an output buffer in order to capture the entire processed contents of the file (i.e. with all the includes included), otherwise you would wind up with a raw file containing only references to the includes. In English, the sequence of commands is basically: create a buffer, read the desired file into the buffer, stuff the contents of that file into the $source variable that GeSHi is looking for, and then clear the buffer. Everything neat and tidy.

The only additional change necessary has to do with the code in the page body that displays the file name. Since this is no longer static, we’ll use the same variable we used to define the file. For sake of comparison, the previous version from the static example is also shown:

Static : $geshi->set_header_content('css-rgo-main.css');

Dynamic: $geshi->set_header_content($file);

You can see both the static and dynamic view source examples – main style sheet and page source, respectively – by clicking the icons in the sidebar at the top of the page.