Finished multi-level TOC menu generation logic.
Этот коммит содержится в:
родитель
b118de1e28
Коммит
ea4ac1c8df
@ -6,7 +6,6 @@
|
|||||||
namespace TOC;
|
namespace TOC;
|
||||||
|
|
||||||
use Knp\Menu\MenuFactory;
|
use Knp\Menu\MenuFactory;
|
||||||
use Knp\Menu\MenuItem;
|
|
||||||
use Sunra\PhpSimple\HtmlDomParser;
|
use Sunra\PhpSimple\HtmlDomParser;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
|
|
||||||
@ -81,37 +80,44 @@ class TocGenerator
|
|||||||
// Extract items
|
// Extract items
|
||||||
|
|
||||||
// Initial settings
|
// Initial settings
|
||||||
$lastLevel = 0;
|
$lastElem = $menu;
|
||||||
$parent = $menu;
|
|
||||||
|
|
||||||
// Do it...
|
// Do it...
|
||||||
foreach ($parsed->find(implode(', ', $tagsToMatch)) as $element) {
|
foreach ($parsed->find(implode(', ', $tagsToMatch)) as $element) {
|
||||||
|
|
||||||
|
// Skip items without IDs
|
||||||
if ( ! $element->id) {
|
if ( ! $element->id) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the tagname and the level
|
// Get the TagName and the level
|
||||||
$tagName = $element->tag;
|
$tagName = $element->tag;
|
||||||
$level = array_search(strtolower($tagName), $tagsToMatch);
|
$level = array_search(strtolower($tagName), $tagsToMatch) + 1;
|
||||||
|
|
||||||
// Determine parent item to which to add child
|
// TEST DEBUG
|
||||||
|
var_dump($element->plaintext . '; ' . $tagName . ' is level ' . $level . ' and lastLevel is ' . $lastElem->getLevel());
|
||||||
|
|
||||||
|
// Determine parent item which to add child
|
||||||
if ($level == 0) {
|
if ($level == 0) {
|
||||||
$parent = $menu;
|
$parent = $menu;
|
||||||
}
|
}
|
||||||
elseif ($level < $lastLevel) { // traverse up parents until difference between is 1
|
elseif ($level == $lastElem->getLevel()) {
|
||||||
for ($l = $lastLevel; $l > $level; $l--) {
|
$parent = $lastElem->getParent();
|
||||||
$parent = $parent->getParent();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
elseif ($level > $lastLevel) { // add children until difference between is 1
|
elseif ($level > $lastElem->getLevel()) {
|
||||||
for ($l = $lastLevel; $l < ($level-1); $l++) {
|
$parent = $lastElem;
|
||||||
|
for ($i = $lastElem->getLevel(); $i < ($level - 1); $i++) {
|
||||||
$parent = $parent->addChild('');
|
$parent = $parent->addChild('');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else { //if ($level < $lastElem->getLevel())
|
||||||
|
$parent = $lastElem->getParent();
|
||||||
|
while ($parent->getLevel() > $level - 1) {
|
||||||
|
$parent = $parent->getParent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$parent->addChild($element->title ?: $element->plaintext, ['uri' => '#' . $element->id]);
|
$lastElem = $parent->addChild($element->title ?: $element->plaintext, ['uri' => '#' . $element->id]);
|
||||||
$lastLevel = $level;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $menu;
|
return $menu;
|
||||||
|
Загрузка…
Ссылка в новой задаче
Block a user