Skip to content

Commit f0825ac

Browse files
committed
-
1 parent 7b5b9dd commit f0825ac

File tree

6 files changed

+72
-235
lines changed

6 files changed

+72
-235
lines changed

src/Generator/JsonGenerator.php

+2-45
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\DomCrawler\Crawler;
2121
use Symfony\Component\Filesystem\Filesystem;
2222
use SymfonyDocsBuilder\BuildConfig;
23+
use SymfonyDocsBuilder\Twig\TocExtension;
2324
use function Symfony\Component\String\u;
2425

2526
class JsonGenerator
@@ -74,7 +75,7 @@ public function generateJson(string $masterDocument = 'index'): array
7475
'parents' => $this->determineParents($parserFilename, $tocTreeHierarchy) ?: [],
7576
'current_page_name' => $parserFilename,
7677
'toc' => $toc = $this->generateToc($metaEntry, current($metaEntry->getTitles())[1]),
77-
'toc_options' => $this->getTocOptions($toc),
78+
'toc_options' => TocExtension::getOptions($toc),
7879
'next' => $next,
7980
'prev' => $prev,
8081
'body' => $crawler->filter('body')->html(),
@@ -121,50 +122,6 @@ private function generateToc(MetaEntry $metaEntry, ?array $titles, int $level =
121122
return $tocTree;
122123
}
123124

124-
private function getTocOptions(array $toc): array
125-
{
126-
$flattendToc = $this->flattenToc($toc);
127-
$maxDepth = 0;
128-
$numVisibleItems = 0;
129-
foreach ($flattendToc as $tocItem) {
130-
$maxDepth = max($maxDepth, $tocItem['level']);
131-
$numVisibleItems++;
132-
}
133-
134-
return [
135-
'maxDepth' => $maxDepth,
136-
'numVisibleItems' => $numVisibleItems,
137-
'size' => $this->getTocSize($numVisibleItems),
138-
];
139-
}
140-
141-
// If you change this method, make the same change in TocNodeRenderer too
142-
private function getTocSize(int $numVisibleItems): string
143-
{
144-
if ($numVisibleItems < 10) {
145-
return 'md';
146-
}
147-
148-
if ($numVisibleItems < 20) {
149-
return 'lg';
150-
}
151-
152-
return 'xl';
153-
}
154-
155-
private function flattenToc(array $toc, array &$flattenedToc = []): array
156-
{
157-
foreach ($toc as $item) {
158-
$flattenedToc[] = $item;
159-
160-
if ([] !== $item['children']) {
161-
$this->flattenToc($item['children'], $flattenedToc);
162-
}
163-
}
164-
165-
return $flattenedToc;
166-
}
167-
168125
private function determineNext(string $parserFilename, array $flattenedTocTree): ?array
169126
{
170127
$foundCurrentFile = false;

src/KernelFactory.php

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use SymfonyDocsBuilder\Directive as SymfonyDirectives;
1818
use SymfonyDocsBuilder\Reference as SymfonyReferences;
1919
use SymfonyDocsBuilder\Twig\AssetsExtension;
20+
use SymfonyDocsBuilder\Twig\TocExtension;
2021
use function Symfony\Component\String\u;
2122

2223
/**
@@ -55,6 +56,7 @@ static function (string $path) use ($parseSubPath): bool {
5556

5657
$twig = $configuration->getTemplateEngine();
5758
$twig->addExtension(new AssetsExtension());
59+
$twig->addExtension(new TocExtension());
5860

5961
return new DocsKernel(
6062
$buildConfig,

src/Renderers/TocNodeRenderer.php

-179
This file was deleted.

src/SymfonyHTMLFormat.php

-10
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,6 @@ public function getNodeRendererFactories(): array
5454
{
5555
$nodeRendererFactories = $this->htmlFormat->getNodeRendererFactories();
5656

57-
$nodeRendererFactories[TocNode::class] = new CallableNodeRendererFactory(
58-
function (TocNode $node) {
59-
return new Renderers\TocNodeRenderer(
60-
$node->getEnvironment(),
61-
$node,
62-
$this->templateRenderer
63-
);
64-
}
65-
);
66-
6757
$nodeRendererFactories[CodeNode::class] = new CallableNodeRendererFactory(
6858
function (CodeNode $node) {
6959
return new Renderers\CodeNodeRenderer(
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{% apply spaceless %}
2-
<div class="toctree-wrapper toc-size-{{ tocOptions.size }}">
2+
{% set toc_options = toc_options(tocItems) %}
3+
<div class="toctree-wrapper toc-size-{{ toc_options.size }}">
34
{% include "toc-level.html.twig" %}
45
</div>
56
{% endapply %}

src/Twig/TocExtension.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Docs Builder package.
5+
* (c) Ryan Weaver <[email protected]>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace SymfonyDocsBuilder\Twig;
11+
12+
use Twig\Extension\AbstractExtension;
13+
use Twig\TwigFunction;
14+
15+
class TocExtension extends AbstractExtension
16+
{
17+
public function getFunctions(): array
18+
{
19+
return [
20+
new TwigFunction('toc_options', [$this, 'getOptions']),
21+
];
22+
}
23+
24+
public static function getOptions(array $toc): array
25+
{
26+
$flattendToc = self::flattenToc($toc);
27+
$maxDepth = 0;
28+
$numVisibleItems = 0;
29+
foreach ($flattendToc as $tocItem) {
30+
$maxDepth = max($maxDepth, $tocItem['level']);
31+
$numVisibleItems++;
32+
}
33+
34+
return [
35+
'maxDepth' => $maxDepth,
36+
'numVisibleItems' => $numVisibleItems,
37+
'size' => self::getTocSize($numVisibleItems),
38+
];
39+
}
40+
41+
private static function flattenToc(array $toc, array &$flattenedToc = []): array
42+
{
43+
foreach ($toc as $item) {
44+
$flattenedToc[] = $item;
45+
46+
if ([] !== $item['children']) {
47+
self::flattenToc($item['children'], $flattenedToc);
48+
}
49+
}
50+
51+
return $flattenedToc;
52+
}
53+
54+
private static function getTocSize(int $numVisibleItems): string
55+
{
56+
if ($numVisibleItems < 10) {
57+
return 'md';
58+
}
59+
60+
if ($numVisibleItems < 20) {
61+
return 'lg';
62+
}
63+
64+
return 'xl';
65+
}
66+
}

0 commit comments

Comments
 (0)