Jump to content

User:Jiangxin/Patch Blankline As List Terminator

From Meta, a Wikimedia project coordination wiki

Description

[edit]

Bug 1115 described the annoyance of the current implement(1.5.2) of list.

Bug 1581: pre over multiple lines in lists (duplicate of 1518) is another problem. Though I have fixed it with User:Jiangxin/Patch Indent Pre, I find out these two annoyance can be fixed with a single solution.

Bug 1115 详细描述了当前 list 实现(1.5.2) 的不方便之处, 并建议以遇到空行或者下一个 list 时终止当前 list。

Bug 1581: pre over multiple lines in lists (duplicate of 1518) 是另外的一个问题, 虽然我已经用补丁: User:Jiangxin/Patch Indent Pre 修复了这个问题,但是我发现这两个问题可以有一个共同的解决方案。

Wiki code like this: (Wiki code 如下:)
<nowiki>
#item 1
blah blah blah...
##item 1-1 \
continued
blah blah blah...
##item 1-2
<pre>
pre text of item 1.2

above is a blank line
# in pre block
* in pre block

  1. item 2

blah blah blah...

another paragraph. </nowiki>

Patched version, will render as (补丁版本将会显示为) :
Unpatched Mediawiki 1.5.2, will render as (Mediawiki 1.5.2 将会显示为) :
  1. item 1
    blah blah blah...
    1. item 1-1 continued
      blah blah blah...
    2. item 1-2
      pre text of item 1.2
      
      above is a blank line
      # in pre block
      * in pre block
      
      
  2. item 2
    blah blah blah...
    

another paragraph.

  1. item 1

blah blah blah...

    1. item 1-1 \

continued blah blah blah...

    1. item 1-2
pre text of item 1.2

above is a blank line
# in pre block
* in pre block

  1. item 2

blah blah blah...

another paragraph.

Solution

[edit]

--- mediawiki-1-5-2/includes/Parser.php	2005-11-17 13:46:01.484375000 +0800
+++ mediawiki/includes/Parser.php	2005-11-17 13:48:07.937500000 +0800
@@ -1684,7 +1684,7 @@
 		if ( !$linestart ) {
 			$output .= array_shift( $textLines );
 		}
-		foreach ( $textLines as $oLine ) {
+		for($i=0; $oLine = $textLines[$i], $i < count($textLines); $i++) {
 			$lastPrefixLength = strlen( $lastPrefix );
 			$preCloseMatch = preg_match('/<\\/pre/i', $oLine );
 			$preOpenMatch = preg_match('/<pre/i', $oLine );
@@ -1693,6 +1693,54 @@
 				$prefixLength = strspn( $oLine, '*#:;' );
 				$pref = substr( $oLine, 0, $prefixLength );
 
+				# Bugfix 1115, 1581(3989): Newline as list item terminator is troublesome, by JiangXin (http://www.worldhello.net).
+				if ( $prefixLength > 0 ) {
+					$firstline = true;
+					$in_pre = false;
+					for($i++; $oLine2 = $textLines[$i], $i < count($textLines); $i++) {
+						
+						if( !empty($preOpenMatch) && empty($preCloseMatch) ) {
+							$in_pre = true;
+						}
+						else if( !empty($preCloseMatch) ) {
+							$in_pre = false;
+						}
+						
+						if ($in_pre) {
+							$firstline = false;
+							$oLine .= "\n" . $oLine2;
+						}
+						else if( preg_match('/^([\*#:;].*|^[\\s]*)$/', $oLine2) ) {
+							$i--;
+							break;
+						}
+						else
+						{
+							if( $firstline )
+							{
+								if( !preg_match('/[\\\]$/', $oLine) )
+								{
+									$firstline = false;
+									$oLine .= "<br>\n" . $oLine2;
+								}
+								else
+								{
+									$oLine = rtrim($oLine, "\\");
+									$oLine .= "\n" . $oLine2;
+								}
+							}
+							else
+							{
+								$oLine .= "\n" . $oLine2;
+							}
+						}
+						$preOpenMatch = preg_match('/<pre/i', $oLine2 );						
+						$preCloseMatch = preg_match('/<\\/pre/i', $oLine2 );
+					}
+					$preOpenMatch = false;
+					$preCloseMatch = false;
+				}
+				
 				# eh?
 				$pref2 = str_replace( ';', ':', $pref );
 				$t = substr( $oLine, $prefixLength );

-- Jiangxin 06:49, 17 November 2005 (UTC)

Test cases in ParserTests.txt

[edit]

add several test cases in ParserTests.txt

<nowiki>
!! test
indent <pre> block test
!! input
#indent <pre> block
:<pre>
  test.
  1. item2
  test.
  1. item3

!! result

  1. indent <pre> block
  test.
  1. item2
      test.
    
  2. item3

!! end


!! test

indent

 block test 2
!! input
#item1
##item1.1
##:<pre>
 pre format content here
    1. item1.2
  1. item2
    • item2.1
      intent content
    • item2.2

!! result

  1. item1
    1. item1.1
       pre format content here
      
    2. item1.2
  2. item2
    • item2.1
      intent content
    • item2.2

!! end

!! test

indent

 block test 3
!! input
#item1
##item1.1
##:<pre>
 pre format content here
!! result
<ol><li>item1
<ol><li>item1.1
<dl><dd><pre>
 pre format content here

!! end

!! test Bug 1115: Newline as list item terminator is troublesome !! input

  1. item 1

blah blah blah...

    1. item 1-1 \

continued blah blah blah...

    1. item 1-2
pre text of item 1.2

above is a blank line
# in pre block
* in pre block

  1. item 2

blah blah blah...

another paragraph. !! result

  1. item 1
    blah blah blah...
    1. item 1-1 continued
      blah blah blah...
    2. item 1-2
      pre text of item 1.2
      
      above is a blank line
      # in pre block
      * in pre block
      
      
  2. item 2
    blah blah blah...
    

another paragraph.

!! end </nowiki>