File: Toc2.php

package info (click to toggle)
php-horde-wicked 2.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,528 kB
  • ctags: 1,236
  • sloc: php: 5,386; xml: 1,027; makefile: 10; sh: 3
file content (86 lines) | stat: -rw-r--r-- 2,033 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php

require_once 'Text/Wiki/Parse/Default/Toc.php';

/**
 * Replaces the default Toc parser to search for Heading2 tokens instead.
 *
 * @package Wicked
 */
class Text_Wiki_Parse_Toc2 extends Text_Wiki_Parse_Toc
{
    /**
    *
    * Generates a replacement for the matched text.
    *
    * Token options are:
    *
    * 'type' => ['list_start'|'list_end'|'item_start'|'item_end'|'target']
    *
    * 'level' => The heading level (1-6).
    *
    * 'count' => Which entry number this is in the list.
    *
    * @access public
    *
    * @param array &$matches The array of matches from parse().
    *
    * @return string A token indicating the TOC collection point.
    *
    */

    public function process(&$matches)
    {
        $count = 0;

        if (isset($matches[1])) {
            $attr = $this->getAttrs(trim($matches[1]));
        } else {
            $attr = array();
        }

        $output = $this->wiki->addToken(
            $this->rule,
            array(
                'type' => 'list_start',
                'level' => 0,
                'attr' => $attr
            )
        );

        foreach ($this->wiki->getTokens('Heading2') as $key => $val) {

            if ($val[1]['type'] != 'start') {
                continue;
            }

            $options = array(
                'type'  => 'item_start',
                'id'    => $val[1]['id'],
                'level' => $val[1]['level'],
                'count' => $count ++
            );

            $output .= $this->wiki->addToken($this->rule, $options);

            $output .= $val[1]['text'];

            $output .= $this->wiki->addToken(
                $this->rule,
                array(
                    'type' => 'item_end',
                    'level' => $val[1]['level']
                )
            );
        }

        $output .= $this->wiki->addToken(
            $this->rule, array(
                'type' => 'list_end',
                'level' => 0
            )
        );

        return "\n$output\n";
    }
}