Go Geshi Function
April 22, 2011| Tweet |
In "industrial" terms, this is considered a recipe, because it contains all the ingredients to a successful cooking show (I mean production).
To get started, you're going to need to download GeSHi from SourceForge (I used version 1.0.8.10 when I developed this).
And you may find it beneficial to also view the author's GeSHi homepage, which contains a great deal of documentation and examples if you choose not to use this function.
Why the Go GeSHi function?
- Automatic creation of .src files, and addition of the link to the box for easier code downloads.
- The .src file automatically detects when you put up a new version and creates the new file then.
- Makes use a bit easier, instead of typing all the OOP components and including the file many times, this is preset to use PHP (though you can change that), and is very simple to use. Works great on sites where all the code is in one language.
- Word-wraps the text that extends beyond your desired width (700px by default) for all pre-tags
- Makes the links to PHP (or other) function documentation open in a new tab, and default to no-follow (to preserve page rank).
Configuring
There's not too much to configure by default, just a few variables:
- $language - The programming language the source is in (for example: PHP) There's a list on the GeSHi homepage of supported languages.
- $path_to_geshi_languages - If you copied the GeSHi folder into your sites directory, there's another GeSHi folder inside of that where all the language packs are at. Adjust if needed.
- $generate_downloads - TRUE to have auto-generation of downloads, and the link shown... FALSE to disable.
- $width_in_px - how wide you want the code area to be.
One last note...
You must pass the source in BASE 64 ENCODING! I assume this makes it easier to store in the source of your other files without all kinds of escaping.
To make it easier, I put up a Base64 Online Encoder which will take care of that for you.
How to use the code
Way too easy...
The Code
(Which just happens to show this function in action!)
Download Original
{ function go_geshi($source) { //Configuration $language = "PHP"; $path_to_geshi_languages = "geshi/geshi"; $generate_downloads = TRUE; $width_in_px = 700; if ($generate_downloads) { } } echo "<style type='text/css'>.geshi-rl-box>pre{white-space:pre-wrap;white-space:-moz-pre-wrap!important;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;width:{$width_in_px}px}</style>"; require_once('geshi/geshi.php'); $geshi = new GeSHi($source, $language, $path_to_geshi_languages); $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS,2); $geshi->set_link_target("_blank\" rel=\"nofollow"); echo "<div class='centeredbox geshi-rl-box'>"; echo $geshi->parse_code() . "</div>"; } }
No comments yet! Be the first!

Facebook
LinkedIn