Skip to content

Adding the "tabs" directive #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2023
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
1 change: 1 addition & 0 deletions src/Directive/ConfigurationBlockDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function processSub(Parser $parser, ?Node $document, string $variable, st
'directives/configuration-block.html.twig',
[
'blocks' => $blocks,
'title' => 'Configuration formats',
]
);

Expand Down
36 changes: 36 additions & 0 deletions src/Directive/TabDirective.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Docs Builder package.
* (c) Ryan Weaver <ryan@symfonycasts.com>
* 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);
}
}
60 changes: 60 additions & 0 deletions src/Directive/TabsDirective.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the Docs Builder package.
* (c) Ryan Weaver <ryan@symfonycasts.com>
* 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, '</div>');
}
}
2 changes: 2 additions & 0 deletions src/KernelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
41 changes: 41 additions & 0 deletions src/Node/TabNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace SymfonyDocsBuilder\Node;

use Doctrine\RST\Nodes\Node;

/**
* Wraps nodes + options in a TabDirective.
*/
class TabNode extends Node
{
/**
* @var Node[]
*/
private array $nodes;

private string $tabName;

public function __construct(array $nodes, string $tabName)
{
$this->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));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="configuration-block">
<div role="tablist" aria-label="Configuration formats" class="configuration-tabs configuration-tabs-length-{{ blocks|length }}">
<div role="tablist" aria-label="{{ title }}" class="configuration-tabs configuration-tabs-length-{{ blocks|length }}">
{% for block in blocks %}
<button role="tab" type="button" data-language="{{ block.language }}"
aria-controls="{{ 'configuration-block-tabpanel-' ~ block.hash }}" aria-selected="{{ loop.first ? 'true' : 'false' }}"
Expand Down
4 changes: 4 additions & 0 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ public function parserUnitBlockProvider()
'blockName' => 'directives/sidebar-code-block-nested',
];

yield 'tabs' => [
'blockName' => 'directives/tabs',
];

yield 'class-reference' => [
'blockName' => 'references/class',
];
Expand Down
24 changes: 24 additions & 0 deletions tests/fixtures/expected/blocks/directives/tabs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div class="configuration-block">
<div role="tablist" aria-label="UX Installation" class="configuration-tabs configuration-tabs-length-2">
<button role="tab" type="button" data-language="webpack-encore" aria-controls="configuration-block-tabpanel-97a055193fa707f2025ee25bb469c74aa29b2c6c" aria-selected="true" data-active="true">
<span>Webpack Encore</span>
</button>
<button role="tab" type="button" data-language="assetmapper" aria-controls="configuration-block-tabpanel-1865de6b2b585de79e8700d7968050722e562c44" aria-selected="false" tabindex="-1">
<span>AssetMapper</span>
</button>
</div>

<div role="tabpanel" id="configuration-block-tabpanel-97a055193fa707f2025ee25bb469c74aa29b2c6c" aria-label="Webpack Encore" class="configuration-codeblock" data-language="webpack-encore" style="">
<p>Webpack Encore stuff!</p>
<div translate="no" data-loc="1" class="notranslate codeblock codeblock-length-sm codeblock-yaml">
<div class="codeblock-scroll">
<pre class="codeblock-lines">1</pre>
<pre class="codeblock-code"><code><span class="hljs-attr">I am yaml:</span></code></pre>
</div>
</div>
</div>
<div role="tabpanel" id="configuration-block-tabpanel-1865de6b2b585de79e8700d7968050722e562c44" aria-label="AssetMapper" class="configuration-codeblock" data-language="assetmapper" style="display: none">
<p>AssetMapper stuff!</p>
<p>And another paragraph.</p>
</div>
</div>
16 changes: 16 additions & 0 deletions tests/fixtures/source/blocks/directives/tabs.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

.. tabs:: UX Installation

.. tab:: Webpack Encore

Webpack Encore stuff!

.. code-block:: yaml

I am yaml:

.. tab:: AssetMapper

AssetMapper stuff!

And another paragraph.