* */ namespace ComboStrap; use ComboStrap\Meta\Api\Metadata; use ComboStrap\TagAttribute\StyleAttribute; use Doku_Renderer; use DokuWiki_Admin_Plugin; use syntax_plugin_combo_toc; class Toc extends Metadata { const CANONICAL = syntax_plugin_combo_toc::CANONICAL; private ?array $tocData = null; public static function createForRequestedPage(): Toc { return self::createForPage(MarkupPath::createFromRequestedPage()); } public static function getClass(): string { return StyleAttribute::addComboStrapSuffix(self::CANONICAL); } /** * @throws ExceptionBadArgument - if the TOC is not an array * @throws ExceptionNotFound - if the TOC variable was not found */ public static function createFromGlobalVariable(): Toc { global $TOC; if ($TOC === null) { throw new ExceptionNotFound("No global TOC variable found"); } return (new Toc()) ->setValue($TOC); } public static function createEmpty(): Toc { return new Toc(); } public function toXhtml(): string { $this->buildCheck(); if ($this->tocData === null) { return ""; } PluginUtility::getSnippetManager()->attachCssInternalStyleSheet(self::CANONICAL); $toc = $this->tocData; $tocMinHeads = Site::getTocMinHeadings(); if (count($toc) < $tocMinHeads) { return ""; } /** * Adding toc number style */ try { $css = Outline::getCssNumberingRulesFor(Outline::TOC_NUMBERING); PluginUtility::getSnippetManager()->attachCssInternalStyleSheet(Outline::TOC_NUMBERING, $css); } catch (ExceptionNotEnabled $e) { // not enabled } catch (ExceptionBadSyntax $e) { LogUtility::error("The toc numbering type was unknown", self::CANONICAL); } /** * Creating the html */ global $lang; // To keep track of the HTML level (levels may be badly encoded) $htmlLevel = 0; $previousLevel = 0; $topTocLevel = Site::getTopTocLevel(); $ulMarkup = ""; foreach ($toc as $tocItem) { $actualLevel = $tocItem["level"]; /** * Skipping to the first top level */ if ($actualLevel < $topTocLevel) { $previousLevel = $actualLevel; continue; } /** * Closing */ $levelDiff = $previousLevel - $actualLevel; switch (true) { case $levelDiff === 0 && (!empty($ulMarkup)): /** * Same level */ $ulMarkup .= ""; break; case ($actualLevel < $previousLevel && !empty($ulMarkup)): /** * One or multiple level up * (from 4 to 2) */ $htmlLevel += $levelDiff; $ulMarkup .= str_repeat("", $levelDiff); $ulMarkup .= ""; break; default: /** * One level down * (We can't go multiple at once) */ $htmlLevel -= 1; $ulMarkup .= "