diff -r 441963e5b07a -r c715631f809a plugins/GeSHi.php --- a/plugins/GeSHi.php Sun Feb 10 18:10:52 2008 -0500 +++ b/plugins/GeSHi.php Sun Feb 10 19:00:04 2008 -0500 @@ -0,0 +1,126 @@ + tag support +$plugins->attachHook('text_wiki_construct', 'geshi_disable_tw_code($this);'); + +function geshi_disable_tw_code($tw) +{ + $tw->disable[] = 'Code'; + foreach ( $tw->rules as $i => $rule ) + { + if ( $rule == 'Code' ) + { + unset($tw->rules[$i]); + return true; + } + } +} + +// Prevent tags from being stripped or sanitized (the plugin will handle all sanitation) +$plugins->attachHook('render_sanitize_pre', 'geshi_strip_code($text, $geshi_code_blocks, $random_id);'); +$plugins->attachHook('render_sanitize_post', 'geshi_restore_code($text, $geshi_code_blocks, $random_id);'); + +function geshi_strip_code(&$text, &$codeblocks, $random_id) +{ + global $geshi_supported_formats; + $codeblocks = array(); + $sf = '(' . implode('|', $geshi_supported_formats) . ')'; + preg_match_all('/([\w\W]*?)<\/code>/ms', $text, $matches); + + // for debug + /* + if ( strstr($text, '' . htmlspecialchars(print_r($matches, true)) . '
' . htmlspecialchars($text) . '
' . htmlspecialchars('/([\w\W]*?)<\/code>/')); + */ + + foreach ( $matches[0] as $i => $match ) + { + $codeblocks[$i] = array( + 'match' => $match, + 'lang' => $matches[1][$i], + 'code' => $matches[2][$i] + ); + $text = str_replace_once($match, "{GESHI_BLOCK:$i:$random_id}", $text); + } +} + +function geshi_restore_code(&$text, &$codeblocks, $random_id) +{ + foreach ( $codeblocks as $i => $match ) + { + $text = str_replace_once("{GESHI_BLOCK:$i:$random_id}", $match['match'], $text); + } +} + +// Formatter hook - where the actual highlighting is performed +$plugins->attachHook('render_wikiformat_veryearly', 'geshi_strip_code($text, $codeblocks, $random_id);'); +$plugins->attachHook('render_wikiformat_post', 'geshi_perform_highlight($result, $codeblocks, $random_id);'); + +function geshi_perform_highlight(&$text, &$codeblocks, $random_id) +{ + static $did_header_tweak = false; + if ( !$did_header_tweak ) + { + $did_header_tweak = true; + global $template; + $template->add_header(' + '); + } + if ( !defined('GESHI_ROOT') ) + define('GESHI_ROOT', ENANO_ROOT . '/plugins/geshi/'); + + require_once ( GESHI_ROOT . '/base.php' ); + + foreach ( $codeblocks as $i => $match ) + { + $lang =& $match['lang']; + $code =& $match['code']; + + $geshi = new GeSHi($code, $lang, null); + $geshi->set_header_type(GESHI_HEADER_PRE); + // $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS); + $geshi->set_overall_class('geshi_highlighted'); + $parsed = $geshi->parse_code(); + + $text = str_replace_once("{GESHI_BLOCK:$i:$random_id}", $parsed, $text); + } +}