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.4 2004/09/25 19:05:13 pmjones Exp $
  3.  
  4.  
  5. /**
  6. * 
  7. * This class implements a Text_Wiki_Parse to find all heading tokens and
  8. * build a table of contents.  The [[toc]] tag gets replaced with a list
  9. * of all the level-2 through level-6 headings.
  10. *
  11. * @author Paul M. Jones <pmjones@ciaweb.net>
  12. *
  13. * @package Text_Wiki
  14. *
  15. */
  16.  
  17.  
  18.     
  19.     
  20.     /**
  21.     * 
  22.     * The regular expression used to parse the source text and find
  23.     * matches conforming to this rule.  Used by the parse() method.
  24.     * 
  25.     * @access public
  26.     * 
  27.     * @var string 
  28.     * 
  29.     * @see parse()
  30.     * 
  31.     */
  32.     
  33.     var $regex = "/\n\[\[toc( .*)?\]\]\n/m";
  34.     
  35.     
  36.     /**
  37.     * 
  38.     * Generates a replacement for the matched text.
  39.     *  
  40.     * Token options are:
  41.     * 
  42.     * 'type' => ['list_start'|'list_end'|'item_start'|'item_end'|'target']
  43.     *
  44.     * 'level' => The heading level (1-6).
  45.     *
  46.     * 'count' => Which entry number this is in the list.
  47.     * 
  48.     * @access public
  49.     *
  50.     * @param array &$matches The array of matches from parse().
  51.     *
  52.     * @return string A token indicating the TOC collection point.
  53.     *
  54.     */
  55.     
  56.     function process(&$matches)
  57.     {
  58.         $count = 0;
  59.         
  60.         if (isset($matches[1])) {
  61.             $attr = $this->getAttrs(trim($matches[1]));
  62.         } else {
  63.             $attr = array();
  64.         }
  65.         
  66.         $output = $this->wiki->addToken(
  67.             $this->rule,
  68.             array(
  69.                 'type' => 'list_start',
  70.                 'level' => 0,
  71.                 'attr' => $attr
  72.             )
  73.         );
  74.         
  75.         foreach ($this->wiki->getTokens('Heading') as $key => $val) {
  76.             
  77.             if ($val[1]['type'] != 'start') {
  78.                 continue;
  79.             }
  80.             
  81.             $options = array(
  82.                 'type'  => 'item_start',
  83.                 'id'    => $val[1]['id'],
  84.                 'level' => $val[1]['level'],
  85.                 'count' => $count ++
  86.             );
  87.             
  88.             $output .= $this->wiki->addToken($this->rule, $options);
  89.             
  90.             $output .= $val[1]['text'];
  91.             
  92.             $output .= $this->wiki->addToken(
  93.                 $this->rule,
  94.                 array(
  95.                     'type' => 'item_end',
  96.                     'level' => $val[1]['level']
  97.                 )
  98.             );
  99.         }
  100.         
  101.         $output .= $this->wiki->addToken(
  102.             $this->rule, array(
  103.                 'type' => 'list_end',
  104.                 'level' => 0
  105.             )
  106.         );
  107.         
  108.         return $output;
  109.     }
  110. }
  111. ?>

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