-
Notifications
You must be signed in to change notification settings - Fork 4
/
TableOfContents.php
284 lines (254 loc) · 10.1 KB
/
TableOfContents.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
/**
* Pico Table Of Contents Plugin
*
* @author Iridescent
* @link https://github.com/iridescent-dev/pico-toc-plugin
* @license http://opensource.org/licenses/MIT The MIT License
* @version 2.0
*/
class TableOfContents extends AbstractPicoPlugin
{
protected $config = array(
// Minimum number of headers required.
'min_headers' => 2,
// Minimum level displayed in the table of contents.
'min_level' => 1,
// Maximum level displayed in the table of contents.
'max_level' => 5,
// The tag used for the list: ol (ordered) or ul (unordered).
'tag' => 'ol',
// The css style applied to the list: numbers, bullets, none or default.
'style' => 'none',
// Heading text, if a heading for the table of contents is desired.
'heading' => null,
);
protected $min_headers, $min_level, $max_level, $tag, $style, $heading, $toc_element_xml;
protected $available_tags = ['ol', 'ul'];
protected $available_styles = ['numbers', 'bullets', 'none', 'default'];
/**
* Triggered after Pico has read its configuration
*
* @see Pico::getConfig()
* @see Pico::getBaseUrl()
* @see Pico::isUrlRewritingEnabled()
*
* @param array &$config array of config variables
*/
public function onConfigLoaded(&$config)
{
if (isset($config['TOC'])) {
foreach ($this->config as $key => $val) {
if (isset($config['TOC'][$key])) {
$this->config[$key] = &$config['TOC'][$key];
}
}
// Check if the tag is valid
if (!in_array($this->config['tag'], $this->available_tags)) {
throw new RuntimeException('Invalid TOC tag "' . $this->config['tag'] . '", the possible values are [ ' . implode(', ', $this->available_tags) . ' ].');
}
// Check if the style is valid
if (!in_array($this->config['style'], $this->available_styles)) {
throw new RuntimeException('Invalid TOC style "' . $this->config['style'] . '", the possible values are [ ' . implode(', ', $this->available_styles) . ' ].');
}
}
}
/**
* Triggered when Pico reads its known meta header fields
*
* @see Pico::getMetaHeaders()
*
* @param string[] &$headers list of known meta header fields; the array
* key specifies the YAML key to search for, the array value is later
* used to access the found value
*/
public function onMetaHeaders(array &$headers)
{
$headers['toc'] = 'TOC';
}
/**
* Triggered after Pico has parsed the meta header
*
* @see DummyPlugin::onMetaParsing()
* @see Pico::getFileMeta()
*
* @param string[] &$meta parsed meta data
*/
public function onMetaParsed(array &$meta)
{
// Checks if the language of the page is set and that it is available for the site
$this->min_headers = $this->getVal('min_headers', $meta);
$this->min_level = $this->getVal('min_level', $meta);
$this->max_level = $this->getVal('max_level', $meta);
$this->tag = $this->getVal('tag', $meta);
$this->style = $this->getVal('style', $meta);
$this->heading = $this->getVal('heading', $meta);
// Check if the tag is valid
if (!in_array($this->tag, $this->available_tags)) {
throw new RuntimeException('Invalid tag "' . $this->tag . '", the possible values are [ ' . implode(', ', $this->available_tags) . ' ].');
}
// Check if the style is valid
if (!in_array($this->style, $this->available_styles)) {
throw new RuntimeException('Invalid style "' . $this->style . '", the possible values are [ ' . implode(', ', $this->available_styles) . ' ].');
}
}
/**
* Triggered after Pico has parsed the contents of the file to serve
*
* @see DummyPlugin::onContentParsing()
* @see DummyPlugin::onContentPrepared()
* @see Pico::getFileContent()
*
* @param string &$content parsed contents (HTML) of the requested page
*/
public function onContentParsed(&$content)
{
if (trim($content) === "") {
return;
}
if ($this->min_level > $this->max_level) {
return; // No level to display
}
$document = new DOMDocument('1.0', 'utf-8');
libxml_use_internal_errors(true);
if (!$document->loadHTML('<?xml encoding="utf-8" ?>' . $content)) {
foreach (libxml_get_errors() as $error) {
// handle errors here
}
libxml_clear_errors();
return;
}
// Get the list of headers
$xPathExpression = [];
for ($i = $this->min_level; $i <= $this->max_level; $i++) {
$xPathExpression[] = "//h$i";
}
$xPathExpression = join("|", $xPathExpression);
$domXPath = new DOMXPath($document);
$headers = $domXPath->query($xPathExpression);
if (!$headers || $headers->length < $this->min_headers) {
return; // Not enough header to display
}
// Initialize TOC element
$div_element = $document->createElement('div');
$div_element->setAttribute('id', 'toc');
// Add heading element, if enabled
if (isset($this->heading)) {
$heading_element = $document->createElement('div', $this->heading);
$heading_element->setAttribute('class', 'toc-heading');
$div_element->appendChild($heading_element);
}
// Add the list element
$list_element = $this->getList($document, $headers);
$div_element->appendChild($list_element);
// Replace [toc] in document
$nodes = $domXPath->query('//p');
foreach ($nodes as $node) {
if (trim($node->nodeValue) === "[toc]") {
$node->parentNode->replaceChild($div_element, $node);
break;
}
}
$content = preg_replace(array("/<(!DOCTYPE|\?xml).+?>/", "/<\/?(html|body)>/"), array("", ""), $document->saveHTML());
// Save the TOC element as string
$this->toc_element_xml = $div_element->ownerDocument->saveXML($div_element);
}
/**
* Triggered before Pico renders the page
*
* @see DummyPlugin::onPageRendered()
*
* @param string &$templateName file name of the template
* @param array &$twigVariables template variables
*/
public function onPageRendering(&$templateName, array &$twigVariables)
{
$twigVariables['toc'] = new Twig_Markup($this->toc_element_xml, 'UTF-8');
}
/* ********************************************************************************* */
/**
* Return the value from the key in metadatas if it exists, default config value otherwise.
*
* @param string $key
* @param string[] $meta parsed meta data
* @return string
*/
private function getVal($key, $meta)
{
return (isset($meta['toc']) && isset($meta['toc'][$key])) ? $meta['toc'][$key] : $this->config[$key];
}
/**
* Creates a list element from the headers.
* Function called recursively to create nested lists.
*
* @param DOMDocument $document
* @param DOMNodeList $headers
* @param integer $index
* @return DOMElement
*/
private function getList($document, $headers, &$index = 0)
{
// Initialize ordered list element
$list_element = $document->createElement($this->tag);
if ($this->style !== "default") {
$list_element->setAttribute('class', "toc-$this->style");
}
for ($index; $index < $headers->length; $index++) {
$curr_header = $headers[$index];
if (isset($curr_header->tagName) && $curr_header->tagName !== '') {
// Add missing id's to the h tags
$id = $curr_header->getAttribute('id');
if ($id === "") {
$id = $this->slugify($curr_header->nodeValue);
$curr_header->setAttribute('id', $id);
}
// Initialize the list item with a link to the header
$li_element = $document->createElement('li');
$a_element = $document->createElement('a');
$a_element->setAttribute('href', "#$id");
$a_element->nodeValue = $curr_header->nodeValue;
$li_element->appendChild($a_element);
$next_header = ($index + 1 < $headers->length) ? $headers[$index + 1] : null;
if ($next_header && strtolower($curr_header->tagName) < strtolower($next_header->tagName)) {
// The next header is at a lower level -> add nested headers
$index++;
$nested_list_element = $this->getList($document, $headers, $index);
$li_element->appendChild($nested_list_element);
}
$list_element->appendChild($li_element);
// Refresh next_header with the updated index
$next_header = ($index + 1 < $headers->length) ? $headers[$index + 1] : null;
if ($next_header && strtolower($curr_header->tagName) > strtolower($next_header->tagName)) {
// The next header is at a higher level -> stop current loop
break;
}
}
}
return $list_element;
}
/**
* Generate a slug from a string.
*
* @param string $text
* @return string
*/
private function slugify($text)
{
// replace non letter or digits by -
$text = preg_replace('~[^\pL\d]+~u', '-', $text);
// transliterate
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
// trim
$text = trim($text, '-');
// remove duplicate -
$text = preg_replace('~-+~', '-', $text);
// lowercase
$text = strtolower($text);
if (empty($text)) {
return 'n-a';
}
return $text;
}
}