MediaWiki REL1_30
Poem.class.php
Go to the documentation of this file.
1<?php
6class Poem {
12 public static function init( &$parser ) {
13 $parser->setHook( 'poem', [ 'Poem', 'renderPoem' ] );
14 return true;
15 }
16
25 public static function renderPoem( $in, $param = [], $parser = null, $frame = false ) {
26 // using newlines in the text will cause the parser to add <p> tags,
27 // which may not be desired in some cases
28 $newline = isset( $param['compact'] ) ? '' : "\n";
29
30 $tag = $parser->insertStripItem( "<br />", $parser->mStripState );
31
32 // replace colons with indented spans
33 $text = preg_replace_callback( '/^(:+)(.+)$/m', [ 'Poem', 'indentVerse' ], $in );
34
35 // replace newlines with <br /> tags unless they are at the beginning or end
36 // of the poem
37 $text = preg_replace(
38 [ "/^\n/", "/\n$/D", "/\n/" ],
39 [ "", "", "$tag\n" ],
40 $text );
41
42 // replace spaces at the beginning of a line with non-breaking spaces
43 $text = preg_replace_callback( '/^( +)/m', [ 'Poem', 'replaceSpaces' ], $text );
44
45 $text = $parser->recursiveTagParse( $text, $frame );
46
47 $attribs = Sanitizer::validateTagAttributes( $param, 'div' );
48
49 // Wrap output in a <div> with "poem" class.
50 if ( isset( $attribs['class'] ) ) {
51 $attribs['class'] = 'poem ' . $attribs['class'];
52 } else {
53 $attribs['class'] = 'poem';
54 }
55
56 return Html::rawElement( 'div', $attribs, $newline . trim( $text ) . $newline );
57 }
58
65 protected static function replaceSpaces( $m ) {
66 return str_replace( ' ', '&#160;', $m[1] );
67 }
68
76 protected static function indentVerse( $m ) {
77 $attribs = [
78 'class' => 'mw-poem-indented',
79 'style' => 'display: inline-block; margin-left: ' . strlen( $m[1] ) . 'em;'
80 ];
81 // @todo Should this really be raw?
82 return Html::rawElement( 'span', $attribs, $m[2] );
83 }
84}
This class handles formatting poems in WikiText, specifically anything within <poem></poem> tags.
Definition Poem.class.php:6
static renderPoem( $in, $param=[], $parser=null, $frame=false)
Parse the text into proper poem format.
static replaceSpaces( $m)
Callback for preg_replace_callback() that replaces spaces with non-breaking spaces.
static init(&$parser)
Bind the renderPoem function to the <poem> tag.
static indentVerse( $m)
Callback for preg_replace_callback() that wraps content in an indented span.
do that in ParserLimitReportFormat instead $parser
Definition hooks.txt:2572
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:1984