Text_Wiki
[ class tree: Text_Wiki ] [ index: Text_Wiki ] [ all elements ]

Source for file Blockquote.php

Documentation is available at Blockquote.php

  1. <?php
  2.  
  3. /**
  4.  *
  5.  * Parse for block-quoted text.
  6.  *
  7.  * Find source text marked as a blockquote, identified by any number of
  8.  * greater-than signs '>' at the start of the line, followed by an
  9.  * optional space, and then the quote text; each '>' indicates an
  10.  * additional level of quoting.
  11.  *
  12.  * @category Text
  13.  *
  14.  * @package Text_Wiki
  15.  *
  16.  * @author Paul M. Jones <pmjones@php.net>
  17.  * @author Michele Tomaiuolo <tomamic@yahoo.it>
  18.  *
  19.  * @license LGPL
  20.  *
  21.  * @version $Id: Blockquote.php,v 1.2 2007/02/01 09:33:00 mic Exp $
  22.  *
  23.  */
  24.  
  25. class Text_Wiki_Parse_Blockquote extends Text_Wiki_Parse {
  26.  
  27.  
  28.     /**
  29.     *
  30.     * Regex for parsing the source text.
  31.     *
  32.     * @access public
  33.     *
  34.     * @var string 
  35.     *
  36.     * @see parse()
  37.     *
  38.     */
  39.  
  40.     var $regex = '/\n((\>).*\n)(?!(\>))/Us';
  41.  
  42.  
  43.     /**
  44.     *
  45.     * Generates a replacement for the matched text.
  46.     *
  47.     * Token options are:
  48.     *
  49.     * 'type' =>
  50.     *     'start' : the start of a blockquote
  51.     *     'end'   : the end of a blockquote
  52.     *
  53.     * 'level' => the indent level (0 for the first level, 1 for the
  54.     * second, etc)
  55.     *
  56.     * @access public
  57.     *
  58.     * @param array &$matches The array of matches from parse().
  59.     *
  60.     * @return series of text and delimited tokens marking the different
  61.     *  list text and list elements.
  62.     *
  63.     */
  64.  
  65.     function process(&$matches)
  66.     {
  67.         // the replacement text we will return to parse()
  68.         $return '';
  69.  
  70.         // the list of post-processing matches
  71.         $list = array();
  72.  
  73.         // $matches[1] is the text matched as a list set by parse();
  74.         // create an array called $list that contains a new set of
  75.         // matches for the various list-item elements.
  76.         preg_match_all(
  77.             '=^(\>+)(.*?\n)=ms',
  78.             $matches[1],
  79.             $list,
  80.             PREG_SET_ORDER
  81.         );
  82.  
  83.         // a stack of starts and ends; we keep this so that we know what
  84.         // indent level we're at.
  85.         $stack = array();
  86.  
  87.         // loop through each list-item element.
  88.         foreach ($list as $key => $val{
  89.  
  90.             // $val[0] is the full matched list-item line
  91.             // $val[1] is the number of initial '>' chars (indent level)
  92.             // $val[2] is the quote text
  93.  
  94.             // we number levels starting at 1, not zero
  95.             $level strlen($val[1]);
  96.  
  97.             // get the text of the line
  98.             $text trim($val[2]);
  99.  
  100.             // add a level to the list?
  101.             while ($level count($stack)) {
  102.  
  103.                 // the current indent level is greater than the number
  104.                 // of stack elements, so we must be starting a new
  105.                 // level.  push the new level onto the stack with a
  106.                 // dummy value (boolean true)...
  107.                 array_push($stacktrue);
  108.  
  109.                 //$return .= "\n";
  110.  
  111.                 // ...and add a start token to the return.
  112.                 $return .= $this->wiki->addToken(
  113.                     $this->rule,
  114.                     array(
  115.                         'type' => 'start',
  116.                         'level' => $level - 1
  117.                     )
  118.                 );
  119.  
  120.                 //$return .= "\n\n";
  121.             }
  122.  
  123.             // remove a level?
  124.             while (count($stack$level{
  125.  
  126.                 // as long as the stack count is greater than the
  127.                 // current indent level, we need to end list types.
  128.                 // continue adding end-list tokens until the stack count
  129.                 // and the indent level are the same.
  130.                 array_pop($stack);
  131.  
  132.                 //$return .= "\n";
  133.  
  134.                 $return .= $this->wiki->addToken(
  135.                     $this->rule,
  136.                     array (
  137.                         'type' => 'end',
  138.                         'level' => count($stack)
  139.                     )
  140.                 );
  141.  
  142.                 $return .= "\n";
  143.             }
  144.  
  145.             // add the line text.
  146.             $return .= $text "\n";
  147.         }
  148.  
  149.         // the last line may have been indented.  go through the stack
  150.         // and create end-tokens until the stack is empty.
  151.         $return .= "\n";
  152.  
  153.         while (count($stack> 0{
  154.             array_pop($stack);
  155.  
  156.             $return .= "\n\n";
  157.  
  158.             $return .= $this->wiki->addToken(
  159.                 $this->rule,
  160.                 array (
  161.                     'type' => 'end',
  162.                     'level' => count($stack)
  163.                 )
  164.             );
  165.  
  166.             $return .= "\n";
  167.         }
  168.  
  169.         // we're done!  send back the replacement text.
  170.         return "\n$return\n\n";
  171.     }
  172. }
  173. ?>

Documentation generated on Mon, 11 Mar 2019 14:55:48 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.