MediaWiki REL1_34
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:74
setHook( $tag, callable $callback)
Create an HTML-style tag, e.g.
Definition Parser.php:5189
recursiveTagParse( $text, $frame=false)
Half-parse wikitext to half-parsed HTML.
Definition Parser.php:795
insertStripItem( $text)
Add an item to the strip state Returns the unique tag which must be inserted into the stripped text T...
Definition Parser.php:1229
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