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
This commit is contained in:
daniel 2012-10-16 12:33:26 +02:00 committed by Gerrit Code Review
parent 0122c20452
commit 064fb261db
2 changed files with 79 additions and 2 deletions

View file

@ -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( "<div dir=\"ltr\">{$out}</div>" );
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

View file

@ -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
*