GeSHi Code Highlighter

Welcome to the page for my "GeSHi Code Highlighter" script. First I want to say that I was having a hard time coming up with a name for this script that accurately described what it does but was also understandable and attractive. "GeSHi wrapper" would probably be a more accurate title, but doesn't provide any information on what it actually does to people who aren't familiar with GeSHi (if that's you, I'm getting there. ;)). So I settled on "GeSHi Code Higlighter" — something that is attractive and will look good in search results and isn't too inaccurate.

If you're bored already, here's the straight dope: This script is a wrapper for the GeSHi code/syntax highlighting class that will take an input file, highlight any code within <pre> tags, and generate an output file - all automatically (and efficiently) and right on your server. If you think you know what you're doing, you can jump straight to the Download and then Instructions.

If you're interested, let's continue and I'll try to explain exactly how this works. First, if you're not familiar with GeSHi, go check it out. It stands for Generic Syntax Highlighter, and it's a set of PHP files that can be used to create syntax highlighting for over 100 languages. It's a great library/class/whatever you want to call it. It's very flexible and does it's job of highlighting code very well. However, the problem is that you have to feed it a specific block of code, something like highlight("<b>My code</b>");, but more complicated with quite a few more lines. That's fine in some cases, but it can get annoying or impractical if you have multiple blocks of code per page or just want a centralized, easy, set-and-forget highlighting system that will highlight code on your pages. It's also very CPU costly because it's highlighting the code on every visit. The alternative here is to run it through once and save the HTML that is produced, but again, that's a bit of work if you have lots of content or that content changes often.

Ok, so what does this do?


This script will take an input page, search the page for code in <pre> tags (with a special format), highlight that code, and then output that highlighted page. The best part is that it only generates a new output page when it detects that the input page has been changed, so that you're not wasting valuable CPU time highlighting the code every time someone visits the page.

All you have to do is upload the file that you want to be highlighted with the code in <pre> tags like this:

<p>Look, here's some code:</p>
<pre lang="cpp" lines="2,6">
#include 
using namespace std;

int main ()
{
  cout << "Hello World!";
  return 0;
}
</pre>
<p>And that's how to write a "Hello World!" program in C++.</p>

The lang attribute is obviously for the language of that specific block of code (more than 100 are available, since we're using GeSHi here), and the lines attribute is for "extra" highlighting specific lines of code if you want (it's optional, obviously). The example in the box above will look like this when highlighted (using my default CSS file):

1
23
45
67
8
#include 
using namespace std; 
int main (){
  cout << "Hello World!";  return 0;
}

Of course the look and feel is completely customizable using CSS and other GeSHi options - just like you would do if you were using GeSHi any other way.

Details

Here's how it works: basically, you have two files, input.html and index.php. You direct everybody to index.php, while the page and content you want to be highlighted is uploaded as input.html. When index.php is run, it determines if input.html has been changed from the last known version. If it has, or if no old version exists, it runs the highlighting sequence, generating another file, generated.html. From here on out, each time a visitor visits the page the script checks whether the input file has been changed — if it has, it generates a new file, if it hasn't, it simply includes generated.html. Simple and efficient. The highlighting code will only run if it needs to.

Where/how can I use this?


In it's current form, this script will only work right out of the box on a per-file basis. This means that you setup the script and provide an input file for each page that you want highlighted.

However, the script is licensed under the GPL and you are free to rip it apart, reuse code, modify it, etc. if you want to get it working with a CMS, blog, forum, or whatever you may need. The "searching" and highlighting code could easily be re-used in a different environment or structure.

Download

For your convenience I have two versions of the script: one with GeSHi and one without. When new versions of GeSHi are released I will try to update the package, but I would always recommend checking the GeSHi website for the latest stable version. If there is a newer version there than what I have included, download the non-GeSHi package and then grab your own copy of the latest GeSHi.

System requirements:

highlighter1.0.zip 41KB [GPLv3]
highlighter1.0+GeSHi1.0.8.4.zip 925KB

Basic Usage Instructions

For the full documentation, please click here.

First, if you haven't already downloaded and extracted the script to a folder on your server, do that now. If you plan on using the script to highlight more than one page, I would extract it into a dedicated folder that you will use as your one location for the script files. Otherwise, extract it into the folder that contains the file you want to highlight.

If you want to jump write in, open up example.php and example_input.html. Here, example.php is the "public" file that you link to, etc., while example_input.html is the input file that contains the content to be processed. To customize this for your own usage, change the values in example.php. The only values you need to change (or ensure they are correct) are $input_file (the input file), $generated_file (the file that will be generated — can really be named whatever you want), and $geshi_path (the path to the GeSHi installation. Specifically, the path to the folder that holds geshi.php, with a trailing slash). $geshi_path may already be correct if you downloaded the GeSHi package and didn't change anything, put double-check it anyway, because it is crucial.

Quick note on highlight.php vs. highlight_nocomments.php: For your convenience and understanding (especially for those of who less familiar with PHP), I've heavily commented the main highlighting file so that you can try to understand what's going on if you want to take a look at it. However, there's so much commenting it's a bit hard on the eyes and can make finding and tracking the actual code difficult. It could also slow down performance by a millisecond or two. So, for PHP vets who just want to look at the code or for performance freaks, I offer you highlight_nocomments.php, with absolutely no commenting whatsoever. Bottom line: the files are identical except for the fact that _nocomments has no commenting. :)

From here, simply insert the code you want to be highlighted in <pre> tags in your input page, as demonstrated in example_input.html, and then link the geshi.css file or your own stylesheet in the head of the page like this: <link href="geshi.css" rel="stylesheet" type="text/css" />

Once that is done, make sure everything is uploaded (including GeSHi), and browse to the PHP page (example.php, in our case.) You should see the content from your input page, with all the code that you put in <pre> blocks highlighted beatifully!

If you're having problems, have questions about how to do something or how something works, or want to read more, please refer to the full Documentation page.

Contact Me

As usual you can contact me using my main contact form. :) I would be more than pleased to help you if you're having problems with a specific setup or if you've found any bugs in the script. :)

Back to Erikswan.net