From ed5812cd81cb7899c50b1970d210175adba39b81 Mon Sep 17 00:00:00 2001 From: "lina.wolf" Date: Mon, 26 Feb 2024 08:09:52 +0100 Subject: [PATCH] [FEATURE] Introduce NavigationTitle --- .../config/guides-restructured-text.php | 4 + .../NavigationTitleFieldListItemRule.php | 34 ++++++ .../DocumentEntryRegistrationTransformer.php | 2 +- .../InternalMenuEntryNodeTransformer.php | 3 +- packages/guides/src/Nodes/DocumentNode.php | 20 ++++ .../Nodes/DocumentTree/DocumentEntryNode.php | 11 ++ .../Nodes/Metadata/NavigationTitleNode.php | 26 +++++ .../expected/anotherPage.html | 110 ++++++++++++++++++ .../expected/index.html | 110 ++++++++++++++++++ .../expected/somePage.html | 110 ++++++++++++++++++ .../input/anotherPage.rst | 7 ++ .../input/guides.xml | 8 ++ .../input/include.rst.txt | 6 + .../input/index.rst | 19 +++ .../input/somePage.rst | 7 ++ .../input/yetAnotherPage.rst | 9 ++ 16 files changed, 484 insertions(+), 2 deletions(-) create mode 100644 packages/guides-restructured-text/src/RestructuredText/Parser/Productions/FieldList/NavigationTitleFieldListItemRule.php create mode 100644 packages/guides/src/Nodes/Metadata/NavigationTitleNode.php create mode 100644 tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/expected/anotherPage.html create mode 100644 tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/expected/index.html create mode 100644 tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/expected/somePage.html create mode 100644 tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/anotherPage.rst create mode 100644 tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/guides.xml create mode 100644 tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/include.rst.txt create mode 100644 tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/index.rst create mode 100644 tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/somePage.rst create mode 100644 tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/yetAnotherPage.rst diff --git a/packages/guides-restructured-text/resources/config/guides-restructured-text.php b/packages/guides-restructured-text/resources/config/guides-restructured-text.php index a88b41d97..d744f29dc 100644 --- a/packages/guides-restructured-text/resources/config/guides-restructured-text.php +++ b/packages/guides-restructured-text/resources/config/guides-restructured-text.php @@ -81,6 +81,7 @@ use phpDocumentor\Guides\RestructuredText\Parser\Productions\FieldList\DateFieldListItemRule; use phpDocumentor\Guides\RestructuredText\Parser\Productions\FieldList\DedicationFieldListItemRule; use phpDocumentor\Guides\RestructuredText\Parser\Productions\FieldList\FieldListItemRule; +use phpDocumentor\Guides\RestructuredText\Parser\Productions\FieldList\NavigationTitleFieldListItemRule; use phpDocumentor\Guides\RestructuredText\Parser\Productions\FieldList\NocommentsFieldListItemRule; use phpDocumentor\Guides\RestructuredText\Parser\Productions\FieldList\NosearchFieldListItemRule; use phpDocumentor\Guides\RestructuredText\Parser\Productions\FieldList\OrganizationFieldListItemRule; @@ -320,6 +321,9 @@ ->set(DedicationFieldListItemRule::class) ->tag('phpdoc.guides.parser.rst.fieldlist') + ->set(NavigationTitleFieldListItemRule::class) + ->tag('phpdoc.guides.parser.rst.fieldlist') + ->set(NocommentsFieldListItemRule::class) ->tag('phpdoc.guides.parser.rst.fieldlist') diff --git a/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/FieldList/NavigationTitleFieldListItemRule.php b/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/FieldList/NavigationTitleFieldListItemRule.php new file mode 100644 index 000000000..76070ec1e --- /dev/null +++ b/packages/guides-restructured-text/src/RestructuredText/Parser/Productions/FieldList/NavigationTitleFieldListItemRule.php @@ -0,0 +1,34 @@ +getTerm()) === 'navigation-title'; + } + + public function apply(FieldListItemNode $fieldListItemNode, BlockContext $blockContext): MetadataNode + { + return new NavigationTitleNode($fieldListItemNode->getPlaintextContent()); + } +} diff --git a/packages/guides/src/Compiler/NodeTransformers/DocumentEntryRegistrationTransformer.php b/packages/guides/src/Compiler/NodeTransformers/DocumentEntryRegistrationTransformer.php index 1a01611ec..1095565cc 100644 --- a/packages/guides/src/Compiler/NodeTransformers/DocumentEntryRegistrationTransformer.php +++ b/packages/guides/src/Compiler/NodeTransformers/DocumentEntryRegistrationTransformer.php @@ -44,7 +44,7 @@ public function leaveNode(Node $node, CompilerContext $compilerContext): Node|nu $this->logger->warning('Document has no title', $compilerContext->getLoggerInformation()); } - $entry = new DocumentEntryNode($node->getFilePath(), $node->getTitle() ?? TitleNode::emptyNode(), $node->isRoot()); + $entry = new DocumentEntryNode($node->getFilePath(), $node->getTitle() ?? TitleNode::emptyNode(), $node->isRoot(), $node->getNavigationTitle() ?? $node->getFilePath()); $compilerContext->getProjectNode()->addDocumentEntry($entry); return $node->setDocumentEntry($entry); diff --git a/packages/guides/src/Compiler/NodeTransformers/MenuNodeTransformers/InternalMenuEntryNodeTransformer.php b/packages/guides/src/Compiler/NodeTransformers/MenuNodeTransformers/InternalMenuEntryNodeTransformer.php index 82245cc99..8f2fa3c23 100644 --- a/packages/guides/src/Compiler/NodeTransformers/MenuNodeTransformers/InternalMenuEntryNodeTransformer.php +++ b/packages/guides/src/Compiler/NodeTransformers/MenuNodeTransformers/InternalMenuEntryNodeTransformer.php @@ -19,6 +19,7 @@ use phpDocumentor\Guides\Nodes\Menu\MenuNode; use phpDocumentor\Guides\Nodes\Menu\TocNode; use phpDocumentor\Guides\Nodes\Node; +use phpDocumentor\Guides\Nodes\TitleNode; use phpDocumentor\Guides\ReferenceResolvers\DocumentNameResolverInterface; use Psr\Log\LoggerInterface; @@ -66,7 +67,7 @@ protected function handleMenuEntry(MenuNode $currentMenu, MenuEntryNode $entryNo $documentEntriesInTree[] = $documentEntry; $newEntryNode = new InternalMenuEntryNode( $documentEntry->getFile(), - $entryNode->getValue() ?? $documentEntry->getTitle(), + $entryNode->getValue() ?? TitleNode::fromString($documentEntry->getNavigationTitle()), [], false, 1, diff --git a/packages/guides/src/Nodes/DocumentNode.php b/packages/guides/src/Nodes/DocumentNode.php index 1acead6b5..fd449ed9e 100644 --- a/packages/guides/src/Nodes/DocumentNode.php +++ b/packages/guides/src/Nodes/DocumentNode.php @@ -19,6 +19,7 @@ use phpDocumentor\Guides\Nodes\DocumentTree\SectionEntryNode; use phpDocumentor\Guides\Nodes\Menu\TocNode; use phpDocumentor\Guides\Nodes\Metadata\MetadataNode; +use phpDocumentor\Guides\Nodes\Metadata\NavigationTitleNode; use function array_filter; use function max; @@ -52,6 +53,8 @@ final class DocumentNode extends CompoundNode private int $maxFootnoteNumber = 0; private int $lastReturnedAnonymousFootnoteNumber = -1; + private string|null $navigationTitle = null; + /** * Variables are replacements in a document or project. * @@ -97,6 +100,19 @@ public function getNodes(string $nodeType = Node::class): array return array_filter($this->value, static fn ($node): bool => $node instanceof $nodeType); } + public function getNavigationTitle(): string|null + { + if ($this->navigationTitle !== null) { + return $this->navigationTitle; + } + + if ($this->getTitle() instanceof TitleNode) { + return $this->getTitle()->toString(); + } + + return null; + } + public function getPageTitle(): string|null { if ($this->metaTitle !== null) { @@ -132,6 +148,10 @@ public function setMetaTitle(string $metaTitle): void public function addHeaderNode(MetadataNode $node): void { + if ($node instanceof NavigationTitleNode) { + $this->navigationTitle = $node->getValue(); + } + $this->headerNodes[] = $node; } diff --git a/packages/guides/src/Nodes/DocumentTree/DocumentEntryNode.php b/packages/guides/src/Nodes/DocumentTree/DocumentEntryNode.php index 0bb498b9c..70e67e939 100644 --- a/packages/guides/src/Nodes/DocumentTree/DocumentEntryNode.php +++ b/packages/guides/src/Nodes/DocumentTree/DocumentEntryNode.php @@ -30,7 +30,13 @@ public function __construct( private readonly string $file, private readonly TitleNode $titleNode, private readonly bool $isRoot = false, + private string $navigationTitle = '', ) { + if ($this->navigationTitle !== '') { + return; + } + + $this->navigationTitle = $titleNode->toString(); } public function getTitle(): TitleNode @@ -81,4 +87,9 @@ public function isRoot(): bool { return $this->isRoot; } + + public function getNavigationTitle(): string + { + return $this->navigationTitle; + } } diff --git a/packages/guides/src/Nodes/Metadata/NavigationTitleNode.php b/packages/guides/src/Nodes/Metadata/NavigationTitleNode.php new file mode 100644 index 000000000..95cb0b656 --- /dev/null +++ b/packages/guides/src/Nodes/Metadata/NavigationTitleNode.php @@ -0,0 +1,26 @@ + + + + Another Page - Bootstrap Theme + + + + + + + +
+ + +
+
+
+
+
+ +
+ + + + + + +
+

Another Page

+ +

Lorem Ipsum Dolor.

+
+ + +
+
+
+
+
+ + + + + + + + + + + diff --git a/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/expected/index.html b/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/expected/index.html new file mode 100644 index 000000000..43a561d9d --- /dev/null +++ b/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/expected/index.html @@ -0,0 +1,110 @@ + + + + Document Title - Bootstrap Theme + + + + + + + +
+ + +
+
+
+
+
+ +
+ + + + + + +
+

Document Title

+ +

Lorem Ipsum Dolor.

+ +
+ + +
+
+
+
+
+ + + + + + + + + + + diff --git a/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/expected/somePage.html b/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/expected/somePage.html new file mode 100644 index 000000000..bee058feb --- /dev/null +++ b/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/expected/somePage.html @@ -0,0 +1,110 @@ + + + + Some Page - Bootstrap Theme + + + + + + + +
+ + +
+
+
+
+
+ +
+ + + + + + +
+

Some Page

+ +

Lorem Ipsum Dolor.

+
+ + +
+
+
+
+
+ + + + + + + + + + + diff --git a/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/anotherPage.rst b/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/anotherPage.rst new file mode 100644 index 000000000..5e259c9eb --- /dev/null +++ b/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/anotherPage.rst @@ -0,0 +1,7 @@ +.. include:: /include.rst.txt + +============ +Another Page +============ + +Lorem Ipsum Dolor. diff --git a/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/guides.xml b/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/guides.xml new file mode 100644 index 000000000..4889c7b2e --- /dev/null +++ b/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/guides.xml @@ -0,0 +1,8 @@ + + + + diff --git a/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/include.rst.txt b/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/include.rst.txt new file mode 100644 index 000000000..c234e39bb --- /dev/null +++ b/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/include.rst.txt @@ -0,0 +1,6 @@ +.. documentblock:: + :identifier: footer + + Generated by `phpDocumentor `__. + +.. role:: custom diff --git a/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/index.rst b/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/index.rst new file mode 100644 index 000000000..2f0e5082d --- /dev/null +++ b/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/index.rst @@ -0,0 +1,19 @@ +.. include:: /include.rst.txt + +:project: + Bootstrap Theme +:version: + 1.0 + +============== +Document Title +============== + +Lorem Ipsum Dolor. + +.. toctree:: + :hidden: + :glob: + :titlesonly: + + * diff --git a/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/somePage.rst b/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/somePage.rst new file mode 100644 index 000000000..dfdf1d25d --- /dev/null +++ b/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/somePage.rst @@ -0,0 +1,7 @@ +.. include:: /include.rst.txt + +========= +Some Page +========= + +Lorem Ipsum :custom:`Dolor`. diff --git a/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/yetAnotherPage.rst b/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/yetAnotherPage.rst new file mode 100644 index 000000000..df7bb789d --- /dev/null +++ b/tests/Integration/tests-full/bootstrap/bootstrap-navigation-title/input/yetAnotherPage.rst @@ -0,0 +1,9 @@ +:navigation-title: Title in Menu + +.. include:: /include.rst.txt + +================ +Yet Another Page +================ + +Lorem Ipsum Dolor.