r97300 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r97299‎ | r97300 | r97301 >
Date:17:52, 16 September 2011
Author:brion
Status:ok
Tags:post1.18deploy 
Comment:
* (bug 30930) Fix bad tab-to-space conversion in SyntaxHighlight_GeSHi when $wgUseTidy on

Tidy always converts tabs to spaces on input; on a big <pre> section this is ok but it tends to fail on syntax-highlighted output, where the spacing should depend on the *output* not the *input markup*.
As a workaround, when $wgUseTidy is enabled we now apply our own tab-to-space conversion preemptively on the input before feeding it into GeSHi for highlighting; this keeps the right spacing through to output.
Modified paths:
  • /trunk/extensions/SyntaxHighlight_GeSHi/SyntaxHighlight_GeSHi.class.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SyntaxHighlight_GeSHi/SyntaxHighlight_GeSHi.class.php
@@ -21,12 +21,19 @@
2222 * @return string
2323 */
2424 public static function parserHook( $text, $args = array(), $parser ) {
25 - global $wgSyntaxHighlightDefaultLang;
 25+ global $wgSyntaxHighlightDefaultLang, $wgUseTidy;
2626 wfProfileIn( __METHOD__ );
2727 self::initialise();
2828 $text = rtrim( $text );
2929 // Don't trim leading spaces away, just the linefeeds
3030 $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+
3138 // Validate language
3239 if( isset( $args['lang'] ) && $args['lang'] ) {
3340 $lang = $args['lang'];
@@ -377,5 +384,34 @@
378385 public static function hOldSpecialVersion_GeSHi( &$sp, &$extensionTypes ) {
379386 return self::hSpecialVersion_GeSHi( $extensionTypes );
380387 }
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+ }
382418 }

Status & tagging log