home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Text / Wiki / Parse / Default / Blockquote.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  4.7 KB  |  180 lines

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