home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Text / Wiki / Parse / Default / Toc.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  2.8 KB  |  130 lines

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