Annotation of /mambo/branches/4.6/mambots/content/geshi.php
Parent Directory
|
Revision Log
Revision 151 - (view) (download)
| 1 : | root | 1 | <?php |
| 2 : | /** | ||
| 3 : | * @version $Id: geshi.php,v 1.1 2005/07/22 01:57:49 eddieajau Exp $ | ||
| 4 : | * @package Mambo | ||
| 5 : | * @copyright (C) 2000 - 2005 Miro International Pty Ltd | ||
| 6 : | * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL | ||
| 7 : | * Mambo is Free Software | ||
| 8 : | */ | ||
| 9 : | |||
| 10 : | /** ensure this file is being included by a parent file */ | ||
| 11 : | defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' ); | ||
| 12 : | |||
| 13 : | csouza | 151 | class geshiXML { |
| 14 : | var $parser; | ||
| 15 : | var $opentags = array(); | ||
| 16 : | var $opencount; | ||
| 17 : | var $accept = array(); | ||
| 18 : | var $pre = false; | ||
| 19 : | var $pattrs = array(); | ||
| 20 : | |||
| 21 : | function geshiXML () { | ||
| 22 : | $this->parser = xml_parser_create(); | ||
| 23 : | $startfunc = array (&$this, 'start_element'); | ||
| 24 : | $endfunc = array (&$this, 'end_element'); | ||
| 25 : | $charfunc = array (&$this, 'character_data'); | ||
| 26 : | xml_set_element_handler ($this->parser, $startfunc, $endfunc); | ||
| 27 : | xml_set_character_data_handler ($this->parser, $charfunc); | ||
| 28 : | } | ||
| 29 : | |||
| 30 : | function parse ($arguments) { | ||
| 31 : | xml_parse($this->parser, $arguments, true); | ||
| 32 : | xml_parser_free($parser); | ||
| 33 : | return $this->pattrs; | ||
| 34 : | } | ||
| 35 : | |||
| 36 : | function start_element ($parser, $element_name, $element_attrs) { | ||
| 37 : | if ($element_name == 'PRE') $pattrs = $element_attrs; | ||
| 38 : | } | ||
| 39 : | |||
| 40 : | function end_element ($parser, $element_name) { | ||
| 41 : | return; | ||
| 42 : | } | ||
| 43 : | |||
| 44 : | function character_data ($parser, $data) { | ||
| 45 : | return; | ||
| 46 : | } | ||
| 47 : | |||
| 48 : | } | ||
| 49 : | |||
| 50 : | root | 1 | $_MAMBOTS->registerFunction( 'onPrepareContent', 'botGeshi' ); |
| 51 : | |||
| 52 : | /** | ||
| 53 : | * Code Highlighting Mambot | ||
| 54 : | * | ||
| 55 : | * Replaces <pre>...</pre> tags with highlighted text | ||
| 56 : | */ | ||
| 57 : | function botGeshi( $published, &$row, &$params, $page=0 ) { | ||
| 58 : | |||
| 59 : | // define the regular expression for the bot | ||
| 60 : | $regex = "#<pre\s*(.*?)>(.*?)</pre>#s"; | ||
| 61 : | |||
| 62 : | if (!$published) { | ||
| 63 : | $row->text = preg_replace( $regex, '', $row->text ); | ||
| 64 : | return; | ||
| 65 : | } | ||
| 66 : | |||
| 67 : | $GLOBALS['_MAMBOT_GESHI_PARAMS'] =& $params; | ||
| 68 : | |||
| 69 : | // perform the replacement | ||
| 70 : | $row->text = preg_replace_callback( $regex, 'botGeshi_replacer', $row->text ); | ||
| 71 : | |||
| 72 : | return true; | ||
| 73 : | } | ||
| 74 : | /** | ||
| 75 : | * Replaces the matched tags an image | ||
| 76 : | * @param array An array of matches (see preg_match_all) | ||
| 77 : | * @return string | ||
| 78 : | */ | ||
| 79 : | function botGeshi_replacer( &$matches ) { | ||
| 80 : | $params =& $GLOBALS['_MAMBOT_GESHI_PARAMS']; | ||
| 81 : | include_once( dirname( __FILE__ ) . '/geshi/geshi.php' ); | ||
| 82 : | csouza | 151 | $parser =& new geshiXML(); |
| 83 : | $args = $parser->parse($matches[1]); | ||
| 84 : | root | 1 | $text = $matches[2]; |
| 85 : | |||
| 86 : | $lang = mosGetParam( $args, 'lang', 'php' ); | ||
| 87 : | $lines = mosGetParam( $args, 'lines', 'false' ); | ||
| 88 : | |||
| 89 : | |||
| 90 : | $html_entities_match = array( "|\<br \/\>|", "#<#", "#>#", "|'|", '#"#', '# #' ); | ||
| 91 : | $html_entities_replace = array( "\n", '<', '>', "'", '"', ' ' ); | ||
| 92 : | |||
| 93 : | $text = preg_replace( $html_entities_match, $html_entities_replace, $text ); | ||
| 94 : | |||
| 95 : | $text = str_replace('<', '<', $text); | ||
| 96 : | $text = str_replace('>', '>', $text); | ||
| 97 : | |||
| 98 : | /* | ||
| 99 : | // Replace 2 spaces with " " so non-tabbed code indents without making huge long lines. | ||
| 100 : | $text = str_replace(" ", " ", $text); | ||
| 101 : | // now Replace 2 spaces with " " to catch odd #s of spaces. | ||
| 102 : | $text = str_replace(" ", " ", $text); | ||
| 103 : | */ | ||
| 104 : | // Replace tabs with " " so tabbed code indents sorta right without making huge long lines. | ||
| 105 : | //$text = str_replace("\t", " ", $text); | ||
| 106 : | $text = str_replace( "\t", ' ', $text ); | ||
| 107 : | |||
| 108 : | $geshi = new GeSHi( $text, $lang, dirname( __FILE__ ) . '/geshi/geshi' ); | ||
| 109 : | if ($lines == 'true') { | ||
| 110 : | $geshi->enable_line_numbers( GESHI_NORMAL_LINE_NUMBERS ); | ||
| 111 : | } | ||
| 112 : | $text = $geshi->parse_code(); | ||
| 113 : | |||
| 114 : | return $text; | ||
| 115 : | } | ||
| 116 : | csouza | 151 | |
| 117 : | |||
| 118 : | ?> |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |
Web Hosting provided by Network Redux.

