forked from d3designs/mdocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoc.php
196 lines (154 loc) · 3.74 KB
/
toc.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* Create a Table of Contents based off the specified HTML
*
* Example:
*
* $toc = new TOC( $html );
* echo $toc->generate();
*
*
* @author Jay Williams
*/
class TOC
{
/**
* The HTML to scan when creating the Table of Contents
*
* @var string
*/
var $content = "";
/**
* The the raw Table of Contents array is stored here
*
* @var array
* @access private
*/
var $toc = array();
/**
* The maximum header level to include in the TOC
*
* @var int
*/
var $maxlevel = 1;
/**
* The minimum header level to include in the TOC
*
* @var int
*/
var $minlevel = 2;
/**
* All headers will be split based off of this delimiter.
* If you don't want to split the headers, simply set this variable to false, or empty.
*
* @var string
*/
var $delimiter = ':';
/**
* Headers will be trimmed based off of this variable.
* If you don't want headers to be trimmed, simply set this variable to false, or empty.
*
* @var string
*/
var $trim = '$ ';
var $html;
function __construct($content='',$maxlevel=1,$minlevel=2)
{
if (empty($content))
return false;
$this->content = & $content;
$this->maxlevel = & $maxlevel;
$this->minlevel = & $minlevel;
return true;
}
function __toString()
{
return $this->html;
}
function scan()
{
$regex = '#<h(['."$this->maxlevel-$this->minlevel".'])(.*?id=("|\')([\w\:\-_]+)\3.*?)?>(.*?)</h\1>#';
preg_replace_callback($regex, array(&$this, 'add'), $this->content);
}
function add($match)
{
$this->toc[] = array(
'level'=>(int)$match[1],
'id'=>$match[4],
'text'=>$match[5]
);
}
function generate()
{
if (count($this->toc) < 1) {
$this->scan();
}
return $this->output();
}
function output_list()
{
$this->html = "";
$toc_total = count($this->toc);
for ($i = 0; $i < $toc_total; $i ++) {
$level = & $this->toc[$i]['level'];
$id = & $this->toc[$i]['id'];
$text = $item['text'];
if (!empty($this->delimiter))
$text = end(explode($this->delimiter,$text));
if (!empty($this->trim))
$text = trim($text,$this->trim);
$link = "<a href=\"#$id\">$text</a>";
if ($i == 0) {
$level = min($level, $this->minlevel);
$stack = array($level);
$this->html .= "\t<ol><li>$link";
} else {
$prev = $stack[count($stack)-1];
if ($level == $prev) {
$this->html .= "</li>\n\t<li>$link";
} elseif ($level < $prev) {
while (count($stack) > 1) {
array_pop($stack);
$this->html .= "</li></ol>";
$prev = $stack[count($stack)-1];
if ($level >= $prev)
break;
}
$this->html .= "</li>\n\t<li>$link";
} else {
$stack[] = $level;
$this->html .= "\n\t<ol><li>$link";
}
}
}
while (count($stack) > 0) {
array_pop($stack);
$this->html .= "</li></ol>";
}
return $this->html;
}
function output()
{
$this->html = "";
foreach ($this->toc as $item) {
$level = & $item['level'];
$id = & $item['id'];
$text = & $item['text'];
if (!empty($this->delimiter))
$text = end(explode($this->delimiter,$text));
if (!empty($this->trim))
$text = trim($text,$this->trim);
$link = "<a href=\"#$id\">$text</a>";
if ($level == $this->maxlevel) {
$this->html .= "<h4>$link</h4>\n";
}else {
$this->html .= "\t<div class=\"menu-item\">$link</div>\n";
}
}
return $this->html;
}
function getHtml() {
return $this->html;
}
}
?>