Implement 'ApiFormatHighlight' hook

Core change I04b1a3842 adds a hook to allow extensions to
syntax-highlight the pretty-printed output from the API.

Change-Id: If0413a1d922ff8a47afc355e0a2cc276cf54b400
This commit is contained in:
Brad Jorsch 2014-09-17 15:32:35 -04:00
parent da41c89fb7
commit 8dde10991c
2 changed files with 48 additions and 0 deletions

View file

@ -294,6 +294,53 @@ class SyntaxHighlight_GeSHi {
return true;
}
/**
* Hook to provide syntax highlighting for API pretty-printed output
*
* @param IContextSource $context
* @param string $text
* @param string $mime
* @param string $format
* @since MW 1.24
*/
public static function apiFormatHighlight( IContextSource $context, $text, $mime, $format ) {
global $wgUseSiteCss;
switch ( $mime ) {
case 'text/javascript':
case 'application/json':
$lang = 'javascript';
break;
case 'text/xml':
$lang = 'xml';
break;
default:
// Don't know how to handle this
return true;
}
$geshi = self::prepare( $text, $lang );
if( $geshi instanceof GeSHi ) {
$out = $geshi->parse_code();
if( !$geshi->error() ) {
$output = $context->getOutput();
$output->addModuleStyles( "ext.geshi.language.$lang" );
$output->addHTML( "<div dir=\"ltr\">{$out}</div>" );
if( $wgUseSiteCss ) {
$output->addModuleStyles( 'ext.geshi.local' );
}
// Inform MediaWiki that we have parsed this page and it shouldn't mess with it.
return false;
}
}
// Bottle out
return true;
}
/**
* Initialise a GeSHi object to format some code, performing
* common setup for all our uses of it

View file

@ -59,6 +59,7 @@ $wgAutoloadClasses['ResourceLoaderGeSHiLocalModule'] = $dir . 'ResourceLoaderGeS
$wgHooks['ExtensionTypes'][] = 'SyntaxHighlight_GeSHi::extensionTypes';
$wgHooks['ResourceLoaderRegisterModules'][] = 'SyntaxHighlight_GeSHi::resourceLoaderRegisterModules';
$wgHooks['ContentGetParserOutput'][] = 'SyntaxHighlight_GeSHi::renderHook';
$wgHooks['ApiFormatHighlight'][] = 'SyntaxHighlight_GeSHi::apiFormatHighlight';
// Module to load MediaWiki:Geshi.css.
$wgResourceModules['ext.geshi.local'] = array( 'class' => 'ResourceLoaderGeSHiLocalModule' );