We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5683
    • 96 Posts
    Here is a plugin that adds the GeSHi syntax highlighter to MODx. The plugin parses the document content, calling GeSHi every time it finds some code inside the <pre> ... </pre> tags.

    You can define a default language for the highlighting, but also write code in another language with <pre language="otherLanguage"> ... </pre>.

    You can see an example here: http://www.dis.uniroma1.it/~allulli/index.php?id=21 (by the way, I have a styling problem with IE in this page, I will post a separate message about it).

    There is also a snippet that does, more or less, the same of my plugin, and that was written concurrently by bll0: http://modxcms.com/forums/index.php/topic,3579.0.html

    And here are the plugin and the complete package:

    Plugin Name: GeSHi
    Description: GeSHI Syntax Highlighter
    Event: OnWebPagePrerender

    /**
     *	GeSHi Plugin 0.4 for MODx
     *	by Luca Allulli, April 2006
     *
     *  Event: OnLoadWebDocument
     *
     *	Based on the GeSHi highlighter at http://qbnz.com/highlighter/
     *
     *	License: GNU GPL
     *
     */
     
    //Begin configuration
            global $defaultGeshiLang;
    	$defaultGeshiLang="java5";
    //End configuration 
     
    function replaceCodeBase($lang, $code) {
    	global $modx;
    	$path = $modx->config["base_path"].'assets/plugins/geshi/geshi/';
    	$geshi = new GeSHi($code, $lang, $path);
    	$geshi->set_header_type(GESHI_HEADER_PRE);
    	$geshi->set_tab_width(4);
    	return '<table width="100%"><tr><td>'.$geshi->parse_code().'</td></tr></table>';
    }
    
    function replaceCode($matches) {
    	global $defaultGeshiLang;
    	return replaceCodeBase($defaultGeshiLang, $matches[1]);
    }
    
    function replaceCodeLang($matches) {
    	return replaceCodeBase($matches[1], $matches[2]);
    }
    
    
    $e = &$modx->Event;
    
    
    switch ($e->name) {
    	case "OnLoadWebDocument":
    
    		include_once($modx->config["base_path"].'assets/plugins/geshi/geshi.php');
    
    		$modx->documentObject['content']=preg_replace_callback("#<pre>(.*?)</pre>#s", "replaceCode", $modx->documentObject['content']);
    
    		$modx->documentObject['content']=preg_replace_callback("#<pre language=\"([^\"]*)\">(.*?)</pre>#s", "replaceCodeLang", $modx->documentObject['content']);
    
    		break;
    		
    	default:	// stop here
    		return; 
    		break;	
    }
    
    


    Edit: Version 0.4 stores the highlighted code into MODx cache.
      • 5683
      • 96 Posts
      And now the styling problem. This page:
      http://www.dis.uniroma1.it/~allulli/index.php?id=21
      is shown correctly by Firefox, which places horizontal scrollbars inside each code box when text overflows; while IE doesn’t place horizontal scrollbars inside boxes, and enlarges the width of the entire page, making it very difficult to be read.

      This is a known problem of IE; do you have any experience on some workaround?

      Here is my current style:

      .java5 {
      	color: #333333;
      	background-color: #F2F2F2;
      	font: 80% "Lucida Console", monospace;
      	border: 1px dashed #85A4B7;
      	border-left: 3px solid #85A4B7;
      	/*margin: 10px 5px 15px auto;*/
      	padding: 3px;
      	width: 98%;
      	overflow: auto;
      	white-space: pre;
      }
      


      Thank you!
        • 3586
        • 27 Posts
        Please see the snippet-geshi thread for my whole comment please.

        Is there a way to enable other geshi features with this plugin? Like line-numbers, uppercasing or something like this?

        Maybe the <pre> tag could look like this:
        <pre language="php" line-numbers="true" uppercase="true>


        I think the problem will be that there are a lot of combinations. I’m not too familiar with regular expressions, but is there a possibility to
        get the different options dynamically (not a lot of preg_replace combinations) like you did it here?
        preg_replace_callback("#<pre>(.*?)</pre>#s", "replaceCode", $modx->documentOutput

          • 5683
          • 96 Posts
          I have updated the plugin: since GeSHi is VERY slow, the new version of the plugin applies highlighting only on those documents that are retrieved from the database. In this way, documents get cached along with their highlighting, reducing to 0 the cost of highlighting cached documents.
            • 25663 MODX Staff
            • 12,272 Posts
            Nice move. smiley
              Ryan Thrash, MODX Co-Founder
              Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
              • 14605
              • 40 Posts
              I managed to get line numbers working, and solve a wrapping issue in IE6, using the following article...

              http://www.websystemsgo.com/index.php?id=9

              to save you time reading the whole thing, you can copy the new core/components/geshi/model/geshi/geshiloader.class.php script from here...

              http://www.websystemsgo.com/index.php?id=10

              Thanks,

              Jon
                • 17499 ☆ A M B ☆
                • 872 Posts
                @Jon

                You mistake this version of GeSHI for Evolution and mine which has been made for Revolution (Evolution has no core/components/ folder ): http://modxcms.com/extras/package/?package=742

                In the last version, line number should be working without having to hack the original components.
                Note that GeSHI is available on git and you can provide you’re improvements officially for further release:

                http://github.com/lossendae/GeSHI

                Thank you smiley

                  • 14605
                  • 40 Posts
                  Hey lossendae

                  Thank you for pointing that out... I have seen that the new version is available via package management.

                  Many thanks

                  Jon
                    • 14605
                    • 40 Posts
                    @lossendae

                    I have now installed the new package... Do you have a page with example usage?? i.e how to switch on line numbers, list of available theme names etc...

                    Many thanks,

                    Jon
                      • 17499 ☆ A M B ☆
                      • 872 Posts
                      No example apge for now, but it’s definitively not a bad idea.

                      I’ll do that this week end and give the link in an official release with some adjustements and a few more options.

                      For now, you can just apply what is written in the readme file.