krl_geshiSyntaxHighlight
This plug-in will allow you to to add syntax highlighted code to your TXP articles.
You can use the tag in one of two ways:
- <krl_geshiSyntaxHighlight language=”language” file=”filename” />
- <krl_geshiSyntaxHighlight language=”language”><notextile>code to highlight</notextile></krl_geshiSyntaxHighlight>
The first reads the code in from a file, the second reads it in-line in the tag itself. The second is more convenient, especially where you have a number of small code snippets you want to highlight (you don’t need to upload any code for each thing you want to highlight) but has a few problems (mainly that you need to wrap the code in notextile tags).
PARAMETERS
There are a number of optional attributes you can use to pass extra information to the plug-in:
- file – if you want to highlight the contents of a file then pass the filename here [optional – if not present then the contents of the tag is used]
- directory – if you are highlighting the contents of a file then this is the directory that file resides in (from the root of the web server) [optional – defaults to “highlighted_source/”]
- language – one of the languages which can be highlighted by GeSHi [optional – defaults to PHP]
- linenumbers – whether or not to display line numbers along with your highlighted code [optional – defaults to true]
- startLineNumbersAt – if displaying line numbers this is the first line number displayed [optional – defaults to 1]
- width – the width in pixels to display the code div at [optional – defaults to 530]
- height – the height in pixels to display the code div at [optional – defaults to 180]
CHANGELOG
2005-01-29 v0.1 First release
EXAMPLE
Here is the plug-in being used to highlight some of the code which makes the plug-in – how cool is that?
- require_once('geshi.php');
- $geshi = new GeSHi($code_to_highlight, $language);
- //$geshi->enable_classes();
- $geshi->set_url_for_keyword_group(3, ''); // this line prevents GeSHi from turning every call to a built in function into a link to the PHP manual...
- $geshi->set_encoding("ISO-8859-1");
- $geshi->enable_line_numbers($linenumbers);
- $geshi->set_header_type(GESHI_HEADER_DIV);
- if ($linenumbers) {
- $geshi->start_line_numbers_at($start_line_numbers_at);
- }
REQUIREMENTS
If you want to use the plug-in in the “in-line in tag” mode then you may have to hack TXP as I describe here so that the textile engine doesn’t get over enthusiastic in it’s replacement of certain characters.
If you want the code to display in a nice manner you will need to add these style rules to the stylesheet which is loaded wherever you highlight code:
margin: 5px 20px 20px 10px;
}
div.krl_highlighted_code div.header {
margin: 0 0 2px;
font-weight: bold;
}
div.krl_highlighted_code div.codeblock {
margin: 0;
padding: 6px;
border: 1px solid #666;
overflow: auto;
font-size: 1.1em;
}
div.krl_highlighted_code ol {
padding: 0 0 0 40px;
}
You can obviously change the numbers in the styles to fit with your site. The ol rule is important – it seems that IE doesn’t show up the line numbers when you don’t have this rule. The other rules are just to make things look a little nicer…
You will also need to set GeSHi up… Download it from here (I got 1.0.4 – the latest at the time of writing). Then extract it so that the geshi.php file is somewhere in your include path and so that the geshi folder (which contains all the language files) sits at the same level as this file.
Then edit the geshi.php file so that the relevant lines look like this:
- function GeSHi ($source, $language, $path = '')
- {
- if ($path == '') {
- $path = dirname( __FILE__ ).'/geshi/';
- }
This should insure that the plug-in can find it’s language files when you select a language to highlight in.
DOWNLOAD
You can download the plug-in from here:
POSSIBLE IMPROVEMENTS
These are all things that I would like to do to the plug-in to make it more useful – if anyone has any ideas on how to approach them or any other ideas for extensions then please let me know…
- Make it so that the inline code method works without you having to add notextile tags
- Figure out a way to make it work nicely without having to hack TXP
FEEDBACK
Please provide / view any feedback on this plugin at the bottom of this blog entry. If it looks like there is the need I’ll try and set something a bit more permanent up but that should do for now…
