Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@

namespace phpDocumentor\Guides\RestructuredText\Directives;

use phpDocumentor\Guides\Compiler\CompilerContextInterface;
use phpDocumentor\Guides\Nodes\Menu\ContentMenuNode;
use phpDocumentor\Guides\Nodes\Menu\SectionMenuEntryNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\ReferenceResolvers\DocumentNameResolverInterface;
use phpDocumentor\Guides\RestructuredText\Directives\Attributes\Directive;
use phpDocumentor\Guides\RestructuredText\Directives\Attributes\Option;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;
use phpDocumentor\Guides\RestructuredText\Nodes\DirectiveNode;

/**
* Standarad rst `contents` directive
* Standard rst `contents` directive
*
* Displays a table of content of the current page
*/
#[Directive(name: 'contents')]
#[Option(name: 'local', type: OptionType::Boolean, description: 'If set, the table of contents will only include sections that are local to the current document.', default: false)]
#[Option(name: 'depth', description: 'The maximum depth of the table of contents.')]
final class ContentsDirective extends BaseDirective
Expand All @@ -35,25 +37,17 @@ public function __construct(
) {
}

public function getName(): string
public function createNode(DirectiveNode $directiveNode, CompilerContextInterface $compilerContext): Node
{
return 'contents';
}

/** {@inheritDoc} */
public function process(
BlockContext $blockContext,
Directive $directive,
): Node {
$options = $directive->getOptions();
$directive = $directiveNode->getDirective();
$absoluteUrl = $this->documentNameResolver->absoluteUrl(
$blockContext->getDocumentParserContext()->getContext()->getDirName(),
$blockContext->getDocumentParserContext()->getContext()->getCurrentFileName(),
(string) $directiveNode->getSourceLocation()->documentDirectory,
(string) $directiveNode->getSourceLocation()->file,
);

return (new ContentMenuNode([new SectionMenuEntryNode($absoluteUrl)]))
->withOptions($this->optionsToArray($options))
->withOptions($this->readAllOptions($directive))
->withCaption($directive->getDataNode())
->withLocal($directive->getOptionBool('local'));
->withLocal((bool) $this->readOption($directive, 'local'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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\RestructuredText\Directives;

use phpDocumentor\Guides\Compiler\CompilerContextInterface;
use phpDocumentor\Guides\Nodes\Menu\ContentMenuNode;
use phpDocumentor\Guides\Nodes\Menu\SectionMenuEntryNode;
use phpDocumentor\Guides\ReferenceResolvers\DocumentNameResolverInterface;
use phpDocumentor\Guides\RestructuredText\Nodes\DirectiveNode;
use phpDocumentor\Guides\RestructuredText\Nodes\DirectiveSourceLocation;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;
use phpDocumentor\Guides\RestructuredText\Parser\DirectiveOption;
use PHPUnit\Framework\TestCase;

final class ContentsDirectiveTest extends TestCase
{
public function testCreatesContentMenuForTheCurrentDocument(): void
{
$documentNameResolver = self::createMock(DocumentNameResolverInterface::class);
$documentNameResolver
->expects(self::once())
->method('absoluteUrl')
->with('docs', 'docs/current')
->willReturn('docs/current');

$directiveNode = new DirectiveNode(new Directive('', 'contents', '', [
'local' => new DirectiveOption('local', null),
'depth' => new DirectiveOption('depth', '2'),
]));
$directiveNode->setSourceLocation(new DirectiveSourceLocation(
file: 'docs/current',
documentDirectory: 'docs',
));

$node = (new ContentsDirective($documentNameResolver))->createNode(
$directiveNode,
self::createStub(CompilerContextInterface::class),
);

self::assertInstanceOf(ContentMenuNode::class, $node);
self::assertCount(1, $node->getMenuEntries());

$firstMenuEntry = $node->getMenuEntries()[0];
self::assertInstanceOf(SectionMenuEntryNode::class, $firstMenuEntry);
self::assertSame('/docs/current', $firstMenuEntry->getDocumentLink());
self::assertTrue($node->isLocal());
self::assertSame(2, $node->getDepth());
}
}
6 changes: 0 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,6 @@ parameters:
count: 1
path: packages/guides-restructured-text/src/RestructuredText/Directives/CodeBlockDirective.php

-
message: '#^phpDocumentor\\Guides\\RestructuredText\\Directives\\ContentsDirective should apply the attribute phpDocumentor\\Guides\\RestructuredText\\Directives\\Attributes\\Directive$#'
identifier: phpat.testConcreteDirectivesHaveTheDirectiveAttribute
count: 1
path: packages/guides-restructured-text/src/RestructuredText/Directives/ContentsDirective.php

-
message: '#^phpDocumentor\\Guides\\RestructuredText\\Directives\\CsvTableDirective should apply the attribute phpDocumentor\\Guides\\RestructuredText\\Directives\\Attributes\\Directive$#'
identifier: phpat.testConcreteDirectivesHaveTheDirectiveAttribute
Expand Down
Loading