MediaWiki  1.30.0
Poem.class.php
Go to the documentation of this file.
1 <?php
6 class 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 }
Poem
This class handles formatting poems in WikiText, specifically anything within <poem></poem> tags.
Definition: Poem.class.php:6
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$attribs
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:1965
Poem::indentVerse
static indentVerse( $m)
Callback for preg_replace_callback() that wraps content in an indented span.
Definition: Poem.class.php:76
$parser
do that in ParserLimitReportFormat instead $parser
Definition: hooks.txt:2572
Poem::init
static init(&$parser)
Bind the renderPoem function to the <poem> tag.
Definition: Poem.class.php:12
Poem::replaceSpaces
static replaceSpaces( $m)
Callback for preg_replace_callback() that replaces spaces with non-breaking spaces.
Definition: Poem.class.php:65
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
Poem::renderPoem
static renderPoem( $in, $param=[], $parser=null, $frame=false)
Parse the text into proper poem format.
Definition: Poem.class.php:25