includes/wikiengine/Parse/Mediawiki/Toc.php
changeset 1 fe660c52c48f
equal deleted inserted replaced
0:902822492a68 1:fe660c52c48f
       
     1 <?php
       
     2 
       
     3 /**
       
     4 * 
       
     5 * Looks through parsed text and builds a table of contents.
       
     6 * 
       
     7 * @category Text
       
     8 * 
       
     9 * @package Text_Wiki
       
    10 * 
       
    11 * @author Paul M. Jones <pmjones@php.net>
       
    12 * 
       
    13 * @license LGPL
       
    14 * 
       
    15 * @version $Id: Toc.php,v 1.4 2005/05/28 21:33:02 pmjones Exp $
       
    16 * 
       
    17 */
       
    18 
       
    19 /**
       
    20 * 
       
    21 * Looks through parsed text and builds a table of contents.
       
    22 * 
       
    23 * This class implements a Text_Wiki_Parse to find all heading tokens and
       
    24 * build a table of contents.  The [[toc]] tag gets replaced with a list
       
    25 * of all the level-2 through level-6 headings.
       
    26 *
       
    27 * @category Text
       
    28 * 
       
    29 * @package Text_Wiki
       
    30 * 
       
    31 * @author Paul M. Jones <pmjones@php.net>
       
    32 * 
       
    33 */
       
    34 
       
    35 
       
    36 class Text_Wiki_Parse_Toc extends Text_Wiki_Parse {
       
    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\[\[toc( .*)?\]\]\n/m";
       
    53     
       
    54     
       
    55     /**
       
    56     * 
       
    57     * Generates a replacement for the matched text.
       
    58     *  
       
    59     * Token options are:
       
    60     * 
       
    61     * 'type' => ['list_start'|'list_end'|'item_start'|'item_end'|'target']
       
    62     *
       
    63     * 'level' => The heading level (1-6).
       
    64     *
       
    65     * 'count' => Which entry number this is in the list.
       
    66     * 
       
    67     * @access public
       
    68     *
       
    69     * @param array &$matches The array of matches from parse().
       
    70     *
       
    71     * @return string A token indicating the TOC collection point.
       
    72     *
       
    73     */
       
    74     
       
    75     function process(&$matches)
       
    76     {
       
    77         $count = 0;
       
    78         
       
    79         if (isset($matches[1])) {
       
    80             $attr = $this->getAttrs(trim($matches[1]));
       
    81         } else {
       
    82             $attr = array();
       
    83         }
       
    84         
       
    85         $output = $this->wiki->addToken(
       
    86             $this->rule,
       
    87             array(
       
    88                 'type' => 'list_start',
       
    89                 'level' => 0,
       
    90                 'attr' => $attr
       
    91             )
       
    92         );
       
    93         
       
    94         foreach ($this->wiki->getTokens('Heading') as $key => $val) {
       
    95             
       
    96             if ($val[1]['type'] != 'start') {
       
    97                 continue;
       
    98             }
       
    99             
       
   100             $options = array(
       
   101                 'type'  => 'item_start',
       
   102                 'id'    => $val[1]['id'],
       
   103                 'level' => $val[1]['level'],
       
   104                 'count' => $count ++
       
   105             );
       
   106             
       
   107             $output .= $this->wiki->addToken($this->rule, $options);
       
   108             
       
   109             $output .= $val[1]['text'];
       
   110             
       
   111             $output .= $this->wiki->addToken(
       
   112                 $this->rule,
       
   113                 array(
       
   114                     'type' => 'item_end',
       
   115                     'level' => $val[1]['level']
       
   116                 )
       
   117             );
       
   118         }
       
   119         
       
   120         $output .= $this->wiki->addToken(
       
   121             $this->rule, array(
       
   122                 'type' => 'list_end',
       
   123                 'level' => 0
       
   124             )
       
   125         );
       
   126         
       
   127         return "\n$output\n";
       
   128     }
       
   129 }
       
   130 ?>