[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/extensions/Poem/ -> Poem.class.php (source)

   1  <?php
   2  /**
   3   * This class handles formatting poems in WikiText, specifically anything within
   4   * <poem></poem> tags.
   5   */
   6  class Poem {
   7      /**
   8       * Bind the renderPoem function to the <poem> tag
   9       * @param Parser $parser
  10       * @return bool true
  11       */
  12  	public static function init( &$parser ) {
  13          $parser->setHook( 'poem', array( 'Poem', 'renderPoem' ) );
  14          return true;
  15      }
  16  
  17      /**
  18       * Parse the text into proper poem format
  19       * @param string $in The text inside the poem tag
  20       * @param array $param
  21       * @param Parser $parser
  22       * @param boolean $frame
  23       * @return string
  24       */
  25  	public static function renderPoem( $in, $param = array(), $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', array( '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              array( "/^\n/", "/\n$/D", "/\n/" ),
  39              array( "", "", "$tag\n" ),
  40              $text );
  41  
  42          // replace spaces at the beginning of a line with non-breaking spaces
  43          $text = preg_replace_callback( '/^( +)/m', array( '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  
  59      /**
  60       * Callback for preg_replace_callback() that replaces spaces with non-breaking spaces
  61       * @param array $m Matches from the regular expression
  62       *   - $m[1] consists of 1 or more spaces
  63       * @return mixed
  64       */
  65  	protected static function replaceSpaces( $m ) {
  66          return str_replace( ' ', '&#160;', $m[1] );
  67      }
  68  
  69      /**
  70       * Callback for preg_replace_callback() that wraps content in an indented span
  71       * @param array $m Matches from the regular expression
  72       *   - $m[1] consists of 1 or more colons
  73       *   - $m[2] consists of the text after the colons
  74       * @return string
  75       */
  76  	protected static function indentVerse( $m ) {
  77          $attribs = array(
  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  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1