From f9a901e6a5ffd77a8616b94cfd59c0c0686a5da7 Mon Sep 17 00:00:00 2001 From: Ryan Schmidt Date: Sat, 11 Oct 2008 21:53:44 +0000 Subject: [PATCH] * fixes for the merged Poem extension, per comments by Tim Starling on CodeReview (r41710) * adding tests to the parser tests file --- includes/parser/Parser.php | 30 ++++++---- maintenance/parserTests.txt | 114 ++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 11 deletions(-) diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index 427e4ff630..d951a02466 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -4183,34 +4183,42 @@ class Parser * based on http://www.mediawiki.org/wiki/Extension:Poem */ - function renderPoem( $in, $param=array() ) { + function renderPoem( $in, $param = array() ) { /* using newlines in the text will cause the parser to add

tags, * which may not be desired in some cases */ - $nl = isset( $param['compact'] ) ? '' : "\n"; + $nl = array_key_exists( 'compact', $param ) ? '' : "\n"; $tag = $this->insertStripItem( "
", $this->mStripState ); - $text = preg_replace( - array( "/^\n/", "/\n$/D", "/\n/", "/^( +)/me" ), - array( "", "", "$tag\n", "str_replace(' ',' ','\\1')" ), - $in ); + // Only strip the very first and very last \n (which trim cannot do) + $text = $in; + if( substr( $in, 0, 1 ) == "\n" ) + $text = substr( $in, 1 ); + if( substr( $text, -1 ) == "\n" ) + $text = substr( $text, 0, -1 ); + + $text = str_replace( "\n", "$tag\n", $text ); + $text = preg_replace_callback( + "/^( +)/m", + create_function( + '$matches', + 'return str_replace(" ", " ", "$matches[0]");' + ), + $text ); $text = $this->recursiveTagParse( $text ); // Pass HTML attributes through to the output. $attribs = Sanitizer::validateTagAttributes( $param, 'div' ); // Wrap output in a

with "poem" class. - if( isset( $attribs['class'] ) ) { + if( array_key_exists( 'class', $attribs ) ) { $attribs['class'] = 'poem ' . $attribs['class']; } else { $attribs['class'] = 'poem'; } - return wfOpenElement( 'div', $attribs ) . - $nl . - trim( $text ) . - "$nl
"; + return XML::openElement( 'div', $attribs ) . $nl . trim( $text ) . $nl . XML::closeElement( 'div' ); } function getImageParams( $handler ) { diff --git a/maintenance/parserTests.txt b/maintenance/parserTests.txt index f397bdc42c..20876f2e8f 100644 --- a/maintenance/parserTests.txt +++ b/maintenance/parserTests.txt @@ -7148,6 +7148,120 @@ language=fa

!! end +!!test + +!!input + +this +is +a +test + +!!result +
+

this
+is
+a
+test +

+
+ +!!end + +!!test + with recursive parsing +!!input + +this ''is'' a '''test''' + +!! result +
+

this is a test +

+
+ +!!end + + +!!test + with leading whitespace +!!input + + + test + + +!!result +
+


+   test
+

+
+ +!!end + +!!test +Horizontal rule +!!input + +some +----- +text + +!!result +
+

some
+

+

+

text +

+
+ +!!end + +!!test + nested +!!input + +this +is +a +test + +!!result +
+


+this
+is
+a
+test
+ +

+
+ +!!end + +!!test + nested with formatting +!!input + +this +'''is''' +a +test + +!!result +
+


+this
+'''is'''
+a
+test
+ +

+
+ +!! end # # -- 2.20.1