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

Source for file Toc.php

Documentation is available at Toc.php

  1. <?php
  2. // $Id: Toc.php,v 1.1 2004/06/06 15:44:34 pmjones Exp $
  3.  
  4.  
  5. /**
  6. * This class implements a Text_Wiki_Parse to find all heading tokens and
  7. * build a table of contents.  The [[toc]] tag gets replaced with a list
  8. * of all the level-2 through level-6 headings.
  9. *
  10. @author Paul M. Jones <pmjones@ciaweb.net>
  11. *
  12. @package Text_Wiki
  13. *
  14. */
  15.  
  16.  
  17.     
  18.     
  19.     /**
  20.     * 
  21.     * The regular expression used to parse the source text and find
  22.     * matches conforming to this rule.  Used by the parse() method.
  23.     * 
  24.     * @access public
  25.     * 
  26.     * @var string 
  27.     * 
  28.     * @see parse()
  29.     * 
  30.     */
  31.     
  32.     var $regex = "/\[\[toc\]\]/m";
  33.     
  34.     
  35.     /**
  36.     * 
  37.     * The collection of headings (text and levels).
  38.     * 
  39.     * @access public
  40.     * 
  41.     * @var array 
  42.     * 
  43.     * @see _getEntries()
  44.     * 
  45.     */
  46.     
  47.     var $entry = array();
  48.     
  49.     
  50.     /**
  51.     * 
  52.     * Custom parsing (have to process heading entries first).
  53.     * 
  54.     * @access public
  55.     * 
  56.     * @see Text_Wiki::parse()
  57.     * 
  58.     */
  59.     
  60.     function parse()
  61.     {
  62.         // have to get all the heading entries before we can parse properly.
  63.         $this->_getEntries();
  64.         
  65.         // now parse the source text for TOC entries
  66.         parent::parse();
  67.     }
  68.     
  69.     
  70.     /**
  71.     * 
  72.     * Generates a replacement for the matched text.
  73.     *  
  74.     * Token options are:
  75.     * 
  76.     * 'type' => ['list_start'|'list_end'|'item_start'|'item_end'|'target']
  77.     *
  78.     * 'level' => The heading level (1-6).
  79.     *
  80.     * 'count' => Which entry number this is in the list.
  81.     * 
  82.     * @access public
  83.     *
  84.     * @param array &$matches The array of matches from parse().
  85.     *
  86.     * @return string A token indicating the TOC collection point.
  87.     *
  88.     */
  89.     
  90.     function process(&$matches)
  91.     {
  92.         $output $this->wiki->addToken(
  93.             $this->rule,
  94.             array('type' => 'list_start')
  95.         );
  96.         
  97.         foreach ($this->entry as $key => $val{
  98.         
  99.             $options = array(
  100.                 'type' => 'item_start',
  101.                 'count' => $val['count'],
  102.                 'level' => $val['level']
  103.             );
  104.             
  105.             $output .= $this->wiki->addToken($this->rule$options);
  106.             
  107.             $output .= $val['text'];
  108.             
  109.             $output .= $this->wiki->addToken(
  110.                 $this->rule,
  111.                 array('type' => 'item_end')
  112.             );
  113.         }
  114.         
  115.         $output .= $this->wiki->addToken(
  116.             $this->rulearray('type' => 'list_end')
  117.         );
  118.         
  119.         return $output;
  120.     }
  121.     
  122.     
  123.     /**
  124.     * 
  125.     * Finds all headings in the text and saves them in $this->entry.
  126.     * 
  127.     * @access private
  128.     *
  129.     * @return void 
  130.     * 
  131.     */
  132.     
  133.     function _getEntries()
  134.     {
  135.         // the wiki delimiter
  136.         $delim $this->wiki->delim;
  137.         
  138.         // list of all TOC entries (h2, h3, etc)
  139.         $this->entry = array();
  140.         
  141.         // the new source text with TOC entry tokens
  142.         $newsrc '';
  143.         
  144.         // when passing through the parsed source text, keep track of when
  145.         // we are in a delimited section
  146.         $in_delim = false;
  147.         
  148.         // when in a delimited section, capture the token key number
  149.         $key '';
  150.         
  151.         // TOC entry count
  152.         $count = 0;
  153.         
  154.         // pass through the parsed source text character by character
  155.         $k strlen($this->wiki->source);
  156.         for ($i = 0; $i $k$i++{
  157.             
  158.             // the current character
  159.             $char $this->wiki->source{$i};
  160.             
  161.             // are alredy in a delimited section?
  162.             if ($in_delim{
  163.             
  164.                 // yes; are we ending the section?
  165.                 if ($char == $delim{
  166.                     
  167.                     // yes, get the replacement text for the delimited
  168.                     // token number and unset the flag.
  169.                     $key = (int)$key;
  170.                     $rule $this->wiki->tokens[$key][0];
  171.                     $opts $this->wiki->tokens[$key][1];
  172.                     $in_delim = false;
  173.                     
  174.                     // is the key a start heading token
  175.                     // of level 2 or deeper?
  176.                     if ($rule == 'Heading' &&
  177.                         $opts['type'== 'start' &&
  178.                         $opts['level'> 1{
  179.                         
  180.                         // yes, add a TOC target link to the
  181.                         // tokens array...
  182.                         $token $this->wiki->addToken(
  183.                             $this->rule
  184.                             array(
  185.                                 'type' => 'target',
  186.                                 'count' => $count,
  187.                                 'level' => $opts['level']
  188.                             )
  189.                         );
  190.                         
  191.                         // ... and to the new source, before the
  192.                         // heading-start token.
  193.                         $newsrc .= $token $delim $key $delim;
  194.                         
  195.                         // retain the toc item
  196.                         $this->entry[= array (
  197.                             'count' => $count,
  198.                             'level' => $opts['level'],
  199.                             'text' => $opts['text']
  200.                         );
  201.                         
  202.                         // increase the count for the next entry
  203.                         $count++;
  204.                         
  205.                     else {
  206.                         // not a heading-start of 2 or deeper.
  207.                         // re-add the delimited token number
  208.                         // as it was in the original source.
  209.                         $newsrc .= $delim $key $delim;
  210.                     }
  211.                     
  212.                 else {
  213.                 
  214.                     // no, add to the delimited token key number
  215.                     $key .= $char;
  216.                     
  217.                 }
  218.                 
  219.             else {
  220.                 
  221.                 // not currently in a delimited section.
  222.                 // are we starting into a delimited section?
  223.                 if ($char == $delim{
  224.                     // yes, reset the previous key and
  225.                     // set the flag.
  226.                     $key '';
  227.                     $in_delim = true;
  228.                 else {
  229.                     // no, add to the output as-is
  230.                     $newsrc .= $char;
  231.                 }
  232.             }
  233.         }
  234.         
  235.         // replace entire source text with changed source text
  236.         $this->wiki->source = $newsrc;
  237.     }
  238. }
  239. ?>

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