r89638 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r89637‎ | r89638 | r89639 >
Date:09:45, 7 June 2011
Author:ialex
Status:resolved (Comments)
Tags:
Comment:
Use preg_replace_callback() instead of preg_repace() with /e, also some style fixes
Modified paths:
  • /trunk/extensions/Poem/Poem.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Poem/Poem.php
@@ -30,7 +30,7 @@
3131 $wgExtensionMessagesFiles['Poem'] = dirname(__FILE__) . '/Poem.i18n.php';
3232
3333 function wfPoemExtension( &$parser ) {
34 - $parser->setHook("poem","PoemExtension");
 34+ $parser->setHook( 'poem', 'wfRenderPoemTag' );
3535 return true;
3636 }
3737
@@ -41,19 +41,21 @@
4242 * @param bool $frame
4343 * @return string
4444 */
45 -function PoemExtension( $in, $param=array(), $parser=null, $frame=false ) {
 45+function wfRenderPoemTag( $in, $param=array(), $parser=null, $frame=false ) {
4646
4747 /* using newlines in the text will cause the parser to add <p> tags,
48 - * which may not be desired in some cases
 48+ * which may not be desired in some cases
4949 */
5050 $nl = isset( $param['compact'] ) ? '' : "\n";
51 -
 51+
5252 $tag = $parser->insertStripItem( "<br />", $parser->mStripState );
 53+
5354 $text = preg_replace(
54 - array( "/^\n/", "/\n$/D", "/\n/", "/^( +)/me" ),
55 - array( "", "", "$tag\n", "str_replace(' ','&#160;','\\1')" ),
 55+ array( "/^\n/", "/\n$/D", "/\n/" ),
 56+ array( "", "", "$tag\n" ),
5657 $in );
57 - $text = $parser->recursiveTagParse( $text, $frame );
 58+ $text = preg_replace_callback( '/^( +)/m', 'wfPoemReplaceSpaces', $text );
 59+ $text = $parser->recursiveTagParse( $text, $frame );
5860
5961 $attribs = Sanitizer::validateTagAttributes( $param, 'div' );
6062
@@ -64,8 +66,15 @@
6567 $attribs['class'] = 'poem';
6668 }
6769
68 - return Xml::openElement( 'div', $attribs ) .
69 - $nl .
70 - trim( $text ) .
71 - "$nl</div>";
 70+ return array(
 71+ Html::rawElement( 'div', $attribs, $nl . trim( $text ) . $nl ),
 72+ 'markerType' => 'none',
 73+ );
7274 }
 75+
 76+/**
 77+ * Callback for preg_replace_callback()
 78+ */
 79+function wfPoemReplaceSpaces( $m ) {
 80+ return str_replace( ' ', '&#160;', $m[1] );
 81+}

Follow-up revisions

RevisionCommit summaryAuthorDate
r89639Remove unneeded experimental change from r89638ialex09:47, 7 June 2011

Comments

#Comment by 😂 (talk | contribs)   01:16, 8 June 2011

Could've moved it all into a class :)

Status & tagging log