Skip to content

[FEATURE] Introduce confval directive #651

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
Nov 3, 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use phpDocumentor\Guides\RestructuredText\Directives\CautionDirective;
use phpDocumentor\Guides\RestructuredText\Directives\ClassDirective;
use phpDocumentor\Guides\RestructuredText\Directives\CodeBlockDirective;
use phpDocumentor\Guides\RestructuredText\Directives\ConfvalDirective;
use phpDocumentor\Guides\RestructuredText\Directives\ContainerDirective;
use phpDocumentor\Guides\RestructuredText\Directives\ContentsDirective;
use phpDocumentor\Guides\RestructuredText\Directives\CsvTableDirective;
Expand Down Expand Up @@ -169,6 +170,7 @@
CodeNodeOptionMapper::class,
),
])
->set(ConfvalDirective::class)
->set(ContainerDirective::class)
->set(ContentsDirective::class)
->arg('$documentNameResolver', service(DocumentNameResolverInterface::class))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<dl class="confval">
<dt id="{{ node.id }}">
<code class="sig-name descname"><span class="pre">{{ node.plainContent }}</span></code></dt>
<dd>
<div class="line-block">
{% if node.type != null %} <div class="line"><strong>Type:</strong> {{ renderNode(node.type) }}</div>
{% endif -%}
{%- if node.required %} <div class="line"><strong>Required:</strong> true</div>
{% endif -%}
{% if node.default != null %} <div class="line"><strong>Type:</strong> {{ renderNode(node.default) }}</div>
{% endif -%}
{%- for key, option in node.additionalOptions -%}
<div class="line"><strong>{{ key }}:</strong> {{ renderNode(option) }}</div>

{% endfor -%}
</div>
<div class="confval-description">
{{ renderNode(node.value) }}
</div>
</dd>
</dl>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use phpDocumentor\Guides\NodeRenderers\TemplateNodeRenderer;
use phpDocumentor\Guides\RestructuredText\DependencyInjection\Compiler\TextRolePass;
use phpDocumentor\Guides\RestructuredText\Nodes\ConfvalNode;
use phpDocumentor\Guides\RestructuredText\Nodes\OptionNode;
use phpDocumentor\Guides\RestructuredText\Nodes\VersionChangeNode;
use phpDocumentor\Guides\TemplateRenderer;
Expand All @@ -27,6 +28,7 @@ class ReStructuredTextExtension extends Extension implements PrependExtensionInt
private const HTML = [
VersionChangeNode::class => 'body/version-change.html.twig',
OptionNode::class => 'body/directive/option.html.twig',
ConfvalNode::class => 'body/directive/confval.html.twig',
];

/** @param mixed[] $configs */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use phpDocumentor\Guides\Nodes\CollectionNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\RestructuredText\Nodes\AdmonitionNode;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;
use phpDocumentor\Guides\RestructuredText\Parser\Productions\Rule;

Expand All @@ -31,6 +32,7 @@ public function __construct(protected Rule $startingRule, private readonly strin
* @param Directive $directive
*/
final protected function processSub(
BlockContext $blockContext,
CollectionNode $collectionNode,
Directive $directive,
): Node|null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use phpDocumentor\Guides\Nodes\CollectionNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\RestructuredText\Nodes\VersionChangeNode;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;
use phpDocumentor\Guides\RestructuredText\Parser\Productions\Rule;

Expand All @@ -32,6 +33,7 @@ public function __construct(protected Rule $startingRule, private readonly strin
* @param Directive $directive
*/
final protected function processSub(
BlockContext $blockContext,
CollectionNode $collectionNode,
Directive $directive,
): Node|null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use phpDocumentor\Guides\Nodes\CollectionNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\RestructuredText\Nodes\AdmonitionNode;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;

use function preg_replace;
Expand Down Expand Up @@ -45,6 +46,7 @@ public function getName(): string
* @param Directive $directive
*/
protected function processSub(
BlockContext $blockContext,
CollectionNode $collectionNode,
Directive $directive,
): Node|null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use phpDocumentor\Guides\Nodes\CollectionNode;
use phpDocumentor\Guides\Nodes\DocumentNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;
use Symfony\Component\String\Slugger\AsciiSlugger;

Expand Down Expand Up @@ -39,6 +40,7 @@ public function getAliases(): array
* @param Directive $directive
*/
protected function processSub(
BlockContext $blockContext,
CollectionNode $collectionNode,
Directive $directive,
): Node|null {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Guides\RestructuredText\Directives;

use phpDocumentor\Guides\Nodes\CollectionNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\ReferenceResolvers\AnchorReducer;
use phpDocumentor\Guides\RestructuredText\Nodes\ConfvalNode;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;
use phpDocumentor\Guides\RestructuredText\Parser\InlineParser;
use phpDocumentor\Guides\RestructuredText\Parser\Productions\Rule;
use phpDocumentor\Guides\RestructuredText\TextRoles\GenericLinkProvider;

use function boolval;
use function in_array;

/**
* The confval directive configuration values.
*
* https://sphinx-toolbox.readthedocs.io/en/stable/extensions/confval.html
*/
final class ConfvalDirective extends SubDirective
{
public const NAME = 'confval';

/** @param Rule<CollectionNode> $startingRule */
public function __construct(
protected Rule $startingRule,
GenericLinkProvider $genericLinkProvider,
private readonly AnchorReducer $anchorReducer,
private readonly InlineParser $inlineParser,
) {
parent::__construct($startingRule);

$genericLinkProvider->addGenericLink(self::NAME, ConfvalNode::LINK_TYPE);
}

public function getName(): string
{
return self::NAME;
}

/** {@inheritDoc}
*
* @param Directive $directive
*/
protected function processSub(
BlockContext $blockContext,
CollectionNode $collectionNode,
Directive $directive,
): Node|null {
$id = $this->anchorReducer->reduceAnchor($directive->getData());
$type = null;
$required = false;
$default = null;
$additionalOptions = [];
if ($directive->hasOption('type')) {
$type = $this->inlineParser->parse($directive->getOption('type')->toString(), $blockContext);
}

if ($directive->hasOption('required')) {
$required = boolval($directive->getOption('required'));
}

if ($directive->hasOption('default')) {
$type = $this->inlineParser->parse($directive->getOption('default')->toString(), $blockContext);
}

foreach ($directive->getOptions() as $option) {
if (in_array($option->getName(), ['type', 'required', 'default', 'noindex'], true)) {
continue;
}

$additionalOptions[$option->getName()] = $this->inlineParser->parse($option->toString(), $blockContext);
}

return new ConfvalNode($id, $directive->getData(), $type, $required, $default, $additionalOptions, $collectionNode->getChildren());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use phpDocumentor\Guides\Nodes\CollectionNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\RestructuredText\Nodes\ContainerNode;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;

/**
Expand All @@ -32,6 +33,7 @@ public function getAliases(): array
* @param Directive $directive
*/
protected function processSub(
BlockContext $blockContext,
CollectionNode $collectionNode,
Directive $directive,
): Node|null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use phpDocumentor\Guides\Nodes\CollectionNode;
use phpDocumentor\Guides\Nodes\DocumentBlockNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;

class DocumentBlockDirective extends SubDirective
Expand All @@ -21,6 +22,7 @@ public function getName(): string
* @param Directive $directive
*/
protected function processSub(
BlockContext $blockContext,
CollectionNode $collectionNode,
Directive $directive,
): Node|null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use phpDocumentor\Guides\Nodes\CollectionNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\Nodes\QuoteNode;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;

/**
Expand All @@ -28,6 +29,7 @@ public function getName(): string
* @param Directive $directive
*/
protected function processSub(
BlockContext $blockContext,
CollectionNode $collectionNode,
Directive $directive,
): Node|null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use phpDocumentor\Guides\Nodes\FigureNode;
use phpDocumentor\Guides\Nodes\ImageNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;
use phpDocumentor\Guides\RestructuredText\Parser\Productions\Rule;

Expand Down Expand Up @@ -37,6 +38,7 @@ public function getName(): string
* @param Directive $directive
*/
protected function processSub(
BlockContext $blockContext,
CollectionNode $collectionNode,
Directive $directive,
): Node|null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use phpDocumentor\Guides\Nodes\InlineCompoundNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\RestructuredText\Nodes\GeneralDirectiveNode;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;

/**
Expand All @@ -25,6 +26,7 @@ public function getName(): string
* @param Directive $directive
*/
protected function processSub(
BlockContext $blockContext,
CollectionNode $collectionNode,
Directive $directive,
): Node|null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use phpDocumentor\Guides\Nodes\CollectionNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\Nodes\QuoteNode;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;

/**
Expand All @@ -27,6 +28,7 @@ public function getName(): string
* @param Directive $directive
*/
protected function processSub(
BlockContext $blockContext,
CollectionNode $collectionNode,
Directive $directive,
): Node|null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\ReferenceResolvers\AnchorReducer;
use phpDocumentor\Guides\RestructuredText\Nodes\OptionNode;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;
use phpDocumentor\Guides\RestructuredText\Parser\Productions\Rule;
use phpDocumentor\Guides\RestructuredText\TextRoles\GenericLinkProvider;
Expand Down Expand Up @@ -47,6 +48,7 @@ public function getName(): string
* @param Directive $directive
*/
protected function processSub(
BlockContext $blockContext,
CollectionNode $collectionNode,
Directive $directive,
): Node|null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use phpDocumentor\Guides\Nodes\CollectionNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\Nodes\QuoteNode;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;

/**
Expand All @@ -28,6 +29,7 @@ public function getName(): string
* @param Directive $directive
*/
protected function processSub(
BlockContext $blockContext,
CollectionNode $collectionNode,
Directive $directive,
): Node|null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\Nodes\ParagraphNode;
use phpDocumentor\Guides\Nodes\ReplacementNode;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;

use function count;
Expand All @@ -30,6 +31,7 @@ public function getName(): string
* @param Directive $directive
*/
final protected function processSub(
BlockContext $blockContext,
CollectionNode $collectionNode,
Directive $directive,
): Node|null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use phpDocumentor\Guides\Nodes\CollectionNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\RestructuredText\Nodes\SidebarNode;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;

/**
Expand All @@ -35,6 +36,7 @@ public function getName(): string
* @param Directive $directive
*/
protected function processSub(
BlockContext $blockContext,
CollectionNode $collectionNode,
Directive $directive,
): Node|null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final public function process(
return null;
}

$node = $this->processSub($collectionNode, $directive);
$node = $this->processSub($blockContext, $collectionNode, $directive);

if ($node === null) {
return null;
Expand All @@ -54,6 +54,7 @@ protected function getStartingRule(): Rule
}

protected function processSub(
BlockContext $blockContext,
CollectionNode $collectionNode,
Directive $directive,
): Node|null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use phpDocumentor\Guides\Nodes\CollectionNode;
use phpDocumentor\Guides\Nodes\Node;
use phpDocumentor\Guides\Nodes\TableNode;
use phpDocumentor\Guides\RestructuredText\Parser\BlockContext;
use phpDocumentor\Guides\RestructuredText\Parser\Directive;
use phpDocumentor\Guides\RestructuredText\Parser\Productions\Rule;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -49,6 +50,7 @@ public function getName(): string

/** {@inheritDoc} */
protected function processSub(
BlockContext $blockContext,
CollectionNode $collectionNode,
Directive $directive,
): Node|null {
Expand Down
Loading