1<?php
2
3require_once 'Text/Wiki/Parse/Default/Toc.php';
4
5/**
6 * Replaces the default Toc parser to search for Heading2 tokens instead.
7 *
8 * @package Wicked
9 */
10class Text_Wiki_Parse_Toc2 extends Text_Wiki_Parse_Toc
11{
12    /**
13    *
14    * Generates a replacement for the matched text.
15    *
16    * Token options are:
17    *
18    * 'type' => ['list_start'|'list_end'|'item_start'|'item_end'|'target']
19    *
20    * 'level' => The heading level (1-6).
21    *
22    * 'count' => Which entry number this is in the list.
23    *
24    * @access public
25    *
26    * @param array &$matches The array of matches from parse().
27    *
28    * @return string A token indicating the TOC collection point.
29    *
30    */
31
32    public function process(&$matches)
33    {
34        $count = 0;
35
36        if (isset($matches[1])) {
37            $attr = $this->getAttrs(trim($matches[1]));
38        } else {
39            $attr = array();
40        }
41
42        $output = $this->wiki->addToken(
43            $this->rule,
44            array(
45                'type' => 'list_start',
46                'level' => 0,
47                'attr' => $attr
48            )
49        );
50
51        foreach ($this->wiki->getTokens('Heading2') as $key => $val) {
52
53            if ($val[1]['type'] != 'start') {
54                continue;
55            }
56
57            $options = array(
58                'type'  => 'item_start',
59                'id'    => $val[1]['id'],
60                'level' => $val[1]['level'],
61                'count' => $count ++
62            );
63
64            $output .= $this->wiki->addToken($this->rule, $options);
65
66            $output .= $val[1]['text'];
67
68            $output .= $this->wiki->addToken(
69                $this->rule,
70                array(
71                    'type' => 'item_end',
72                    'level' => $val[1]['level']
73                )
74            );
75        }
76
77        $output .= $this->wiki->addToken(
78            $this->rule, array(
79                'type' => 'list_end',
80                'level' => 0
81            )
82        );
83
84        return "\n$output\n";
85    }
86}
87