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

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