|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Docs Builder package. |
| 7 | + * (c) Ryan Weaver <[email protected]> |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace SymfonyDocsBuilder\Generator; |
| 13 | + |
| 14 | +use Doctrine\RST\Environment; |
| 15 | +use Doctrine\RST\Meta\MetaEntry; |
| 16 | +use function Symfony\Component\String\u; |
| 17 | + |
| 18 | +/** |
| 19 | + * It encapsulates all the logic needed to generate the full TOC items |
| 20 | + * from the given doc meta entries and provides some TOC utilities. |
| 21 | + */ |
| 22 | +class TocGenerator |
| 23 | +{ |
| 24 | + private $metaEntry; |
| 25 | + private $cachedToc; |
| 26 | + |
| 27 | + public function __construct(MetaEntry $metaEntry) |
| 28 | + { |
| 29 | + $this->metaEntry = $metaEntry; |
| 30 | + } |
| 31 | + |
| 32 | + public function getToc(): array |
| 33 | + { |
| 34 | + if (null !== $this->cachedToc) { |
| 35 | + return $this->cachedToc; |
| 36 | + } |
| 37 | + |
| 38 | + return $this->cachedToc = $this->doGenerateToc($this->metaEntry, current($this->metaEntry->getTitles())[1]); |
| 39 | + } |
| 40 | + |
| 41 | + public function getFlatenedToc(): array |
| 42 | + { |
| 43 | + return $this->doFlattenToc($this->getToc()); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns the number of TOC items per indentation level |
| 48 | + * (which correspond to <h1>, <h2>, <h3>, etc. elements) |
| 49 | + */ |
| 50 | + public function getNumItemsPerLevel(): array |
| 51 | + { |
| 52 | + $numItems = [1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0]; |
| 53 | + foreach ($this->getFlatenedToc() as $tocItem) { |
| 54 | + ++$numItems[$tocItem['level']]; |
| 55 | + } |
| 56 | + |
| 57 | + $numItems['total'] = array_sum($numItems); |
| 58 | + |
| 59 | + return $numItems; |
| 60 | + } |
| 61 | + |
| 62 | + private function doGenerateToc(MetaEntry $metaEntry, ?array $titles, $level = 1): array |
| 63 | + { |
| 64 | + if (null === $titles) { |
| 65 | + return []; |
| 66 | + } |
| 67 | + |
| 68 | + $toc = []; |
| 69 | + foreach ($titles as $title) { |
| 70 | + $toc[] = [ |
| 71 | + 'url' => sprintf('%s#%s', $metaEntry->getUrl(), Environment::slugify($title[0])), |
| 72 | + 'page' => u($metaEntry->getUrl())->beforeLast('.html')->toString(), |
| 73 | + 'fragment' => Environment::slugify($title[0]), |
| 74 | + 'title' => $title[0], |
| 75 | + 'level' => $level, |
| 76 | + 'children' => $this->doGenerateToc($metaEntry, $title[1], $level + 1), |
| 77 | + ]; |
| 78 | + } |
| 79 | + |
| 80 | + return $toc; |
| 81 | + } |
| 82 | + |
| 83 | + private function doFlattenToc(array $toc, array &$flattenedToc = []): array |
| 84 | + { |
| 85 | + foreach ($toc as $item) { |
| 86 | + $flattenedToc[] = $item; |
| 87 | + |
| 88 | + if ([] !== $item['children']) { |
| 89 | + $this->doFlattenToc($item['children'], $flattenedToc); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return $flattenedToc; |
| 94 | + } |
| 95 | +} |
0 commit comments