| 6 |
- |
1 |
<?php
|
|
|
2 |
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
| 8 |
- |
3 |
echo file_get_contents("snippets/" . $_POST['page'] . "_head.html");
|
|
|
4 |
$str = file_get_contents("snippets/" . $_POST['page'] . ".html");
|
|
|
5 |
echo buildTOC($str);
|
|
|
6 |
echo $str;
|
|
|
7 |
}
|
|
|
8 |
|
|
|
9 |
function buildTOC($str) {
|
|
|
10 |
include('php/simple_html_dom.php');
|
|
|
11 |
|
|
|
12 |
$html = str_get_html($str);
|
|
|
13 |
$toc = [];
|
|
|
14 |
|
|
|
15 |
foreach($html->find('h3[data-toc]') as $element) {
|
|
|
16 |
$entry = "<li><a href=\"#" . $element->{'id'} . "\">" . $element->innertext . "</a></li>";
|
|
|
17 |
if (empty($toc[$element->{'data-toc'}])) { $toc[$element->{'data-toc'}] = []; }
|
|
|
18 |
$toc[$element->{'data-toc'}][$element->innertext] = $entry;
|
|
|
19 |
}
|
|
|
20 |
|
|
|
21 |
ksort($toc);
|
|
|
22 |
|
|
|
23 |
$tocStr = "";
|
|
|
24 |
if (!empty($toc)) {
|
|
|
25 |
$tocStr .= "<div class=\"container-fluid py-3\">";
|
|
|
26 |
$tocStr .= "<button class=\"collapsible\">Table of Contents</button>";
|
|
|
27 |
$tocStr .= "<div class=\"tocContent\">";
|
|
|
28 |
foreach($toc as $key => $value) {
|
|
|
29 |
$tocStr .= "<p><strong>" . $key . "</strong></p><ul>";
|
|
|
30 |
ksort($toc[$key]);
|
|
|
31 |
foreach($toc[$key] as $entry) {
|
|
|
32 |
$tocStr .= "$entry" . PHP_EOL;
|
|
|
33 |
}
|
|
|
34 |
$tocStr .= "</ul>";
|
|
|
35 |
}
|
|
|
36 |
$tocStr .= "</div></div><hr>";
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
return $tocStr;
|
|
|
40 |
}
|