kelvinluck.com

a stroke of luck

Plugin to add GeSHi syntax highlighting to Textpattern – Part II


Update 2:

This is a very old page imported from my previous blog. If there is missing content below or anything that doesn’t make sense then please check the page on my old blog.

Update:

see the krl_geshiSyntaxHighlight project page for more information.

…continued from yesterdays post.

EDIT: Please note that the code samples in this look better than they did when I wrote it because I have since updated the engine which generates them (as you will see if you read my other entries) – so when I talk about quotes and underscores and the like being changed into wierd HTML entities it was true!

OK – thanks to some people on the textpattern forums I’ve managed to half fix the problem from yesterday. By placing <notextile> tags around the code in my example I now have the quotes not being changed into strange HTML entities. As you can see here:

  1. function GeSHi ($source, $language, $path = '')
  2. {
  3.   if ($path == '') {
  4.     $path = dirname( __FILE__ ).'/geshi/';
  5.   }

Unfortunately the FILE is still getting translated into first italic tags and then &lt;i&gt;FILE&lt;/i&gt;.

While I was wondering if there was a way to avoid this I came across Johan Nilsson’s glx_code plugin…

How comes I didn’t find it yesterday when I was looking for a way to highlight my code? Oh well – I’ve gone far enough along my path now that I need to complete it! And the way that the glx_code plugin works gave me an idea. It avoids any issues with textile by loading the file with the code sample in rather than by having it included inside the page (as my approach did). So I thought I would add the option to do this to my plugin. And here is the result marking up the above bit of code:

  1. function GeSHi ($source, $language, $path = '')
  2. {
  3.   if ($path == '') {
  4.     $path = dirname( __FILE__ ).'/geshi/';
  5. }

Sweet – that seems to work nicely :D I’m not sure it is a perfect solution because it requires creating a file on my server for each code snippet I want to highlight but it is at least functional. I’ll leave it like that for now and see if I can find a way to get the contents of the tag without textile getting it’s grubby mits on it first!

And I’ll be back soon with another installment which will hopefully contain the final plugin and instructions for it’s use…



Plugin to add GeSHi syntax highlighting to Textpattern


Update 2:

This is a very old page imported from my previous blog. If there is missing content below or anything that doesn’t make sense then please check the page on my old blog.

Update:

see the krl_geshiSyntaxHighlight project page for more information.

So I have this new blog thing and it occured to me that one thing I would want to do with it is to post code I’ve written. I had a look around and it didn’t seem like there was a plugin available to do syntax highlighting for code posted to Textpattern. So I thought that it would be a good quick project for me to get to know abit more about Textpattern to try and add the syntax highlighting capabilities of GeSHi to it.

And thats exactly what I’ve done! It’s functional but not quite perfect at the moment and it’s past my bedtime so this is as far as I got…

First download GeSHi 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/';
}

D’oh! Just looking at that I have noticed that there is a problem with where htmlentities is called within GeSHi. As you can see, the quote marks have got all messed up in the code above. So I’m going to have to leave it for the moment I think and return to getting this polished along with providing the plugin tomorrow…