Rev 6 | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo file_get_contents("snippets/" . $_POST['page'] . "_head.html");
$str = file_get_contents("snippets/" . $_POST['page'] . ".html");
echo buildTOC($str);
echo $str;
}
function buildTOC($str) {
include('php/simple_html_dom.php');
$html = str_get_html($str);
$toc = [];
foreach($html->find('h3[data-toc]') as $element) {
$entry = "<li><a href=\"#" . $element->{'id'} . "\">" . $element->innertext . "</a></li>";
if (empty($toc[$element->{'data-toc'}])) { $toc[$element->{'data-toc'}] = []; }
$toc[$element->{'data-toc'}][$element->innertext] = $entry;
}
ksort($toc);
$tocStr = "";
if (!empty($toc)) {
$tocStr .= "<div class=\"container-fluid py-3\">";
$tocStr .= "<button class=\"collapsible\">Table of Contents</button>";
$tocStr .= "<div class=\"tocContent\">";
foreach($toc as $key => $value) {
$tocStr .= "<p><strong>" . $key . "</strong></p><ul>";
ksort($toc[$key]);
foreach($toc[$key] as $entry) {
$tocStr .= "$entry" . PHP_EOL;
}
$tocStr .= "</ul>";
}
$tocStr .= "</div></div><hr>";
}
return $tocStr;
}