GeSHi - Generic Syntax Highlighter
Previous article Next articleAdd the Generic Syntax Highlighter (GeSHi) to the frontend of your website.
The idea for this article originates from the former CMS Made Simple Wiki and the original author was tsw.
How to use
1. Create a file named block.cms_geshi.php with the following content:
/**
* GeSHi - Generic Syntax Highlighter
* -------------------------------------------------------------------
* File: block.cms_geshi.php
* Type: block
* Purpose: highlight a block of text using GeSHi
* https://www.cmscanbesimple.org/blog/geshi-generic-syntax-highlighter
* -------------------------------------------------------------------
*/
function smarty_block_cms_geshi($params, $content, &$smarty)
{
if (isset($content))
{
include_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cms_geshi' . DIRECTORY_SEPARATOR . 'geshi.php');
$lang = isset($params['lang']) ? $params['lang'] : 'php';
$styles = isset($params['styles']) ? $params['styles'] : '';
$geshi = new GeSHi($content, $lang, '', true);
$uid = get_userid(FALSE);
if ( !empty ($uid) ) echo $geshi->error();
// Show title in header
$title = isset($params['title']) ? $params['title'] : '';
if (!empty($title)) { $geshi->set_header_content($title); }
// Show linenumbers
$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
// Show CSS classes
$geshi->enable_classes(true);
// Show text indent, etc.
$geshi->set_header_type(GESHI_HEADER_DIV);
// Show external links to PHP documentation
$geshi->enable_keyword_links(false);
// Set target to external links
$geshi->set_link_target('_blank', $styles);
echo $geshi->parse_code();
}
}
?>
2. Download the GeSHi files and unzip the archive.
3. Create a new folder in the core plugins directory named cms_geshi and upload the GeSHi folders and files in it.
4. Move the created block.cms_geshi.php file to the plugins folder.
.../assets/plugins/block.cms_geshi.php
/assets/plugins.../cms_geshi/contrib/...
/assets/plugins/cms_geshi.../docs/...
/assets/plugins/cms_geshi.../geshi/...
/assets/plugins/cms_geshi.../geshi.php
5. Copy the GeSHi stylesheet content into a new CMSMS stylesheet and attach it to the template.
Refer to GeSHi documentation on GeSHi's capabilities GeSHi doc.
Now you can have highlighted code by calling this block from your page with:
<?php
echo 'foo bar';
?>
{/cms_geshi}
<?php
if ($test == true) { echo 1; }
?>
{/literal}{/cms_geshi}
<?php
if ($test == true) { echo 1; }
?>
{/literal}{/cms_geshi}
Note that you need {literal}{/literal} tags around your code when braces are used inside.
Working example
Just look above, I use it here at the website! :)
Comment Form
ReviewManager
ReviewManager
7 Comments
Hi, Rolf,
5. Copy the GeSHi stylesheet content into a new CMSMS stylesheet and attach it to the template. could you descripe here in details for cms madesimple 2.2.8?
thank you very much
The GesHi syntax highlighter is only for the frontend of the website...
I know JoMorg is working on updating AceEditor! Don't know the current status.
Grtz. Rolf
Hi, Rolf,
I use CMSms 2.0 – and I am desperately missing AceEditor, Template Externalizer or at least a syntax highlighter. Now I found GeSHi here on your website. I am not an expert in programming – but I fear, GeSHi can only be usd in the frontend?
Or am I – hopefully – mistaken and I can apply GeSHi in the backend, too?
Thanks for supporting CMSms and for some information!
Reinhard
I have re-tested the latest GeSHi features and have rewritten the blog article.
http://sourceforge.net/projects/geshi/files/
Update: With the upgrade to CMSMS 1.11.10, including Smarty 3.1.16 the Smarty plugins folder moved... You will get the error: unknow tag "cms_geshi".
To fix this move the block.cms_geshi.php file and the complete geshi folder at your webserver from lib/smarty/plugins/ to lib/smarty/libs/plugins/
Hi. I followed the steps you indicated, but could you tell me where to find the Geshi css file?
Thank you.