-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPluginToc.php
85 lines (73 loc) · 2.13 KB
/
PluginToc.php
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
<?php
class PluginToc extends WikiPlugin
{
public function executePhaseOne()
{
if (!isset($this->parameters['skip'])) {
return FALSE;
}
$parent = end($this->parents);
$parent->attr['toc-skip'] = TRUE;
$this->parser->removeTag($parent, $this->tag);
return TRUE;
}
public function executePhaseTwo()
{
$tag = $this->tag;
$parser = $this->parser;
$bare = isset($this->parameters['bare']);
$parser->changeTag($this->parents, $tag, 'div');
$css_class = isset($this->parameters['class']) ? $this->parameters['class'] : 'sidebar';
if (isset($tag->attr['class'])) {
$tag->attr['class'] .= ' ';
} else {
$tag->attr['class'] = '';
}
$tag->attr['class'] .= $css_class;
$new_heading = $parser->openTag('heading', array('toc-skip' => TRUE), array('2', '2'));
$parser->handleString(isset($this->parameters['heading']) ? $this->parameters['heading'] : 'Contents');
$parser->closeTag('heading');
$parser->generateId($new_heading);
$iter = new ParserIterator($this->parser, 'inclusive', 'heading');
$last_level = 1;
foreach ($iter as $heading) {
if ($heading->data[0] == 1 || isset($heading->attr['toc-skip'])) {
unset($heading->attr['toc-skip']);
continue;
}
$level_changes = 0;
while ($last_level > $heading->data[0]) {
$parser->closeTag('li');
$parser->closeTag('ul');
$last_level--;
}
while ($last_level < $heading->data[0]) {
// Enabling this will generate valid HTML, but the bullets
// will look funny if one heading level is skipped
/*if ($level_changes) {
$parser->openTag('li');
}*/
$parser->openTag('ul');
$last_level++;
$level_changes++;
}
if (!$level_changes) {
$parser->closeTag('li');
}
$parser->openTag('li');
$parser->openTag('link', array('href' => '#' . $heading->attr['id']));
$parser->handleString(rtrim($parser->captureText($heading)));
$parser->closeTag('link');
}
while ($last_level > 1) {
$parser->closeTag('li');
$parser->closeTag('ul');
$last_level--;
}
$parser->closeTag('ul');
if ($bare) {
$parser->unwrapChildren(end($this->parents), $tag);
}
return TRUE;
}
}