Hello,
I was reading Commodore64 topic [Plugin] GeSHi Syntax Highlighter -
http://modxcms.com/forums/index.php/topic,3651.0.html and I decided to make some improvements in his plugin.
bll0 in Commodore64 topic wrote:
Is there a way to enable other geshi features with this plugin? Like line-numbers, uppercasing or something like this?
I was thinking about the same features. After some hours I did them. Now <pre> tag may have also parameters. It look like this:
<pre [options]>code</pre>
In options u can write all GeSHi methods that can have one parameter. Maybe small example with one option:
<pre set_language="php">code</pre>
or multiple options:
<pre set_language="java" enable_line_numbers="GESHI_NORMAL_LINE_NUMBERS" set_header_type="GESHI_HEADER_PRE_VALID">code</pre>
Installation and more examples you can see here:
http://q3e.pl/tutorials/modx-geshi-plugin.html
Code for plugin:
/************************************
*
* GeSHi highlighter Plugin
* for MODx v.1.0.0 (modxcms.com)
*
* Written by AnIHiL
* Created: 26 July 2009
* Last updated: 26 July 2009
* GeSHi version: 1.0.7.7
*
* Version: 1.0
*
* Events: OnLoadWebDocument
*
* Contact: [email protected]
*
************************************
* Created from: GeSHi Syntax Highlighter 0.4
* Author: Commodore64
* Link: http://modxcms.com/extras/package/167
* Forum link: http://modxcms.com/forums/index.php/topic,3651.0.html
************************************/
global $geshi_settings;
$geshi_settings = array();
function callback2($matches) {
if (($matches[1] == '') || ($matches[2] == '')) return 0;
global $geshi_settings;
$geshi_settings = array_merge($geshi_settings, array($matches[1] => $matches[2]));
}
function callback($matches) {
preg_replace_callback('#[\n\t\r\f ]*([a-zA-Z][a-zA-Z0-9_]*?)[\n\t\r\f ]*=[\n\t\r\f ]*"([a-zA-Z0-9_]*?)"[\n\t\r\f ]*#s', 'callback2', $matches[1]);
return geshiCode($matches[2]);
}
function geshiCode($code) {
global $geshi_settings;
$code = substr($code,2);
$path = $modx->config["base_path"].'assets/plugins/geshi/geshi/';
$geshi = new GeSHi($code, 'php', $path);
$geshi->set_header_type(GESHI_HEADER_PRE_TABLE);
$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
$geshi->set_tab_width(4);
foreach ($geshi_settings as $key => $value) {
eval("\$geshi->".$key."(".$value.");");
}
return '<div class="codetop"><div class="codetexttop">'.strtoupper($geshi_settings[set_language]?$geshi_settings[set_language]:'php').' code</div></div><div class="codemain">'.utf8_encode(html_entity_decode($geshi->parse_code())).'</div>';
}
switch ($modx->Event->name) {
case "OnLoadWebDocument":
include_once($modx->config["base_path"].'assets/plugins/geshi/geshi.php');
$modx->documentObject['content']=preg_replace_callback('#<pre(.*?)>[\n]?(.*?)</pre>#s', 'callback', $modx->documentObject['content']);
break;
default:
return;
break;
}
Ps. I’m using FCKEditor on my site and when I insert <pre> all the time after <pre> it put new line and I cannot remove it so I put in plugin: $code = substr($code,2); to remove first line (2 is because its UTF-8 encoding).