Inline preg_replace callback functions

No need to have them on the class level. PHP supports closures for a
long time now.

Change-Id: I124f2b3f706e5f7266a8b3236dd5de9fef792db5
This commit is contained in:
Thiemo Kreuz 2020-06-12 08:46:52 +02:00 committed by Huji
parent 28006eef6c
commit fc02062ece

View file

@ -32,7 +32,21 @@ class Poem {
$tag = $parser->insertStripItem( "<br />" );
// replace colons with indented spans
$text = preg_replace_callback( '/^(:++)(.+)$/m', [ self::class, 'indentVerse' ], $in );
$text = preg_replace_callback(
'/^(:++)(.+)$/m',
function ( array $matches ) {
$indention = strlen( $matches[1] );
return Html::rawElement(
'span',
[
'class' => 'mw-poem-indented',
'style' => 'display: inline-block; margin-left: ' . $indention . 'em;'
],
$matches[2]
);
},
$in
);
// replace newlines with <br /> tags unless they are at the beginning or end
// of the poem, or would directly follow exactly 4 dashes. See Parser::internalParse() for
@ -44,7 +58,13 @@ class Poem {
);
// replace spaces at the beginning of a line with non-breaking spaces
$text = preg_replace_callback( '/^ +/m', [ self::class, 'replaceSpaces' ], $text );
$text = preg_replace_callback(
'/^ +/m',
function ( array $matches ) {
return str_repeat( '&#160;', strlen( $matches[0] ) );
},
$text
);
$text = $parser->recursiveTagParse( $text, $frame );
@ -63,30 +83,4 @@ class Poem {
return Html::rawElement( 'div', $attribs, $newline . trim( $text ) . $newline );
}
/**
* Callback for preg_replace_callback() that replaces spaces with non-breaking spaces
* @param string[] $m Matches from the regular expression
* - $m[0] consists of 1 or more spaces
* @return string
*/
protected static function replaceSpaces( array $m ) {
return str_repeat( '&#160;', strlen( $m[0] ) );
}
/**
* Callback for preg_replace_callback() that wraps content in an indented span
* @param string[] $m Matches from the regular expression
* - $m[1] consists of 1 or more colons
* - $m[2] consists of the text after the colons
* @return string
*/
protected static function indentVerse( array $m ) {
$attribs = [
'class' => 'mw-poem-indented',
'style' => 'display: inline-block; margin-left: ' . strlen( $m[1] ) . 'em;'
];
// @todo Should this really be raw?
return Html::rawElement( 'span', $attribs, $m[2] );
}
}