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 f13cbb397..95476ea1d 100644 --- a/packages/guides-restructured-text/resources/config/guides-restructured-text.php +++ b/packages/guides-restructured-text/resources/config/guides-restructured-text.php @@ -212,6 +212,7 @@ ->set(TipDirective::class) ->set(TitleDirective::class) ->set(ToctreeDirective::class) + ->bind('$startingRule', service(InlineMarkupRule::class)) ->set(MenuDirective::class) ->set(TodoDirective::class) ->set(UmlDirective::class) diff --git a/packages/guides-restructured-text/src/RestructuredText/Directives/ToctreeDirective.php b/packages/guides-restructured-text/src/RestructuredText/Directives/ToctreeDirective.php index 0a90c3fea..c57e8c7a6 100644 --- a/packages/guides-restructured-text/src/RestructuredText/Directives/ToctreeDirective.php +++ b/packages/guides-restructured-text/src/RestructuredText/Directives/ToctreeDirective.php @@ -4,11 +4,13 @@ namespace phpDocumentor\Guides\RestructuredText\Directives; +use phpDocumentor\Guides\Nodes\InlineCompoundNode; use phpDocumentor\Guides\Nodes\Menu\TocNode; use phpDocumentor\Guides\Nodes\Node; use phpDocumentor\Guides\RestructuredText\Parser\BlockContext; use phpDocumentor\Guides\RestructuredText\Parser\Directive; use phpDocumentor\Guides\RestructuredText\Parser\DirectiveOption; +use phpDocumentor\Guides\RestructuredText\Parser\Productions\Rule; use phpDocumentor\Guides\RestructuredText\Toc\ToctreeBuilder; /** @@ -22,8 +24,11 @@ */ class ToctreeDirective extends BaseDirective { - public function __construct(private readonly ToctreeBuilder $toctreeBuilder) - { + /** @param Rule $startingRule */ + public function __construct( + private readonly ToctreeBuilder $toctreeBuilder, + private readonly Rule $startingRule, + ) { } public function getName(): string @@ -46,6 +51,14 @@ public function process( $options, ); - return (new TocNode($toctreeFiles))->withOptions($this->optionsToArray($options)); + $tocNode = (new TocNode($toctreeFiles))->withOptions($this->optionsToArray($options)); + + if (isset($options['caption'])) { + $blockContextOfCaption = new BlockContext($blockContext->getDocumentParserContext(), (string) $options['caption']->getValue()); + $inlineNode = $this->startingRule->apply($blockContextOfCaption); + $tocNode = $tocNode->withCaption($inlineNode); + } + + return $tocNode; } } diff --git a/packages/guides/resources/template/html/body/menu/table-of-content.html.twig b/packages/guides/resources/template/html/body/menu/table-of-content.html.twig index 20dd75a21..3bbd3de71 100644 --- a/packages/guides/resources/template/html/body/menu/table-of-content.html.twig +++ b/packages/guides/resources/template/html/body/menu/table-of-content.html.twig @@ -1,3 +1,6 @@
+ {% if node.caption %} +

{{ renderNode(node.caption) }}

+ {% endif -%} {% include "body/menu/menu-level.html.twig" %}
diff --git a/packages/guides/src/Nodes/Menu/ContentMenuNode.php b/packages/guides/src/Nodes/Menu/ContentMenuNode.php index ae791b9e2..a0e9f1b7f 100644 --- a/packages/guides/src/Nodes/Menu/ContentMenuNode.php +++ b/packages/guides/src/Nodes/Menu/ContentMenuNode.php @@ -13,15 +13,11 @@ namespace phpDocumentor\Guides\Nodes\Menu; -use phpDocumentor\Guides\Nodes\InlineCompoundNode; - use function is_scalar; /** @link https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#table-of-contents */ class ContentMenuNode extends MenuNode { - private InlineCompoundNode|null $caption; - public function getDepth(): int { if ($this->hasOption('depth') && is_scalar($this->getOption('depth'))) { @@ -35,17 +31,4 @@ public function isPageLevelOnly(): bool { return false; } - - public function getCaption(): InlineCompoundNode|null - { - return $this->caption; - } - - public function withCaption(InlineCompoundNode|null $caption): ContentMenuNode - { - $that = clone $this; - $that->caption = $caption; - - return $that; - } } diff --git a/packages/guides/src/Nodes/Menu/MenuNode.php b/packages/guides/src/Nodes/Menu/MenuNode.php index 0162fdfde..d78c48007 100644 --- a/packages/guides/src/Nodes/Menu/MenuNode.php +++ b/packages/guides/src/Nodes/Menu/MenuNode.php @@ -14,6 +14,7 @@ namespace phpDocumentor\Guides\Nodes\Menu; use phpDocumentor\Guides\Nodes\CompoundNode; +use phpDocumentor\Guides\Nodes\InlineCompoundNode; use phpDocumentor\Guides\Nodes\Node; use const PHP_INT_MAX; @@ -25,6 +26,7 @@ */ abstract class MenuNode extends CompoundNode { + private InlineCompoundNode|null $caption = null; protected const DEFAULT_DEPTH = PHP_INT_MAX; /** @var MenuEntryNode[] */ @@ -60,4 +62,17 @@ public function getMenuEntries(): array } abstract public function isPageLevelOnly(): bool; + + public function getCaption(): InlineCompoundNode|null + { + return $this->caption; + } + + public function withCaption(InlineCompoundNode|null $caption): static + { + $that = clone $this; + $that->caption = $caption; + + return $that; + } } diff --git a/tests/Integration/tests/toctree/toctree-caption/expected/index.html b/tests/Integration/tests/toctree/toctree-caption/expected/index.html new file mode 100644 index 000000000..ba1bfb09a --- /dev/null +++ b/tests/Integration/tests/toctree/toctree-caption/expected/index.html @@ -0,0 +1,17 @@ + +
+

Document Title

+ +
+

Table of Contents

+ +
+ +
+ + diff --git a/tests/Integration/tests/toctree/toctree-caption/input/index.rst b/tests/Integration/tests/toctree/toctree-caption/input/index.rst new file mode 100644 index 000000000..da46272c8 --- /dev/null +++ b/tests/Integration/tests/toctree/toctree-caption/input/index.rst @@ -0,0 +1,9 @@ +Document Title +============== + +.. toctree:: + :caption: **Table of Contents** + :titlesonly: + + page1 + page2 diff --git a/tests/Integration/tests/toctree/toctree-caption/input/page1.rst b/tests/Integration/tests/toctree/toctree-caption/input/page1.rst new file mode 100644 index 000000000..1f44f1177 --- /dev/null +++ b/tests/Integration/tests/toctree/toctree-caption/input/page1.rst @@ -0,0 +1,8 @@ +Page 1 +====== + +Chapter 1 +--------- + +Chapter 2 +--------- diff --git a/tests/Integration/tests/toctree/toctree-caption/input/page2.rst b/tests/Integration/tests/toctree/toctree-caption/input/page2.rst new file mode 100644 index 000000000..ff8195304 --- /dev/null +++ b/tests/Integration/tests/toctree/toctree-caption/input/page2.rst @@ -0,0 +1,4 @@ +Page 2 +====== + +Lorem Ipsum Dolor