Use GeShi syntax highlight with Kirby

I have been looking for a while for a CMS that is simple and allows syntax highlight. For a local knowledge base.
Kirby seems to be a good candidate.

But I can not seem to make the highlight plugin work.

I know the plugin is loading, I tested with a print() statement. But it does not do ANY highlight, just show the standard code block.

What is the “secret” of activating GeSHI ?

What exactly have you done? Could you post your code?

I didn’t do anything special…

I am testing on a Windows UniformServer (standard WAMP thing)

Downloaded Kirby-2.1.1
On top of that I copied kirbycms-knowledge-base-master from 6wunderkinder (https://github.com/christianreber/kirbycms-knowledge-base)

I then changed a section of the text to be some code. Followed standard syntax for code:

```php

<? print "hello world!";?>

```

But only highlighted as code, not syntax highlighted.

I then tried to copy over a default GeSHI installation (to get more languages). Nothing.

I further tested with other plugins, the columns… but again, not parsed.

Wondor if my local WAMP is to blame ?

I don’t think it works like this. I copied this example bit from the GeShi documentation

<?php
//
// Include the GeSHi library
//
include_once 'geshi.php';
 
//
// Define some source to highlight, a language to use
// and the path to the language files
//
 
$source = '$foo = 45;
for ( $i = 1; $i < $foo; $i++ )
{
  echo "$foo\n";
  --$foo;
}';
$language = 'php';
 
//
// Create a GeSHi object
//
 
$geshi = new GeSHi($source, $language);
 
//
// And echo the result!
//
echo $geshi->parse_code();
?>

into a template and added the geshi.php file and the geshi folder into /site/plugins and it works perfectly.

Since this is a php class, you need to call it somewhere.

BTW. I use prism.js and so does the Kirby website. You may want to check that out instead? http://prismjs.com

Keep in mind that you probably have to update the knowledge base template, it was made for Kirby 1 and has never been updated to work with Kirby 2, so you might run into errors.

OK, that is clear.
I would prefer GeSHI because it has a language that prismjs does not.

I understand the use in a template. But what do I need to change to have code replaced (´´´php… ´´´) with syntax highlight ? I kind of need to call $geshi->parse_code(); for the text in “fenced code”. Not assuming that is automatic ?

And is there a way to change the string for code, it’s difficult for me to type the back ticks

You could use a Kirbytext pre filter: http://getkirby.com/docs/advanced/kirbytext/#kirbytext-filter.

Extract the text items and the language between the backticks using regex and parse them with geshi.

Another option: A custom Kirbytext tag, have a look at the columns plugin as a basis for ideas: http://getkirby-plugins.com/columns. The you would not need to enter backticks either.

The code embed plugin might be a starting point as well: https://github.com/fanningert/kirbycms-extension-code

Something like this has already been done: https://gist.github.com/jancbeck/73a906af92feb37a2ed0
I have not tested this though, but it might be a good starting point.

@lukasbestle thanks, that got me a long way.
I found, maybe helpful to others, that to actually color highlight the syntax, I needed to load the CSS in the template. This CSS can be generated from the GeSHi package by calling a PHP script. Needed to comment out a .text class.

I am 99% there. But what I do not understand is the difference between this:

**This works **

 Source text: /spaces are here for the forum only../
 ` ` `php
 <? print("this is nice!"); ?>
` ` `

 And highlighter: 
 return preg_replace_callback('!```(.*?)```!is', function($code){

This is not interpreted and outputs as normal text

  ¤¤¤php
 <? print("this is nice!"); ?>
  ¤¤¤

 And highlighter: 
 return preg_replace_callback('!¤¤¤(.*?)¤¤¤!is', function($code){

As if the kirbytext is only interpreted if I use back ticks.

I can’t really find the place that can tell the difference. This is my last step

Thanks

Generally this should work just as well. But is that the currency sign found on scandinavian keyboards? Maybe that could be an encoding issue. Have you tried using a different character?

1 Like

MAAAN do I feel stupid! I work with these things every day. And I am trapped by a dumb encoding issue. One of the files (highlight.php) was ANSI, the source UTF-8. Once that was fixed, it worked.

So, I now have something that works. GREAT.

For the people that read this, you may want to have a look at GeSHi documentation. With a few changes you can have line numbers and other goodies.

Something like:

$geshi->set_header_type(GESHI_HEADER_PRE_TABLE);
$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
$geshi->enable_classes();
$geshi->enable_keyword_links(false);
$geshi->set_header_content(“”.$lang.“”);

Again, THANK YOU for your help!

Awesome! You are welcome. :smiley: