MediaWiki REL1_32
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, or would directly follow exactly 4 dashes. See Parser::internalParse() for
39 // the exact syntax for horizontal rules.
40 $text = preg_replace(
41 [ '/^\n/', '/\n$/D', '/(?<!^----)\n/m' ],
42 [ "", "", "$tag\n" ],
43 $text
44 );
45
46 // replace spaces at the beginning of a line with non-breaking spaces
47 $text = preg_replace_callback( '/^( +)/m', [ self::class, 'replaceSpaces' ], $text );
48
49 $text = $parser->recursiveTagParse( $text, $frame );
50
51 // Because of limitations of the regular expression above, horizontal rules with more than 4
52 // dashes still need special handling.
53 $text = str_replace( '<hr />' . $tag, '<hr />', $text );
54
55 $attribs = Sanitizer::validateTagAttributes( $param, 'div' );
56
57 // Wrap output in a <div> with "poem" class.
58 if ( isset( $attribs['class'] ) ) {
59 $attribs['class'] = 'poem ' . $attribs['class'];
60 } else {
61 $attribs['class'] = 'poem';
62 }
63
64 return Html::rawElement( 'div', $attribs, $newline . trim( $text ) . $newline );
65 }
66
73 protected static function replaceSpaces( array $m ) {
74 return str_replace( ' ', '&#160;', $m[1] );
75 }
76
84 protected static function indentVerse( array $m ) {
85 $attribs = [
86 'class' => 'mw-poem-indented',
87 'style' => 'display: inline-block; margin-left: ' . strlen( $m[1] ) . 'em;'
88 ];
89 // @todo Should this really be raw?
90 return Html::rawElement( 'span', $attribs, $m[2] );
91 }
92}
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition Parser.php:68
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:73
static indentVerse(array $m)
Callback for preg_replace_callback() that wraps content in an indented span.
Definition Poem.php:84
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
see documentation in includes Linker php for Linker::makeImageLink or false for current used if you return false $parser
Definition hooks.txt:1873
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:2063
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))