From 064fb261db1b9cfb5d2c556f0ac0c142c13ea77d Mon Sep 17 00:00:00 2001 From: daniel Date: Tue, 16 Oct 2012 12:33:26 +0200 Subject: [PATCH] Use new ContentGetParserOutput hook. Using ContentGetParserOutput instead of ShowRawCssJs allows highliting to be applied for other kinds of scripts as well (e.g. Lua). It also allows more special case code for CSS and JS to be phased out. NOTE: this requires Ibfb2cbef to be merged in core! Change-Id: Ie260c22680ec9a31e505c685d70e17efe8a7bf44 --- SyntaxHighlight_GeSHi.class.php | 62 ++++++++++++++++++++++++++++++++- SyntaxHighlight_GeSHi.php | 19 +++++++++- 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/SyntaxHighlight_GeSHi.class.php b/SyntaxHighlight_GeSHi.class.php index 6d002a65..38bf3052 100644 --- a/SyntaxHighlight_GeSHi.class.php +++ b/SyntaxHighlight_GeSHi.class.php @@ -227,7 +227,9 @@ class SyntaxHighlight_GeSHi { /** * Hook into Article::view() to provide syntax highlighting for - * custom CSS and JavaScript pages + * custom CSS and JavaScript pages. + * + * B/C for MW 1.20 and before. 1.21 and later use renderHook() instead. * * @param string $text * @param Title $title @@ -258,10 +260,68 @@ class SyntaxHighlight_GeSHi { return true; } + /** + * Hook into Content::getParserOutput to provide syntax highlighting for + * script content. + * + * @return bool + * @since MW 1.21 + */ + public static function renderHook( Content $content, Title $title, + ParserOptions $options, $generateHtml, ParserOutput &$output + ) { + + global $wgSyntaxHighlightModels, $wgUseSiteCss; + + // Determine the language + $model = $content->getModel(); + if ( !isset( $wgSyntaxHighlightModels[$model] ) ) { + // We don't care about this model, carry on. + return true; + } + + if ( !$generateHtml ) { + // Nothing to do. + return false; + } + + // Hope that $wgSyntaxHighlightModels does not contain silly types. + $text = Contenthandler::getContentText( $content ); + + if ( $text === null || $text === false ) { + // Oops! Non-text content? + return false; + } + + $lang = $wgSyntaxHighlightModels[$model]; + + // Attempt to format + $geshi = self::prepare( $text, $lang ); + if( $geshi instanceof GeSHi ) { + + $out = $geshi->parse_code(); + if( !$geshi->error() ) { + // Done + $output->addHeadItem( self::buildHeadItem( $geshi ), "source-$lang" ); + $output->setText( "
{$out}
" ); + + if( $wgUseSiteCss ) { + $output->addModuleStyles( 'ext.geshi.local' ); + } + return false; + } + } + + // Bottle out + return true; + } + /** * Initialise a GeSHi object to format some code, performing * common setup for all our uses of it * + * @note Used only until MW 1.20 + * * @param string $text * @param string $lang * @return GeSHi diff --git a/SyntaxHighlight_GeSHi.php b/SyntaxHighlight_GeSHi.php index 1367f2e0..0b0649a3 100644 --- a/SyntaxHighlight_GeSHi.php +++ b/SyntaxHighlight_GeSHi.php @@ -52,13 +52,30 @@ $wgSyntaxHighlightDefaultLang = null; //Change this in LocalSettings.php $dir = dirname(__FILE__) . '/'; $wgExtensionMessagesFiles['SyntaxHighlight_GeSHi'] = $dir . 'SyntaxHighlight_GeSHi.i18n.php'; $wgAutoloadClasses['SyntaxHighlight_GeSHi'] = $dir . 'SyntaxHighlight_GeSHi.class.php'; -$wgHooks['ShowRawCssJs'][] = 'SyntaxHighlight_GeSHi::viewHook'; $wgHooks['ParserFirstCallInit'][] = 'efSyntaxHighlight_GeSHiSetup'; $wgHooks['ExtensionTypes'][] = 'SyntaxHighlight_GeSHi::hSpecialVersion_GeSHi'; +if ( defined( 'MW_SUPPORTS_CONTENTHANDLER' ) ) { + // since MW 1.21 + $wgHooks['ContentGetParserOutput'][] = 'SyntaxHighlight_GeSHi::renderHook'; +} else { + // B/C until 1.20 + $wgHooks['ShowRawCssJs'][] = 'SyntaxHighlight_GeSHi::viewHook'; +} + + $wgAutoloadClasses['HighlightGeSHilocal'] = $dir . 'SyntaxHighlight_GeSHi.local.php'; $wgResourceModules['ext.geshi.local'] = array( 'class' => 'HighlightGeSHilocal' ); +/** + * Map content models to the corresponding language names to be used with the highlighter. + * Pages with one of the given content models will automatically be highlighted. + */ +$wgSyntaxHighlightModels = array( + CONTENT_MODEL_CSS => 'css', + CONTENT_MODEL_JAVASCRIPT => 'javascript', +); + /** * Register parser hook *