[ Index ]

PHP Cross Reference of Mambo 4.6.5

[ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/mambots/content/ -> geshi.php (source)

   1  <?php
   2  /**
   3  * @package Mambo Open Source
   4  * @copyright (C) 2005 - 2007 Mambo Foundation Inc.
   5  * @license http://www.opensource.org/licenses/gpl-2.0.php GNU/GPL
   6  *
   7  * Mambo was originally developed by Miro (www.miro.com.au) in 2000. Miro assigned the copyright in Mambo to The Mambo Foundation in 2005 to ensure
   8  * that Mambo remained free Open Source software owned and managed by the community.
   9  * Mambo is Free Software
  10  */
  11  
  12  /** ensure this file is being included by a parent file */
  13  defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
  14  
  15  class geshiXML {
  16      var $parser;
  17      var $opentags = array();
  18      var $opencount;
  19      var $accept = array();
  20      var $pre = false;
  21      var $pattrs = array();
  22  
  23  	function geshiXML () {
  24          $this->parser = xml_parser_create();
  25          $startfunc = array (&$this, 'start_element');
  26          $endfunc = array (&$this, 'end_element');
  27          $charfunc = array (&$this, 'character_data');
  28          xml_set_element_handler ($this->parser, $startfunc, $endfunc);
  29          xml_set_character_data_handler ($this->parser, $charfunc);
  30      }
  31  
  32  	function parse ($arguments) {
  33          xml_parse($this->parser, $arguments, true);
  34          if ($parser) {
  35              xml_parser_free($parser);
  36          }
  37          return $this->pattrs;
  38      }
  39  
  40  	function start_element ($parser, $element_name, $element_attrs) {
  41          if ($element_name == 'PRE') $pattrs = $element_attrs;
  42      }
  43  
  44  	function end_element ($parser, $element_name) {
  45          return;
  46      }
  47  
  48  	function character_data ($parser, $data) {
  49          return;
  50      }
  51  
  52  }
  53  
  54  $_MAMBOTS->registerFunction( 'onPrepareContent', 'botGeshi' );
  55  
  56  /**
  57  * Code Highlighting Mambot
  58  *
  59  * Replaces <pre>...</pre> tags with highlighted text
  60  */
  61  function botGeshi( $published, &$row, &$params, $page=0 ) {
  62  
  63      // define the regular expression for the bot
  64      $regex = "#<pre\s*(.*?)>(.*?)</pre>#s";
  65  
  66      if (is_callable(array($row, 'getText'))) $localtext = $row->getText();
  67      else $localtext = $row->text;
  68      if (!$published) {
  69          $localtext = preg_replace( $regex, '', $localtext );
  70          if (is_callable(array($row, 'saveText'))) $row->saveText($localtext);
  71          else $row->text = $localtext;
  72          return;
  73      }
  74  
  75      $GLOBALS['_MAMBOT_GESHI_PARAMS'] =& $params;
  76  
  77      // perform the replacement
  78      $localtext = preg_replace_callback( $regex, 'botGeshi_replacer', $localtext );
  79      if (is_callable(array($row, 'saveText'))) $row->saveText($localtext);
  80      else $row->text = $localtext;
  81  
  82      return true;
  83  }
  84  /**
  85  * Replaces the matched tags an image
  86  * @param array An array of matches (see preg_match_all)
  87  * @return string
  88  */
  89  function botGeshi_replacer( &$matches ) {
  90      $params =& $GLOBALS['_MAMBOT_GESHI_PARAMS'];
  91      include_once( dirname( __FILE__ ) . '/geshi/geshi.php' );
  92      $parser =& new geshiXML();
  93      $args = $parser->parse($matches[1]);
  94      $text = $matches[2];
  95  
  96      $lang = mosGetParam( $args, 'lang', 'php' );
  97      $lines = mosGetParam( $args, 'lines', 'false' );
  98  
  99  
 100      $html_entities_match = array( "|\<br \/\>|", "#<#", "#>#", "|&#39;|", '#&quot;#', '#&nbsp;#' );
 101      $html_entities_replace = array( "\n", '&lt;', '&gt;', "'", '"', ' ' );
 102  
 103      $text = preg_replace( $html_entities_match, $html_entities_replace, $text );
 104  
 105      $text = str_replace('&lt;', '<', $text);
 106      $text = str_replace('&gt;', '>', $text);
 107  
 108  /*
 109      // Replace 2 spaces with "&nbsp; " so non-tabbed code indents without making huge long lines.
 110      $text = str_replace("  ", "&nbsp; ", $text);
 111      // now Replace 2 spaces with " &nbsp;" to catch odd #s of spaces.
 112      $text = str_replace("  ", " &nbsp;", $text);
 113  */
 114      // Replace tabs with "&nbsp; &nbsp;" so tabbed code indents sorta right without making huge long lines.
 115      //$text = str_replace("\t", "&nbsp; &nbsp;", $text);
 116      $text = str_replace( "\t", '  ', $text );
 117  
 118      $geshi = new GeSHi( $text, $lang, dirname( __FILE__ ) . '/geshi/geshi' );
 119      if ($lines == 'true') {
 120          $geshi->enable_line_numbers( GESHI_NORMAL_LINE_NUMBERS );
 121      }
 122      $text = $geshi->parse_code();
 123  
 124      return $text;
 125  }
 126  
 127  
 128  ?>