We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1703
    • 4 Posts
    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).
      • 26931
      • 2,314 Posts
      thanks for sharing!

      ...will it be possible to display MODx snippet-calls within, and not parsing them?
        • 1703
        • 4 Posts
        You mean to create snippet like bIIo did?
        [[GeSHi?&file=`source/myFile.php` &lang=`php`]]

        Maybe write example how you think it should work smiley
          • 26931
          • 2,314 Posts
          no, i mean your plugin will be used to display code on the frontend and highlight it, right?

          MODx parses code within <code> and <pre> tags, so if you want to display MODx snippet calls (for example MODx tutorials) they will be parsed, have a look at this thread -> http://modxcms.com/forums/index.php/topic,37787.msg228151.html

          j

            • 1703
            • 4 Posts
            I was testing <code> and <pre> tags how they behaviour. When I’m using <code> than <pre> or <pre [options]> its working good for me. <code> has only CSS stylesheet that I can modify. It’s in file assets/templates/modxhost/modx.css
            /* Code
            ------------------------------------------------------------------------- */    
            
            code {
                background-color: #f4f4f4;
                border-bottom: 1px solid #ddd;
            	padding: 10px;
            	display: block;
            }


            So I think it isn’t parsed by MODx. When I’m testing <pre> or <pre [options]> tags and inside <code> they are also working but it doesn’t look so good.

            There is problem in TinyMCE. It’s all the time parsing <pre> tag I cannot use <pre [options]>. Maybe class="mceNoEditor" can help in TinyMCE.

            Examples: http://q3e.pl/sites/code-pre-example.html
              • 3749
              • 24,544 Posts
              Quote from: sharkbait at Jul 26, 2009, 06:16 AM

              no, i mean your plugin will be used to display code on the frontend and highlight it, right?

              MODx parses code within <code> and <pre> tags, so if you want to display MODx snippet calls (for example MODx tutorials) they will be parsed, have a look at this thread -> http://modxcms.com/forums/index.php/topic,37787.msg228151.html

              I think that could work as an option, either by replacing the tag characters with HTML entities, or by converting ’[[’ to ’[<!-- -->[’ so the tags wouldn’t be processed.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting