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

Documentation generated on Mon, 11 Mar 2019 13:56:51 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.