Text_Wiki
[ class tree: Text_Wiki ] [ index: Text_Wiki ] [ all elements ]

Source for file list.php

Documentation is available at list.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: list.php,v 1.3 2004/03/30 16:47:00 pmjones Exp $
  20.  
  21.  
  22. /**
  23. * This class implements a Text_Wiki_Rule to find source text marked as
  24. * a bulleted or numbered list.  In short, if a line starts with '* ' then
  25. * it is a bullet list item; if a line starts with '# ' then it is a
  26. * number list item.  Spaces in front of the * or # indicate an indented
  27. * sub-list.  The list items must be on sequential lines (no blank lines
  28. * between them) -- a blank line indicates the beginning of a new list.
  29. *
  30. @author Paul M. Jones <pmjones@ciaweb.net>
  31. *
  32. @package Text_Wiki
  33. *
  34. */
  35.  
  36.     
  37.     
  38.     /**
  39.     * 
  40.     * The regular expression used to parse the source text and find
  41.     * matches conforming to this rule.  Used by the parse() method.
  42.     * 
  43.     * @access public
  44.     * 
  45.     * @var string 
  46.     * 
  47.     * @see parse()
  48.     * 
  49.     */
  50.     
  51.     var $regex = '/\n((\*|#) .*\n)(?! {0,}(\*|#))/Us';
  52.     
  53.     
  54.     /**
  55.     * 
  56.     * Generates a replacement for the matched text.  Token options are:
  57.     * 
  58.     * 'type' =>
  59.     *     'bullet_start' : the start of a bullet list
  60.     *     'bullet_end'   : the end of a bullet list
  61.     *     'number_start' : the start of a number list
  62.     *     'number_end'   : the end of a number list
  63.     *     'item_start'   : the start of item text (bullet or number)
  64.     *     'item_end'     : the end of item text (bullet or number)
  65.     *     'unknown'      : unknown type of list or item
  66.     *
  67.     * 'level' => the indent level (0 for the first level, 1 for the
  68.     * second, etc)
  69.     *
  70.     * 'count' => the list item number at this level. not needed for
  71.     * xhtml, but very useful for PDF and RTF.
  72.     * 
  73.     * @access public
  74.     *
  75.     * @param array &$matches The array of matches from parse().
  76.     *
  77.     * @return series of text and delimited tokens marking the different
  78.     *  list text and list elements.
  79.     *
  80.     */
  81.     
  82.     function process(&$matches)
  83.     {
  84.         // the replacement text we will return
  85.         $return '';
  86.         
  87.         // the list of post-processing matches
  88.         $list = array();
  89.         
  90.         // a stack of list-start and list-end types; we keep this
  91.         // so that we know what kind of list we're working with
  92.         // (bullet or number) and what indent level we're at.
  93.         $stack = array();
  94.         
  95.         // the item count is the number of list items for any
  96.         // given list-type on the stack
  97.         $itemcount = array();
  98.         
  99.         // populate $list with this set of matches. $matches[1] is the
  100.         // text matched as a list set by parse().
  101.         preg_match_all(
  102.             '=^( {0,})(\*|#) (.*)$=Ums',
  103.             $matches[1],
  104.             $list,
  105.             PREG_SET_ORDER
  106.         );
  107.         
  108.         // loop through each list-item element.
  109.         foreach ($list as $key => $val{
  110.             
  111.             // $val[0] is the full matched list-item line
  112.             // $val[1] is the number of initial spaces (indent level)
  113.             // $val[2] is the list item type (* or #)
  114.             // $val[3] is the list item text
  115.             
  116.             // how many levels are we indented? (1 means the "root"
  117.             // list level, no indenting.)
  118.             $level strlen($val[1]+ 1;
  119.             
  120.             // get the list item type
  121.             if ($val[2== '*'{
  122.                 $type 'bullet';
  123.             elseif ($val[2== '#'{
  124.                 $type 'number';
  125.             else {
  126.                 $type 'unknown';
  127.             }
  128.             
  129.             // get the text of the list item
  130.             $text $val[3];
  131.             
  132.             // add a level to the list?
  133.             if ($level count($stack)) {
  134.                 
  135.                 // the current indent level is greater than the
  136.                 // number of stack elements, so we must be starting
  137.                 // a new list.  push the new list type onto the
  138.                 // stack...
  139.                 array_push($stack$type);
  140.                 
  141.                 // ...and add a list-start token to the return.
  142.                 $return .= $this->addToken(
  143.                     array(
  144.                         'type' => $type '_start',
  145.                         'level' => $level - 1
  146.                     )
  147.                 );
  148.             }
  149.             
  150.             // remove a level from the list?
  151.             while (count($stack$level{
  152.                 
  153.                 // so we don't keep counting the stack, we set up a temp
  154.                 // var for the count.  -1 becuase we're going to pop the
  155.                 // stack in the next command.  $tmp will then equal the
  156.                 // current level of indent.
  157.                 $tmp count($stack- 1;
  158.                 
  159.                 // as long as the stack count is greater than the
  160.                 // current indent level, we need to end list types. 
  161.                 // continue adding end-list tokens until the stack count
  162.                 // and the indent level are the same.
  163.                 $return .= $this->addToken(
  164.                     array (
  165.                         'type' => array_pop($stack'_end',
  166.                         'level' => $tmp
  167.                     )
  168.                 );
  169.                 
  170.                 // reset to the current (previous) list type so that
  171.                 // the new list item matches the proper list type.
  172.                 $type $stack[$tmp - 1];
  173.                 
  174.                 // reset the item count for the popped indent level
  175.                 $itemcount[$tmp + 1= 0;
  176.             }
  177.             
  178.             // add to the item count for this list (taking into account
  179.             // which level we are at).
  180.             if (isset($itemcount[$level])) {
  181.                 // first count
  182.                 $itemcount[$level= 1;
  183.             else {
  184.                 // increment count
  185.                 $itemcount[$level]++;
  186.             }
  187.             
  188.             // create a list-item starting token.
  189.             $start $this->addToken(
  190.                 array(
  191.                     'type' => 'item_start',
  192.                     'level' => $level,
  193.                     'count' => $itemcount[$level]
  194.                 )
  195.             );
  196.             
  197.             // create a list-item ending token.
  198.             $end $this->addToken(
  199.                 array(
  200.                     'type' => 'item_end',
  201.                     'level' => $level,
  202.                     'count' => $itemcount[$level]
  203.                 )
  204.             );
  205.             
  206.             // add the starting token, list-item text, and ending token
  207.             // to the return.
  208.             $return .= $start $val[3$end;
  209.         }
  210.         
  211.         // the last list-item may have been indented.  go through the
  212.         // list-type stack and create end-list tokens until the stack
  213.         // is empty.
  214.         while (count($stack> 0{
  215.             $return .= $this->addToken(
  216.                 array (
  217.                     'type' => array_pop($stack'_end',
  218.                     'level' => count($stack)
  219.                 )
  220.             );
  221.         }
  222.         
  223.         // we're done!  send back the replacement text.
  224.         return "\n" $return "\n";
  225.     }
  226.     
  227.     
  228.     /**
  229.     * 
  230.     * Renders a token into text matching the requested format.
  231.     * 
  232.     * @access public
  233.     * 
  234.     * @param array $options The "options" portion of the token (second
  235.     *  element).
  236.     * 
  237.     * @return string The text rendered from the token options.
  238.     * 
  239.     */
  240.     
  241.     function renderXhtml($options)
  242.     {
  243.         // make nice variables (type, level, count)
  244.         extract($options);
  245.         
  246.         // set up indenting so that the results look nice; we do this
  247.         // in two steps to avoid str_pad mathematics.  ;-)
  248.         $pad str_pad(''$level"\t");
  249.         $pad str_replace("\t"'    '$pad);
  250.         
  251.         switch ($type{
  252.         
  253.         case 'bullet_start':
  254.             return $pad "<ul>\n";
  255.             break;
  256.         
  257.         case 'bullet_end':
  258.             return $pad "</ul>\n";
  259.             break;
  260.         
  261.         case 'number_start':
  262.             return $pad "<ol>\n";
  263.             break;
  264.         
  265.         case 'number_end':
  266.             return $pad "</ol>\n";
  267.             break;
  268.         
  269.         case 'item_start':
  270.             return $pad "<li>";
  271.             break;
  272.         
  273.         case 'item_end':
  274.             return "</li>\n";
  275.             break;
  276.         
  277.         default:
  278.             return '';
  279.         
  280.         }
  281.     }
  282. }
  283. ?>

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