|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Docs Builder package. |
| 5 | + * (c) Ryan Weaver <[email protected]> |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +namespace SymfonyDocsBuilder\Directive; |
| 11 | + |
| 12 | +use Doctrine\RST\Directives\SubDirective; |
| 13 | +use Doctrine\RST\Nodes\Node; |
| 14 | +use Doctrine\RST\Parser; |
| 15 | +use SymfonyDocsBuilder\Node\TabNode; |
| 16 | + |
| 17 | +class TabsDirective extends SubDirective |
| 18 | +{ |
| 19 | + public function getName(): string |
| 20 | + { |
| 21 | + return 'tabs'; |
| 22 | + } |
| 23 | + |
| 24 | + public function processSub(Parser $parser, ?Node $document, string $variable, string $data, array $options): ?Node |
| 25 | + { |
| 26 | + $tabsTitle = $data; |
| 27 | + if (!$tabsTitle) { |
| 28 | + throw new \RuntimeException(sprintf('The "tabs" directive requires a title: ".. tabs:: Title".')); |
| 29 | + } |
| 30 | + |
| 31 | + $blocks = []; |
| 32 | + foreach ($document->getNodes() as $tabNode) { |
| 33 | + if (!$tabNode instanceof TabNode) { |
| 34 | + throw new \RuntimeException(sprintf('Only ".. tab::" content can appear within the "tabs" directive.')); |
| 35 | + } |
| 36 | + |
| 37 | + $content = ''; |
| 38 | + foreach ($tabNode->getNodes() as $node) { |
| 39 | + $content .= $node->render(); |
| 40 | + } |
| 41 | + |
| 42 | + $blocks[] = [ |
| 43 | + 'hash' => hash('sha1', $tabNode->getTabName()), |
| 44 | + 'language_label' => $tabNode->getTabName(), |
| 45 | + 'language' => $tabNode->getSluggedTabName(), |
| 46 | + 'code' => $content, |
| 47 | + ]; |
| 48 | + } |
| 49 | + |
| 50 | + $wrapperDiv = $parser->renderTemplate( |
| 51 | + 'directives/configuration-block.html.twig', |
| 52 | + [ |
| 53 | + 'blocks' => $blocks, |
| 54 | + 'title' => $tabsTitle, |
| 55 | + ] |
| 56 | + ); |
| 57 | + |
| 58 | + return $parser->getNodeFactory()->createWrapperNode(null, $wrapperDiv, '</div>'); |
| 59 | + } |
| 60 | +} |
0 commit comments