Skip to content

Commit 991e017

Browse files
committed
Register Document Entries
1 parent d2dc8e4 commit 991e017

File tree

17 files changed

+193
-281
lines changed

17 files changed

+193
-281
lines changed

packages/guides-restructured-text/src/RestructuredText/Toc/ToctreeBuilder.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,7 @@ public function buildToctreeFiles(
3232
$toctreeFiles = [];
3333

3434
foreach ($this->parseToctreeFiles($lines) as $file) {
35-
if ($this->isGlob($options, $file)) {
36-
$globPattern = $file;
37-
38-
$globFiles = $this->globSearcher
39-
->globSearch($parserContext, $globPattern);
40-
41-
foreach ($globFiles as $globFile) {
42-
// if glob finds a file already explicitly defined
43-
// don't duplicate it in the toctree again
44-
if (in_array($globFile, $toctreeFiles, true)) {
45-
continue;
46-
}
47-
48-
$toctreeFiles[] = $globFile;
49-
}
50-
} else {
51-
$absoluteUrl = $this->urlGenerator->absoluteUrl(
52-
$parserContext->getDirName(),
53-
$file,
54-
);
55-
56-
$toctreeFiles[] = $absoluteUrl;
57-
}
35+
$toctreeFiles[] = $file;
5836
}
5937

6038
return $toctreeFiles;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Guides\Compiler\NodeTransformers;
6+
7+
use phpDocumentor\Guides\Compiler\CompilerContext;
8+
use phpDocumentor\Guides\Compiler\NodeTransformer;
9+
use phpDocumentor\Guides\Nodes\DocumentNode;
10+
use phpDocumentor\Guides\Nodes\DocumentTree\DocumentEntryNode;
11+
use phpDocumentor\Guides\Nodes\Node;
12+
use phpDocumentor\Guides\Nodes\TitleNode;
13+
use Psr\Log\LoggerInterface;
14+
15+
/**
16+
* @implements NodeTransformer<Node>
17+
*
18+
*/
19+
class DocumentEntryRegistrationTransformer implements NodeTransformer
20+
{
21+
public function __construct(
22+
private readonly LoggerInterface $logger,
23+
) {
24+
}
25+
26+
public function enterNode(Node $node, CompilerContext $compilerContext): Node
27+
{
28+
return $node;
29+
}
30+
31+
public function leaveNode(Node $node, CompilerContext $compilerContext): Node|null
32+
{
33+
if (!$node instanceof DocumentNode) {
34+
return $node;
35+
}
36+
37+
if ($node->getTitle() === null) {
38+
$this->logger->warning('Document has not title', $node->getLoggerInformation());
39+
}
40+
41+
$entry = new DocumentEntryNode($node->getFilePath(), $node->getTitle()??TitleNode::emptyNode());
42+
$compilerContext->getProjectNode()->addDocumentEntry($entry);
43+
44+
return $node->withDocumentEntry($entry);
45+
}
46+
47+
public function supports(Node $node): bool
48+
{
49+
return $node instanceof DocumentNode;
50+
}
51+
52+
public function getPriority(): int
53+
{
54+
// Before MenuNodeTransformer
55+
return 5000;
56+
}
57+
}

packages/guides/src/Compiler/NodeTransformers/MenuNodeTransformer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
<?php
23

34
declare(strict_types=1);

packages/guides/src/Compiler/Passes/MetasPass.php

Lines changed: 0 additions & 60 deletions
This file was deleted.

packages/guides/src/Nodes/DocumentNode.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
namespace phpDocumentor\Guides\Nodes;
1515

1616
use phpDocumentor\Guides\Meta\FootnoteTarget;
17+
use phpDocumentor\Guides\Nodes\DocumentTree\DocumentEntryNode;
18+
use phpDocumentor\Guides\Nodes\DocumentTree\SectionEntryNode;
1719
use phpDocumentor\Guides\Nodes\Metadata\MetadataNode;
1820

1921
use function array_filter;
@@ -61,6 +63,8 @@ final class DocumentNode extends CompoundNode
6163
private bool $titleFound = false;
6264

6365
private string|null $metaTitle = null;
66+
private DocumentEntryNode|null $documentEntry = null;
67+
private SectionEntryNode|null $rootSectionEntry = null;
6468

6569
public function __construct(
6670
private readonly string $hash,
@@ -240,4 +244,29 @@ public function getFootnoteTargetAnonymous(): FootnoteTarget|null
240244

241245
return null;
242246
}
247+
public function getDocumentEntry(): DocumentEntryNode|null
248+
{
249+
return $this->documentEntry;
250+
}
251+
252+
public function withDocumentEntry(DocumentEntryNode $documentEntry): DocumentNode
253+
{
254+
$node = clone($this);
255+
$node->documentEntry = $documentEntry;
256+
return $node;
257+
}
258+
259+
/**
260+
* @return SectionEntryNode|null
261+
*/
262+
public function getRootSectionEntry(): ?SectionEntryNode
263+
{
264+
return $this->rootSectionEntry;
265+
}
266+
267+
public function setRootSectionEntry(?SectionEntryNode $rootSectionEntry): void
268+
{
269+
$this->rootSectionEntry = $rootSectionEntry;
270+
}
271+
243272
}

packages/guides/src/Nodes/TitleNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct(InlineCompoundNode $value, protected int $level, pro
2727

2828
public static function emptyNode(): self
2929
{
30-
return new TitleNode(new InlineCompoundNode([new PlainTextInlineNode('<Unknown>')]), 0, '');
30+
return new TitleNode(new InlineCompoundNode([new PlainTextInlineNode('<Unknown>')]), 1, '');
3131
}
3232

3333
public function getLevel(): int
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace phpDocumentor\Guides\Compiler\NodeTransformers;
4+
5+
use Monolog\Logger;
6+
use phpDocumentor\Guides\Compiler\CompilerContext;
7+
use phpDocumentor\Guides\Nodes\ClassNode;
8+
use phpDocumentor\Guides\Nodes\DocumentNode;
9+
use phpDocumentor\Guides\Nodes\DocumentTree\DocumentEntryNode;
10+
use phpDocumentor\Guides\Nodes\ProjectNode;
11+
use phpDocumentor\Guides\Nodes\SectionNode;
12+
use phpDocumentor\Guides\Nodes\TitleNode;
13+
use PHPUnit\Framework\MockObject\Invocation;
14+
use PHPUnit\Framework\TestCase;
15+
use Psr\Log\LoggerInterface;
16+
17+
class DocumentEntryRegistrationTransformerTest extends TestCase
18+
{
19+
private CompilerContext $context;
20+
21+
protected function setUp(): void
22+
{
23+
$this->context = self::getCompilerContext('some/path');
24+
}
25+
26+
private static function getCompilerContext(string $path): CompilerContext
27+
{
28+
$context = new CompilerContext(new ProjectNode());
29+
30+
return $context->withShadowTree(new DocumentNode('123', $path));
31+
}
32+
33+
public function testLeaveNodeWillReturnDocumentNodeWithEntry(): void
34+
{
35+
$node = new DocumentNode('', '');
36+
$node->setValue([new SectionNode(TitleNode::emptyNode())]);
37+
$mockLogger = $this->createMock(LoggerInterface::class);
38+
$mockLogger->expects($this->never())->method('warning');
39+
$mockLogger->expects($this->never())->method('error');
40+
41+
$transformer = new DocumentEntryRegistrationTransformer($mockLogger);
42+
43+
$result = $transformer->leaveNode($node, $this->context);
44+
self::assertInstanceOf(DocumentNode::class, $result);
45+
self::assertInstanceOf(DocumentEntryNode::class, $result->getDocumentEntry());
46+
}
47+
48+
public function testDocumentWithoutTitleCausesWarning(): void
49+
{
50+
$node = new DocumentNode('', '');
51+
$mockLogger = $this->createMock(LoggerInterface::class);
52+
$mockLogger->expects($this->once())->method('warning');
53+
54+
$transformer = new DocumentEntryRegistrationTransformer($mockLogger);
55+
56+
$result = $transformer->leaveNode($node, $this->context);
57+
self::assertInstanceOf(DocumentNode::class, $result);
58+
self::assertInstanceOf(DocumentEntryNode::class, $result->getDocumentEntry());
59+
}
60+
}

0 commit comments

Comments
 (0)