diff --git a/SyntaxHighlight_GeSHi.class.php b/SyntaxHighlight_GeSHi.class.php index 6d1f73c7..9579baaf 100644 --- a/SyntaxHighlight_GeSHi.class.php +++ b/SyntaxHighlight_GeSHi.class.php @@ -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( "
{$out}
" ); + 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 diff --git a/SyntaxHighlight_GeSHi.php b/SyntaxHighlight_GeSHi.php index da33ebee..caf3b302 100644 --- a/SyntaxHighlight_GeSHi.php +++ b/SyntaxHighlight_GeSHi.php @@ -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' );