Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
248 jpm 1
<?php
2
// $Id: Toc.php,v 1.1 2005-01-20 19:43:20 jpm 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
class Text_Wiki_Parse_Toc extends Text_Wiki_Parse {
19
 
20
 
21
    /**
22
    *
23
    * The regular expression used to parse the source text and find
24
    * matches conforming to this rule.  Used by the parse() method.
25
    *
26
    * @access public
27
    *
28
    * @var string
29
    *
30
    * @see parse()
31
    *
32
    */
33
 
34
    var $regex = "/\n\[\[toc( .*)?\]\]\n/m";
35
 
36
 
37
    /**
38
    *
39
    * Generates a replacement for the matched text.
40
    *
41
    * Token options are:
42
    *
43
    * 'type' => ['list_start'|'list_end'|'item_start'|'item_end'|'target']
44
    *
45
    * 'level' => The heading level (1-6).
46
    *
47
    * 'count' => Which entry number this is in the list.
48
    *
49
    * @access public
50
    *
51
    * @param array &$matches The array of matches from parse().
52
    *
53
    * @return string A token indicating the TOC collection point.
54
    *
55
    */
56
 
57
    function process(&$matches)
58
    {
59
        $count = 0;
60
 
61
        if (isset($matches[1])) {
62
            $attr = $this->getAttrs(trim($matches[1]));
63
        } else {
64
            $attr = array();
65
        }
66
 
67
        $output = $this->wiki->addToken(
68
            $this->rule,
69
            array(
70
                'type' => 'list_start',
71
                'level' => 0,
72
                'attr' => $attr
73
            )
74
        );
75
 
76
        foreach ($this->wiki->getTokens('Heading') as $key => $val) {
77
 
78
            if ($val[1]['type'] != 'start') {
79
                continue;
80
            }
81
 
82
            $options = array(
83
                'type'  => 'item_start',
84
                'id'    => $val[1]['id'],
85
                'level' => $val[1]['level'],
86
                'count' => $count ++
87
            );
88
 
89
            $output .= $this->wiki->addToken($this->rule, $options);
90
 
91
            $output .= $val[1]['text'];
92
 
93
            $output .= $this->wiki->addToken(
94
                $this->rule,
95
                array(
96
                    'type' => 'item_end',
97
                    'level' => $val[1]['level']
98
                )
99
            );
100
        }
101
 
102
        $output .= $this->wiki->addToken(
103
            $this->rule, array(
104
                'type' => 'list_end',
105
                'level' => 0
106
            )
107
        );
108
 
109
        return $output;
110
    }
111
}
112
?>