MediaWiki REL1_31
Poem.php
Go to the documentation of this file.
1<?php
2
10class Poem {
15 public static function init( Parser $parser ) {
16 $parser->setHook( 'poem', [ self::class, 'renderPoem' ] );
17 }
18
27 public static function renderPoem( $in, array $param = [], Parser $parser, PPFrame $frame ) {
28 // using newlines in the text will cause the parser to add <p> tags,
29 // which may not be desired in some cases
30 $newline = isset( $param['compact'] ) ? '' : "\n";
31
32 $tag = $parser->insertStripItem( "<br />" );
33
34 // replace colons with indented spans
35 $text = preg_replace_callback( '/^(:+)(.+)$/m', [ self::class, 'indentVerse' ], $in );
36
37 // replace newlines with <br /> tags unless they are at the beginning or end
38 // of the poem
39 $text = preg_replace(
40 [ "/^\n/", "/\n$/D", "/\n/" ],
41 [ "", "", "$tag\n" ],
42 $text
43 );
44
45 // replace spaces at the beginning of a line with non-breaking spaces
46 $text = preg_replace_callback( '/^( +)/m', [ self::class, 'replaceSpaces' ], $text );
47
48 $text = $parser->recursiveTagParse( $text, $frame );
49
50 $attribs = Sanitizer::validateTagAttributes( $param, 'div' );
51
52 // Wrap output in a <div> with "poem" class.
53 if ( isset( $attribs['class'] ) ) {
54 $attribs['class'] = 'poem ' . $attribs['class'];
55 } else {
56 $attribs['class'] = 'poem';
57 }
58
59 return Html::rawElement( 'div', $attribs, $newline . trim( $text ) . $newline );
60 }
61
68 protected static function replaceSpaces( array $m ) {
69 return str_replace( ' ', '&#160;', $m[1] );
70 }
71
79 protected static function indentVerse( array $m ) {
80 $attribs = [
81 'class' => 'mw-poem-indented',
82 'style' => 'display: inline-block; margin-left: ' . strlen( $m[1] ) . 'em;'
83 ];
84 // @todo Should this really be raw?
85 return Html::rawElement( 'span', $attribs, $m[2] );
86 }
87}
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:70
This class handles formatting poems in WikiText, specifically anything within <poem></poem> tags.
Definition Poem.php:10
static replaceSpaces(array $m)
Callback for preg_replace_callback() that replaces spaces with non-breaking spaces.
Definition Poem.php:68
static indentVerse(array $m)
Callback for preg_replace_callback() that wraps content in an indented span.
Definition Poem.php:79
static renderPoem( $in, array $param=[], Parser $parser, PPFrame $frame)
Parse the text into proper poem format.
Definition Poem.php:27
static init(Parser $parser)
Bind the renderPoem function to the <poem> tag.
Definition Poem.php:15
do that in ParserLimitReportFormat instead $parser
Definition hooks.txt:2603
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing & $attribs
Definition hooks.txt:2014