Skip to content

[TASK] Issue warning if a document is not included in any toctree #846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/contributions/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
Contributions
=============

.. toctree::
:glob:
:hidden:

*


Clone the mono repository
=========================

Expand Down
5 changes: 4 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ are currently reading about::

You will then find the rendered documentation in the directory output

.. toctree::
.. toctree::

about
usage
configuration
components/index
extension/index
rst-reference/index
contributions/index
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function parse(ParserContext $parserContext, string $contents): DocumentN
private function parseDocument(NodeWalker $walker, string $hash): DocumentNode
{
$document = new DocumentNode($hash, ltrim($this->getParserContext()->getCurrentAbsolutePath(), '/'));
$document->setOrphan(true);
$this->document = $document;

while ($event = $walker->next()) {
Expand Down
4 changes: 4 additions & 0 deletions packages/guides/src/Compiler/CompilerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public function getShadowTree(): TreeNode
/** @return array<string, string> */
public function getLoggerInformation(): array
{
if (!isset($this->shadowTree)) {
return [];
}

return [...$this->getDocumentNode()->getLoggerInformation()];
}
}
62 changes: 62 additions & 0 deletions packages/guides/src/Compiler/Passes/ToctreeValidationPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

namespace phpDocumentor\Guides\Compiler\Passes;

use phpDocumentor\Guides\Compiler\CompilerContext;
use phpDocumentor\Guides\Compiler\CompilerPass;
use phpDocumentor\Guides\Nodes\DocumentNode;
use phpDocumentor\Guides\Nodes\DocumentTree\DocumentEntryNode;
use phpDocumentor\Guides\Nodes\ProjectNode;
use Psr\Log\LoggerInterface;

final class ToctreeValidationPass implements CompilerPass
{
public function __construct(
private readonly LoggerInterface $logger,
) {
}

public function getPriority(): int
{
return 20; // must be run very late
}

/**
* @param DocumentNode[] $documents
*
* @return DocumentNode[]
*/
public function run(array $documents, CompilerContext $compilerContext): array
{
$projectNode = $compilerContext->getProjectNode();

foreach ($documents as $document) {
if (!$this->isMissingInToctree($projectNode->getDocumentEntry($document->getFilePath()), $projectNode) || $document->isOrphan()) {
continue;
}

$this->logger->warning(
'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',
$document->getLoggerInformation(),
);
}

return $documents;
}

public function isMissingInToctree(DocumentEntryNode $documentEntry, ProjectNode $projectNode): bool
{
return $documentEntry->getParent() === null && $documentEntry !== $projectNode->getRootDocumentEntry();
}
}
9 changes: 7 additions & 2 deletions tests/Functional/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
use phpDocumentor\Guides\Compiler\Compiler;
use phpDocumentor\Guides\Compiler\CompilerContext;
use phpDocumentor\Guides\NodeRenderers\NodeRenderer;
use phpDocumentor\Guides\Nodes\DocumentTree\DocumentEntryNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\Nodes\ProjectNode;
use phpDocumentor\Guides\Nodes\TitleNode;
use phpDocumentor\Guides\Parser;
use phpDocumentor\Guides\RenderContext;
use phpDocumentor\Guides\Settings\ProjectSettings;
Expand Down Expand Up @@ -102,10 +104,13 @@ public function testFunctional(
$parser = $this->getContainer()->get(Parser::class);
assert($parser instanceof Parser);
$document = $parser->parse($rst);
$documentEntry = new DocumentEntryNode($document->getFilePath(), $document->getTitle() ?? TitleNode::fromString(''), true);

$compiler = $this->getContainer()->get(Compiler::class);
assert($compiler instanceof Compiler);
$compiler->run([$document], new CompilerContext(new ProjectNode()));
$projectNode = new ProjectNode();
$projectNode->setDocumentEntries([$documentEntry]);
$compiler->run([$document], new CompilerContext($projectNode));

$inputFilesystem = new Filesystem(new MemoryAdapter());
$inputFilesystem->write('img/test-image.jpg', 'Some image');
Expand All @@ -126,7 +131,7 @@ public function testFunctional(
$outfs = new Filesystem(new MemoryAdapter()),
'',
$format,
new ProjectNode(),
$projectNode,
);

$rendered = '';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
:orphan:


========
Overview
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
:orphan:

.. _rst-overview:

Overview
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
:orphan:

.. _sphinx-overview:

Overview
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
:orphan:

.. _rst-overview:

Overview
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
:orphan:

.. _rst-overview:

RST Overview
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
:orphan:

.. _sphinx-overview:

Sphinx Overview
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
:orphan:

==============
Document Title
==============
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
:orphan:

======
Page 1
======
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
:orphan:

Some File
==========

Expand Down
2 changes: 2 additions & 0 deletions tests/Integration/tests/hyperlink-to-page/input/page1.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
:orphan:

Page 1
======
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
:orphan:

.. _page1:

Some file
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
:orphan:

Subpage 1
=========
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@ An image with relative paths
:alt: Alternative text

Description


.. toctree::
:hidden:
:glob:

subfolder/*
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ An image with relative paths
:alt: Alternative text

Description

.. toctree::
:hidden:
:glob:

subfolder/*
7 changes: 7 additions & 0 deletions tests/Integration/tests/images/image-absolute/input/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,10 @@ An image with relative paths
.. image:: images/hero2-illustration.svg
:width: 400
:alt: Alternative text


.. toctree::
:hidden:
:glob:

subfolder/*
6 changes: 6 additions & 0 deletions tests/Integration/tests/images/image-relative/input/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ An image with relative paths
.. image:: images/hero2-illustration.svg
:width: 400
:alt: Alternative text

.. toctree::
:hidden:
:glob:

subfolder/*
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ An image with relative paths
.. image:: ../images/hero2-illustration.svg
:width: 400
:alt: Alternative text

.. toctree::
:hidden:
:glob:

subfolder/*

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
:orphan:

======
A Page
======
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
:orphan:

===============
Subfolder index
===============
Expand Down
2 changes: 2 additions & 0 deletions tests/Integration/tests/navigation/references/input/page.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
:orphan:

Page 1
*******

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
:orphan:

===============
Subfolder index
===============
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!-- content start -->
<div class="section" id="index">
<h1>index</h1>

</div>

<!-- content end -->
Original file line number Diff line number Diff line change
@@ -0,0 +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"} []
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- content start -->
<div class="section" id="overview">
<h1>Overview</h1>

<p>Lorem Ipsum Dolor</p>
</div>

<!-- content end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
index
=====
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Overview
========

Lorem Ipsum Dolor