MediaWiki  1.33.0
Poem.php
Go to the documentation of this file.
1 <?php
2 
10 class 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 }
Poem
This class handles formatting poems in WikiText, specifically anything within <poem></poem> tags.
Definition: Poem.php:10
Poem\init
static init(Parser $parser)
Bind the renderPoem function to the <poem> tag.
Definition: Poem.php:15
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
Poem\renderPoem
static renderPoem( $in, array $param, Parser $parser, PPFrame $frame)
Parse the text into proper poem format.
Definition: Poem.php:27
Poem\indentVerse
static indentVerse(array $m)
Callback for preg_replace_callback() that wraps content in an indented span.
Definition: Poem.php:84
$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:1985
$parser
see documentation in includes Linker php for Linker::makeImageLink or false for current used if you return false $parser
Definition: hooks.txt:1802
array
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))
PPFrame
Definition: Preprocessor.php:166
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Poem\replaceSpaces
static replaceSpaces(array $m)
Callback for preg_replace_callback() that replaces spaces with non-breaking spaces.
Definition: Poem.php:73