-
-
Notifications
You must be signed in to change notification settings - Fork 15
[FEATURE] add configuration block directive #795
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
.../guides-restructured-text/src/RestructuredText/Directives/ConfigurationBlockDirective.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Guides\RestructuredText\Directives; | ||
|
||
use phpDocumentor\Guides\Nodes\CodeNode; | ||
use phpDocumentor\Guides\Nodes\CollectionNode; | ||
use phpDocumentor\Guides\Nodes\Configuration\ConfigurationBlockNode; | ||
use phpDocumentor\Guides\Nodes\Configuration\ConfigurationTab; | ||
use phpDocumentor\Guides\Nodes\Node; | ||
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext; | ||
use phpDocumentor\Guides\RestructuredText\Parser\Directive; | ||
use phpDocumentor\Guides\RestructuredText\Parser\Productions\Rule; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\String\Slugger\AsciiSlugger; | ||
use Symfony\Component\String\Slugger\SluggerInterface; | ||
|
||
use function assert; | ||
use function get_debug_type; | ||
|
||
final class ConfigurationBlockDirective extends SubDirective | ||
{ | ||
private SluggerInterface $slugger; | ||
|
||
/** | ||
* @param Rule<CollectionNode> $startingRule | ||
* @param array<string, string> $languageLabels | ||
*/ | ||
public function __construct( | ||
private LoggerInterface $logger, | ||
Rule $startingRule, | ||
private readonly array $languageLabels = [], | ||
) { | ||
parent::__construct($startingRule); | ||
|
||
$this->slugger = new AsciiSlugger(); | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'configuration-block'; | ||
} | ||
|
||
protected function processSub( | ||
BlockContext $blockContext, | ||
CollectionNode $collectionNode, | ||
Directive $directive, | ||
): Node|null { | ||
$tabs = []; | ||
foreach ($collectionNode->getValue() as $child) { | ||
if (!$child instanceof CodeNode) { | ||
$this->logger->warning('The ".. configuration-block::" directive only supports code blocks, "' . get_debug_type($child) . '" given.'); | ||
|
||
continue; | ||
} | ||
|
||
$language = $child->getLanguage(); | ||
assert($language !== null); | ||
|
||
$label = $this->languageLabels[$language] ?? $this->slugger->slug($language, ' ')->title()->toString(); | ||
|
||
$tabs[] = new ConfigurationTab( | ||
$label, | ||
$this->slugger->slug($label)->lower()->toString(), | ||
$child, | ||
); | ||
} | ||
|
||
return new ConfigurationBlockNode($tabs); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/guides/resources/template/html/body/configuration-block.html.twig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<div class="configuration-block"> | ||
<div role="tablist" aria-label="Configuration formats" class="configuration-tabs configuration-tabs-length-{{ node.tabs|length }}"> | ||
{% for tab in node.tabs %} | ||
<button role="tab" type="button" data-language="{{ tab.slug }}" | ||
aria-controls="{{ 'configuration-block-tabpanel-' ~ tab.hash }}" aria-selected="{{ loop.first ? 'true' : 'false' }}" | ||
{{ loop.first ? 'data-active="true"' }} {{ not loop.first ? 'tabindex="-1"' }}> | ||
<span>{{ tab.label }}</span> | ||
</button> | ||
{% endfor %} | ||
</div> | ||
|
||
{% for tab in node.tabs %} | ||
<div role="tabpanel" id="{{ 'configuration-block-tabpanel-' ~ tab.hash }}" aria-label="{{ tab.label }}" class="configuration-codeblock" data-language="{{ tab.slug }}" style="{{ not loop.first ? 'display: none' }}"> | ||
{{ renderNode(tab.content) }} | ||
</div> | ||
{% endfor %} | ||
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/guides/src/Nodes/Configuration/ConfigurationBlockNode.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Guides\Nodes\Configuration; | ||
|
||
use phpDocumentor\Guides\Nodes\AbstractNode; | ||
|
||
/** @extends AbstractNode<list<ConfigurationTab>> */ | ||
final class ConfigurationBlockNode extends AbstractNode | ||
{ | ||
/** @param list<ConfigurationTab> $tabs */ | ||
linawolf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public function __construct( | ||
array $tabs, | ||
) { | ||
$this->value = $tabs; | ||
} | ||
|
||
/** @return list<ConfigurationTab> */ | ||
public function getTabs(): array | ||
{ | ||
return $this->value; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/guides/src/Nodes/Configuration/ConfigurationTab.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Guides\Nodes\Configuration; | ||
|
||
use phpDocumentor\Guides\Nodes\CodeNode; | ||
|
||
use function hash; | ||
|
||
final class ConfigurationTab | ||
{ | ||
public readonly string $hash; | ||
|
||
public function __construct( | ||
public readonly string $label, | ||
public readonly string $slug, | ||
public readonly CodeNode $content, | ||
) { | ||
$this->hash = hash('xxh128', $content->getValue()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
tests/Integration/tests/directives/directive-configuration-block/expected/index.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<!-- content start --> | ||
<div class="section" id="directive-tests"> | ||
<h1>Directive tests</h1> | ||
|
||
<div class="configuration-block"> | ||
<div role="tablist" aria-label="Configuration formats" class="configuration-tabs configuration-tabs-length-2"> | ||
<button role="tab" type="button" data-language="yaml" | ||
aria-controls="configuration-block-tabpanel-523eafc9524d67db5f21ecae2362d532" aria-selected="true" | ||
data-active="true" > | ||
<span>Yaml</span> | ||
</button> | ||
<button role="tab" type="button" data-language="custom" | ||
aria-controls="configuration-block-tabpanel-a51ba27b3c76153f629592baf23834dc" aria-selected="false" | ||
tabindex="-1"> | ||
<span>CUSTOM</span> | ||
</button> | ||
</div> | ||
|
||
<div role="tabpanel" id="configuration-block-tabpanel-523eafc9524d67db5f21ecae2362d532" aria-label="Yaml" class="configuration-codeblock" data-language="yaml" style=""> | ||
<pre><code class="language-yaml"># app/config/services.yml</code></pre> | ||
</div> | ||
<div role="tabpanel" id="configuration-block-tabpanel-a51ba27b3c76153f629592baf23834dc" aria-label="CUSTOM" class="configuration-codeblock" data-language="custom" style="display: none"> | ||
<pre><code class="language-php">// config/routes.php</code></pre> | ||
</div> | ||
</div> | ||
|
||
</div> | ||
|
||
|
||
<!-- content end --> |
9 changes: 9 additions & 0 deletions
9
tests/Integration/tests/directives/directive-configuration-block/input/guides.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<guides xmlns="https://www.phpdoc.org/guides" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="https://www.phpdoc.org/guides packages/guides-cli/resources/schema/guides.xsd"> | ||
<extension class="\phpDocumentor\Guides\RestructuredText\DependencyInjection\ReStructuredTextExtension"> | ||
<code-language-label language="php" label="CUSTOM" /> | ||
linawolf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<code-language-label language="other" label="Other" /> | ||
</extension> | ||
</guides> |
13 changes: 13 additions & 0 deletions
13
tests/Integration/tests/directives/directive-configuration-block/input/index.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Directive tests | ||
=============== | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/services.yml | ||
|
||
.. code-block:: php | ||
|
||
// config/routes.php | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.