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.1 2004/01/04 01:35:22 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.                 // ...and add a start token to the return.
  117.                 $return .= $this->addToken(
  118.                     array(
  119.                         'type' => 'start',
  120.                         'level' => $level - 1
  121.                     )
  122.                 );
  123.                 
  124.                 $return .= "\n";
  125.             }
  126.             
  127.             // remove a level?
  128.             while (count($stack$level{
  129.                 
  130.                 // as long as the stack count is greater than the
  131.                 // current indent level, we need to end list types.
  132.                 // continue adding end-list tokens until the stack count
  133.                 // and the indent level are the same.
  134.                 array_pop($stack);
  135.                 
  136.                 $return .= $this->addToken(
  137.                     array (
  138.                         'type' => 'end',
  139.                         'level' => count($stack)
  140.                     )
  141.                 );
  142.                 
  143.                 $return .= "\n";
  144.             }
  145.             
  146.             // add the line text.
  147.             $return .= $text "\n";
  148.         }
  149.         
  150.         // the last line may have been indented.  go through the stack
  151.         // and create end-tokens until the stack is empty.
  152.         while (count($stack> 0{
  153.             array_pop($stack);
  154.             $return .= $this->addToken(
  155.                 array (
  156.                     'type' => 'end',
  157.                     'level' => count($stack)
  158.                 )
  159.             );
  160.         }
  161.         
  162.         // we're done!  send back the replacement text.
  163.         return "\n$return";
  164.     }
  165.     
  166.     
  167.     /**
  168.     * 
  169.     * Renders a token into text matching the requested format.
  170.     * 
  171.     * @access public
  172.     * 
  173.     * @param array $options The "options" portion of the token (second
  174.     *  element).
  175.     * 
  176.     * @return string The text rendered from the token options.
  177.     * 
  178.     */
  179.     
  180.     function renderXhtml($options)
  181.     {
  182.         $type $options['type'];
  183.         $level $options['level'];
  184.     
  185.         // set up indenting so that the results look nice; we do this
  186.         // in two steps to avoid str_pad mathematics.  ;-)
  187.         $pad str_pad(''$level"\t");
  188.         $pad str_replace("\t"'    '$pad);
  189.         
  190.         // starting
  191.         if ($type == 'start'{
  192.             return "$pad<blockquote>";
  193.         }
  194.         
  195.         // ending
  196.         if ($type == 'end'{
  197.             return $pad "</blockquote>\n";
  198.         }
  199.     }
  200. }
  201. ?>

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