Index: trunk/extensions/SyntaxHighlight_GeSHi/SyntaxHighlight_GeSHi.class.php |
— | — | @@ -21,12 +21,19 @@ |
22 | 22 | * @return string |
23 | 23 | */ |
24 | 24 | public static function parserHook( $text, $args = array(), $parser ) { |
25 | | - global $wgSyntaxHighlightDefaultLang; |
| 25 | + global $wgSyntaxHighlightDefaultLang, $wgUseTidy; |
26 | 26 | wfProfileIn( __METHOD__ ); |
27 | 27 | self::initialise(); |
28 | 28 | $text = rtrim( $text ); |
29 | 29 | // Don't trim leading spaces away, just the linefeeds |
30 | 30 | $text = preg_replace( '/^\n+/', '', $text ); |
| 31 | + |
| 32 | + if( $wgUseTidy ) { |
| 33 | + // HTML Tidy will convert tabs to spaces incorrectly (bug 30930). |
| 34 | + // Preemptively replace the spaces in a more controlled fashion. |
| 35 | + $text = self::tabsToSpaces( $text ); |
| 36 | + } |
| 37 | + |
31 | 38 | // Validate language |
32 | 39 | if( isset( $args['lang'] ) && $args['lang'] ) { |
33 | 40 | $lang = $args['lang']; |
— | — | @@ -377,5 +384,34 @@ |
378 | 385 | public static function hOldSpecialVersion_GeSHi( &$sp, &$extensionTypes ) { |
379 | 386 | return self::hSpecialVersion_GeSHi( $extensionTypes ); |
380 | 387 | } |
381 | | - |
| 388 | + |
| 389 | + /** |
| 390 | + * Convert tabs to spaces |
| 391 | + * |
| 392 | + * @param string $text |
| 393 | + * @return string |
| 394 | + */ |
| 395 | + private static function tabsToSpaces( $text ) { |
| 396 | + $lines = explode( "\n", $text ); |
| 397 | + $lines = array_map( array( __CLASS__, 'tabsToSpacesLine' ), $lines ); |
| 398 | + return implode( "\n", $lines ); |
| 399 | + } |
| 400 | + |
| 401 | + /** |
| 402 | + * Convert tabs to spaces for a single line |
| 403 | + * |
| 404 | + * @param string $text |
| 405 | + * @return string |
| 406 | + */ |
| 407 | + private static function tabsToSpacesLine( $line ) { |
| 408 | + $parts = explode( "\t", $line ); |
| 409 | + $width = 8; // To match tidy's config & typical browser defaults |
| 410 | + $out = $parts[0]; |
| 411 | + foreach( array_slice( $parts, 1 ) as $chunk ) { |
| 412 | + $spaces = $width - (strlen( $out ) % $width); |
| 413 | + $out .= str_repeat( ' ', $spaces ); |
| 414 | + $out .= $chunk; |
| 415 | + } |
| 416 | + return $out; |
| 417 | + } |
382 | 418 | } |