diff --git a/src/Directive/ConfigurationBlockDirective.php b/src/Directive/ConfigurationBlockDirective.php index a13f5464..83850ce4 100644 --- a/src/Directive/ConfigurationBlockDirective.php +++ b/src/Directive/ConfigurationBlockDirective.php @@ -66,6 +66,7 @@ public function processSub(Parser $parser, ?Node $document, string $variable, st 'directives/configuration-block.html.twig', [ 'blocks' => $blocks, + 'title' => 'Configuration formats', ] ); diff --git a/src/Directive/TabDirective.php b/src/Directive/TabDirective.php new file mode 100644 index 00000000..81e1c27f --- /dev/null +++ b/src/Directive/TabDirective.php @@ -0,0 +1,36 @@ + + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace SymfonyDocsBuilder\Directive; + +use Doctrine\RST\Directives\SubDirective; +use Doctrine\RST\Nodes\Node; +use Doctrine\RST\Parser; +use SymfonyDocsBuilder\Node\TabNode; + +/** + * Directive that only appears within the "tabs" directive. + */ +class TabDirective extends SubDirective +{ + public function getName(): string + { + return 'tab'; + } + + public function processSub(Parser $parser, ?Node $document, string $variable, string $data, array $options): ?Node + { + $tabName = $data; + if (!$tabName) { + throw new \RuntimeException(sprintf('The "tab" directive requires a tab name: ".. tab:: Tab Name".')); + } + + return new TabNode($document->getNodes(), $data); + } +} diff --git a/src/Directive/TabsDirective.php b/src/Directive/TabsDirective.php new file mode 100644 index 00000000..0a8c25e6 --- /dev/null +++ b/src/Directive/TabsDirective.php @@ -0,0 +1,60 @@ + + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace SymfonyDocsBuilder\Directive; + +use Doctrine\RST\Directives\SubDirective; +use Doctrine\RST\Nodes\Node; +use Doctrine\RST\Parser; +use SymfonyDocsBuilder\Node\TabNode; + +class TabsDirective extends SubDirective +{ + public function getName(): string + { + return 'tabs'; + } + + public function processSub(Parser $parser, ?Node $document, string $variable, string $data, array $options): ?Node + { + $tabsTitle = $data; + if (!$tabsTitle) { + throw new \RuntimeException(sprintf('The "tabs" directive requires a title: ".. tabs:: Title".')); + } + + $blocks = []; + foreach ($document->getNodes() as $tabNode) { + if (!$tabNode instanceof TabNode) { + throw new \RuntimeException(sprintf('Only ".. tab::" content can appear within the "tabs" directive.')); + } + + $content = ''; + foreach ($tabNode->getNodes() as $node) { + $content .= $node->render(); + } + + $blocks[] = [ + 'hash' => hash('sha1', $tabNode->getTabName()), + 'language_label' => $tabNode->getTabName(), + 'language' => $tabNode->getSluggedTabName(), + 'code' => $content, + ]; + } + + $wrapperDiv = $parser->renderTemplate( + 'directives/configuration-block.html.twig', + [ + 'blocks' => $blocks, + 'title' => $tabsTitle, + ] + ); + + return $parser->getNodeFactory()->createWrapperNode(null, $wrapperDiv, ''); + } +} diff --git a/src/KernelFactory.php b/src/KernelFactory.php index 8d640d8a..dceed396 100644 --- a/src/KernelFactory.php +++ b/src/KernelFactory.php @@ -91,6 +91,8 @@ private static function getDirectives(): array new SymfonyDirectives\ScreencastDirective(), new SymfonyDirectives\SeeAlsoDirective(), new SymfonyDirectives\SidebarDirective(), + new SymfonyDirectives\TabDirective(), + new SymfonyDirectives\TabsDirective(), new SymfonyDirectives\TipDirective(), new SymfonyDirectives\TopicDirective(), new SymfonyDirectives\WarningDirective(), diff --git a/src/Node/TabNode.php b/src/Node/TabNode.php new file mode 100644 index 00000000..620cc750 --- /dev/null +++ b/src/Node/TabNode.php @@ -0,0 +1,41 @@ +nodes = $nodes; + $this->tabName = $tabName; + + parent::__construct(); + } + + public function getNodes(): array + { + return $this->nodes; + } + + public function getTabName(): string + { + return $this->tabName; + } + + public function getSluggedTabName(): string + { + return strtolower(str_replace(' ', '-', $this->tabName)); + } +} diff --git a/src/Templates/default/html/directives/configuration-block.html.twig b/src/Templates/default/html/directives/configuration-block.html.twig index 03a48d3e..47ceaf62 100644 --- a/src/Templates/default/html/directives/configuration-block.html.twig +++ b/src/Templates/default/html/directives/configuration-block.html.twig @@ -1,5 +1,5 @@