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

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