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. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at                              |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Paul M. Jones <pmjones@ciaweb.net>                          |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: blockquote.php,v 1.3 2004/03/30 19:36:27 pmjones Exp $
  20.  
  21.  
  22. /**
  23. * This class implements a Text_Wiki_Rule to find source text marked as a
  24. * blockquote, identified by any number of greater-than signs '>' at the
  25. * start of the line, followed by a space, and then the quote text; each
  26. * '>' indicates an additional level of quoting.
  27. *
  28. @author Paul M. Jones <pmjones@ciaweb.net>
  29. *
  30. @package Text_Wiki
  31. *
  32. */
  33.  
  34.     
  35.     
  36.     /**
  37.     * 
  38.     * The regular expression used to parse the source text and find
  39.     * matches conforming to this rule.  Used by the parse() method.
  40.     * 
  41.     * @access public
  42.     * 
  43.     * @var string 
  44.     * 
  45.     * @see parse()
  46.     * 
  47.     */
  48.     
  49.     var $regex = '/\n((\>).*\n)(?!(\>))/Us';
  50.     
  51.     
  52.     /**
  53.     * 
  54.     * Generates a replacement for the matched text.  Token options are:
  55.     * 
  56.     * 'type' =>
  57.     *     'start' : the start of a blockquote
  58.     *     'end'   : the end of a blockquote
  59.     *
  60.     * 'level' => the indent level (0 for the first level, 1 for the
  61.     * second, etc)
  62.     * 
  63.     * @access public
  64.     *
  65.     * @param array &$matches The array of matches from parse().
  66.     *
  67.     * @return series of text and delimited tokens marking the different
  68.     *  list text and list elements.
  69.     *
  70.     */
  71.     
  72.     function process(&$matches)
  73.     {
  74.         // the replacement text we will return to parse()
  75.         $return '';
  76.         
  77.         // the list of post-processing matches
  78.         $list = array();
  79.         
  80.         // $matches[1] is the text matched as a list set by parse();
  81.         // create an array called $list that contains a new set of
  82.         // matches for the various list-item elements.
  83.         preg_match_all(
  84.             '=^(\>+) (.*\n)=Ums',
  85.             $matches[1],
  86.             $list,
  87.             PREG_SET_ORDER
  88.         );
  89.         
  90.         // a stack of starts and ends; we keep this so that we know what
  91.         // indent level we're at.
  92.         $stack = array();
  93.         
  94.         // loop through each list-item element.
  95.         foreach ($list as $key => $val{
  96.             
  97.             // $val[0] is the full matched list-item line
  98.             // $val[1] is the number of initial '>' chars (indent level)
  99.             // $val[2] is the quote text
  100.             
  101.             // we number levels starting at 1, not zero
  102.             $level strlen($val[1]);
  103.             
  104.             // get the text of the line
  105.             $text $val[2];
  106.             
  107.             // add a level to the list?
  108.             if ($level count($stack)) {
  109.                 
  110.                 // the current indent level is greater than the number
  111.                 // of stack elements, so we must be starting a new
  112.                 // level.  push the new level onto the stack with a 
  113.                 // dummy value (boolean true)...
  114.                 array_push($stacktrue);
  115.                 
  116.                 $return .= "\n";
  117.                 
  118.                 // ...and add a start token to the return.
  119.                 $return .= $this->addToken(
  120.                     array(
  121.                         'type' => 'start',
  122.                         'level' => $level - 1
  123.                     )
  124.                 );
  125.                 
  126.                 $return .= "\n\n";
  127.             }
  128.             
  129.             // remove a level?
  130.             while (count($stack$level{
  131.                 
  132.                 // as long as the stack count is greater than the
  133.                 // current indent level, we need to end list types.
  134.                 // continue adding end-list tokens until the stack count
  135.                 // and the indent level are the same.
  136.                 array_pop($stack);
  137.                 
  138.                 $return .= "\n\n";
  139.                 
  140.                 $return .= $this->addToken(
  141.                     array (
  142.                         'type' => 'end',
  143.                         'level' => count($stack)
  144.                     )
  145.                 );
  146.                 
  147.                 $return .= "\n";
  148.             }
  149.             
  150.             // add the line text.
  151.             $return .= $text;
  152.         }
  153.         
  154.         // the last line may have been indented.  go through the stack
  155.         // and create end-tokens until the stack is empty.
  156.         $return .= "\n";
  157.         
  158.         while (count($stack> 0{
  159.             array_pop($stack);
  160.             $return .= $this->addToken(
  161.                 array (
  162.                     'type' => 'end',
  163.                     'level' => count($stack)
  164.                 )
  165.             );
  166.         }
  167.         
  168.         // we're done!  send back the replacement text.
  169.         return "$return\n";
  170.     }
  171.     
  172.     
  173.     /**
  174.     * 
  175.     * Renders a token into text matching the requested format.
  176.     * 
  177.     * @access public
  178.     * 
  179.     * @param array $options The "options" portion of the token (second
  180.     *  element).
  181.     * 
  182.     * @return string The text rendered from the token options.
  183.     * 
  184.     */
  185.     
  186.     function renderXhtml($options)
  187.     {
  188.         $type $options['type'];
  189.         $level $options['level'];
  190.     
  191.         // set up indenting so that the results look nice; we do this
  192.         // in two steps to avoid str_pad mathematics.  ;-)
  193.         $pad str_pad(''$level"\t");
  194.         $pad str_replace("\t"'    '$pad);
  195.         
  196.         // starting
  197.         if ($type == 'start'{
  198.             return "$pad<blockquote>";
  199.         }
  200.         
  201.         // ending
  202.         if ($type == 'end'{
  203.             return $pad "</blockquote>\n";
  204.         }
  205.     }
  206. }
  207. ?>

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