Skip to content

Commit 6db5c1b

Browse files
committed
[TASK] Issue warning if a document is not included in any toctree
Sphinx also issues this kind of warning. Project who do not desire the warning could deactivate the pass resolves TYPO3-Documentation/render-guides#332
1 parent 5a7764d commit 6db5c1b

File tree

9 files changed

+99
-3
lines changed

9 files changed

+99
-3
lines changed

docs/contributions/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
Contributions
55
=============
66

7+
.. toctree::
8+
:glob:
9+
:hidden:
10+
11+
*
12+
13+
714
Clone the mono repository
815
=========================
916

docs/index.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ are currently reading about::
3535

3636
You will then find the rendered documentation in the directory output
3737

38-
.. toctree::
38+
.. toctree::
39+
3940
about
4041
usage
4142
configuration
43+
components/index
4244
extension/index
4345
rst-reference/index
46+
contributions/index

packages/guides/src/Compiler/CompilerContext.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public function getLoggerInformation(): array
7676
if (!isset($this->shadowTree)) {
7777
return [];
7878
}
79+
7980
return [...$this->getDocumentNode()->getLoggerInformation()];
8081
}
8182
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link https://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Guides\Compiler\Passes;
15+
16+
use phpDocumentor\Guides\Compiler\CompilerContext;
17+
use phpDocumentor\Guides\Compiler\CompilerPass;
18+
use phpDocumentor\Guides\Nodes\DocumentNode;
19+
use phpDocumentor\Guides\Nodes\DocumentTree\DocumentEntryNode;
20+
use phpDocumentor\Guides\Nodes\ProjectNode;
21+
use Psr\Log\LoggerInterface;
22+
23+
final class ToctreeValidationPass implements CompilerPass
24+
{
25+
public function __construct(
26+
private readonly LoggerInterface $logger,
27+
) {
28+
}
29+
30+
public function getPriority(): int
31+
{
32+
return 20; // must be run very late
33+
}
34+
35+
/**
36+
* @param DocumentNode[] $documents
37+
*
38+
* @return DocumentNode[]
39+
*/
40+
public function run(array $documents, CompilerContext $compilerContext): array
41+
{
42+
$projectNode = $compilerContext->getProjectNode();
43+
44+
foreach ($documents as $document) {
45+
if (!$this->isMissingInToctree($projectNode->getDocumentEntry($document->getFilePath()), $projectNode) || $document->isOrphan()) {
46+
continue;
47+
}
48+
49+
$this->logger->warning(
50+
'Document "' . $document->getFilePath() . '" isn\'t included in any toctree. Include it in a `.. toctree::` directive or add `:orphan:` in the first line of the rst document',
51+
$document->getLoggerInformation(),
52+
);
53+
}
54+
55+
return $documents;
56+
}
57+
58+
public function isMissingInToctree(DocumentEntryNode $documentEntry, ProjectNode $projectNode): bool
59+
{
60+
return $documentEntry->getParent() === null && $documentEntry !== $projectNode->getRootDocumentEntry();
61+
}
62+
}

tests/Functional/FunctionalTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
use phpDocumentor\Guides\Compiler\Compiler;
2525
use phpDocumentor\Guides\Compiler\CompilerContext;
2626
use phpDocumentor\Guides\NodeRenderers\NodeRenderer;
27+
use phpDocumentor\Guides\Nodes\DocumentTree\DocumentEntryNode;
2728
use phpDocumentor\Guides\Nodes\Node;
2829
use phpDocumentor\Guides\Nodes\ProjectNode;
30+
use phpDocumentor\Guides\Nodes\TitleNode;
2931
use phpDocumentor\Guides\Parser;
3032
use phpDocumentor\Guides\RenderContext;
3133
use phpDocumentor\Guides\Settings\ProjectSettings;
@@ -102,10 +104,13 @@ public function testFunctional(
102104
$parser = $this->getContainer()->get(Parser::class);
103105
assert($parser instanceof Parser);
104106
$document = $parser->parse($rst);
107+
$documentEntry = new DocumentEntryNode($document->getFilePath(), $document->getTitle() ?? TitleNode::fromString(''), true);
105108

106109
$compiler = $this->getContainer()->get(Compiler::class);
107110
assert($compiler instanceof Compiler);
108-
$compiler->run([$document], new CompilerContext(new ProjectNode()));
111+
$projectNode = new ProjectNode();
112+
$projectNode->setDocumentEntries([$documentEntry]);
113+
$compiler->run([$document], new CompilerContext($projectNode));
109114

110115
$inputFilesystem = new Filesystem(new MemoryAdapter());
111116
$inputFilesystem->write('img/test-image.jpg', 'Some image');
@@ -126,7 +131,7 @@ public function testFunctional(
126131
$outfs = new Filesystem(new MemoryAdapter()),
127132
'',
128133
$format,
129-
new ProjectNode(),
134+
$projectNode,
130135
);
131136

132137
$rendered = '';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!-- content start -->
2+
<div class="section" id="index">
3+
<h1>index</h1>
4+
5+
</div>
6+
7+
<!-- content end -->
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
app.WARNING: Document "overview" isn't included in any toctree. Include it in a `.. toctree::` directive or add `:orphan:` in the first line of the rst document {"rst-file":"overview.rst"} []
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!-- content start -->
2+
<div class="section" id="overview">
3+
<h1>Overview</h1>
4+
5+
<p>Lorem Ipsum Dolor</p>
6+
</div>
7+
8+
<!-- content end -->
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
index
2+
=====

0 commit comments

Comments
 (0)