From 224b22a051051f6c2e494c3a2fb4adb42898e2d1 Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Tue, 14 Jan 2014 19:24:18 +0100 Subject: Update to MediaWiki 1.22.1 --- .../SyntaxHighlight_GeSHi.class.php | 43 +++++++++++++++++++--- 1 file changed, 37 insertions(+), 6 deletions(-) (limited to 'extensions/SyntaxHighlight_GeSHi/SyntaxHighlight_GeSHi.class.php') diff --git a/extensions/SyntaxHighlight_GeSHi/SyntaxHighlight_GeSHi.class.php b/extensions/SyntaxHighlight_GeSHi/SyntaxHighlight_GeSHi.class.php index b1c9851d..7318574d 100644 --- a/extensions/SyntaxHighlight_GeSHi/SyntaxHighlight_GeSHi.class.php +++ b/extensions/SyntaxHighlight_GeSHi/SyntaxHighlight_GeSHi.class.php @@ -28,6 +28,12 @@ class SyntaxHighlight_GeSHi { // Don't trim leading spaces away, just the linefeeds $text = preg_replace( '/^\n+/', '', $text ); + if( $wgUseTidy ) { + // HTML Tidy will convert tabs to spaces incorrectly (bug 30930). + // Preemptively replace the spaces in a more controlled fashion. + $text = self::tabsToSpaces( $text ); + } + // Validate language if( isset( $args['lang'] ) && $args['lang'] ) { $lang = $args['lang']; @@ -95,12 +101,6 @@ class SyntaxHighlight_GeSHi { if( $enclose === GESHI_HEADER_DIV ) { $out = str_replace( "\n", '', $out ); } - // HTML Tidy will convert tabs to spaces incorrectly (bug 30930). - // But the conversion from tab to space occurs while reading the input, - // before the conversion from to tab, so we can armor it that way. - if( $wgUseTidy ) { - $out = str_replace( "\t", ' ', $out ); - } // Register CSS $parser->getOutput()->addHeadItem( self::buildHeadItem( $geshi ), "source-{$lang}" ); @@ -488,4 +488,35 @@ class SyntaxHighlight_GeSHi { public static function hOldSpecialVersion_GeSHi( &$sp, &$extensionTypes ) { return self::hSpecialVersion_GeSHi( $extensionTypes ); } + + /** + * Convert tabs to spaces + * + * @param string $text + * @return string + */ + private static function tabsToSpaces( $text ) { + $lines = explode( "\n", $text ); + $lines = array_map( array( __CLASS__, 'tabsToSpacesLine' ), $lines ); + return implode( "\n", $lines ); + } + + /** + * Convert tabs to spaces for a single line + * + * @param $line + * @internal param string $text + * @return string + */ + private static function tabsToSpacesLine( $line ) { + $parts = explode( "\t", $line ); + $width = 8; // To match tidy's config & typical browser defaults + $out = $parts[0]; + foreach( array_slice( $parts, 1 ) as $chunk ) { + $spaces = $width - (strlen( $out ) % $width); + $out .= str_repeat( ' ', $spaces ); + $out .= $chunk; + } + return $out; + } } -- cgit v1.2.2