From 8fb655258d0f7ce4973105a76ff486fa94392b3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20Tisza?= Date: Mon, 12 Oct 2015 12:32:17 -0700 Subject: [PATCH] Fix SyntaxHighlight incompatibility The class existence check Scribunto used to tell apart current and B/C versions of SyntaxHighlight does not work with recent versions. This caused the B/C branch to be invoked unnecessarily, which resulted in deprecation warnings. Also, the supposedly non-B/C branch also invoked B/C code which has no error handling. The commit removes B/C support and adds a new way of interacting with SyntaxHighlight. Bug: T109873 Change-Id: I2d518b5412efbe4e8ddb43e7c465ea55dc44b1a3 --- includes/common/ScribuntoContent.php | 53 ++++++++++++++-------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/includes/common/ScribuntoContent.php b/includes/common/ScribuntoContent.php index ebfa2874..ff2ec8e4 100644 --- a/includes/common/ScribuntoContent.php +++ b/includes/common/ScribuntoContent.php @@ -47,7 +47,7 @@ class ScribuntoContent extends TextContent { protected function fillParserOutput( Title $title, $revId, ParserOptions $options, $generateHtml, ParserOutput &$output ) { - global $wgParser, $wgScribuntoUseGeSHi, $wgUseSiteCss; + global $wgParser; $text = $this->getNativeData(); @@ -121,32 +121,8 @@ class ScribuntoContent extends TextContent { $engine = Scribunto::newDefaultEngine(); $engine->setTitle( $title ); - - // Add HTML for the actual script - $language = $engine->getGeSHiLanguage(); - if ( $wgScribuntoUseGeSHi && class_exists( SyntaxHighlight_GeSHi::class ) && $language ) { - $geshi = SyntaxHighlight_GeSHi::prepare( $text, $language ); - $geshi->set_language( $language ); - if ( $geshi instanceof GeSHi && !$geshi->error() ) { - $code = $geshi->parse_code(); - if ( $code ) { - // @todo Once we drop support for old versions of - // Extension:SyntaxHighlight_GeSHi, drop the ugly test and - // the BC case. - global $wgAutoloadClasses; - if ( isset( $wgAutoloadClasses['ResourceLoaderGeSHiModule'] ) ) { - $output->addModuleStyles( "ext.geshi.language.$language" ); - } else { - // Backwards compatibility - $output->addHeadItem( SyntaxHighlight_GeSHi::buildHeadItem( $geshi ), "source-{$language}" ); - } - if ( $wgUseSiteCss ) { - $output->addModuleStyles( 'ext.geshi.local' ); - } - $output->setText( $output->getRawText() . $code ); - return $output; - } - } + if ( $this->highlight( $text, $output, $engine ) ) { + return $output; } // No GeSHi, or GeSHi can't parse it, use plain
@@ -158,4 +134,27 @@ class ScribuntoContent extends TextContent {
 
 		return $output;
 	}
+
+	/**
+	 * Adds syntax highlighting to the output (or do not touch it and return false).
+	 * @param string $text
+	 * @param ParserOutput $output
+	 * @param ScribuntoEngineBase $engine
+	 * @return bool Success status
+	 */
+	protected function highlight( $text, ParserOutput $output, ScribuntoEngineBase $engine ) {
+		global $wgScribuntoUseGeSHi;
+		$language = $engine->getGeSHiLanguage();
+		if ( $wgScribuntoUseGeSHi && class_exists( SyntaxHighlight::class ) && $language ) {
+			$status = SyntaxHighlight::highlight( $text, $language );
+			if ( $status->isGood() ) {
+				// @todo replace addModuleStyles line with the appropriate call on
+				// SyntaxHighlight once one is created
+				$output->addModuleStyles( 'ext.pygments' );
+				$output->setText( $output->getRawText() . $status->getValue() );
+				return true;
+			}
+		}
+		return false;
+	}
 }