-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #636 from phpDocumentor/feature/standard-domain
[FEATURE] Introduce option directive
- Loading branch information
Showing
40 changed files
with
517 additions
and
65 deletions.
There are no files selected for viewing
This file contains 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 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
9 changes: 9 additions & 0 deletions
9
packages/guides-restructured-text/resources/template/html/body/directive/option.html.twig
This file contains 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 @@ | ||
{%- for id in node.additionalIds %} | ||
<a id="{{ id }}"></a> | ||
{% endfor -%} | ||
<dl class="domain-default-option"> | ||
<dt class="domain-default-option-name" id="{{ node.id }}">{{ node.plainContent }}</dt> | ||
<dd class="domain-default-option-description"> | ||
{{ renderNode(node.value) }} | ||
</dd> | ||
</dl> |
This file contains 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
79 changes: 79 additions & 0 deletions
79
packages/guides-restructured-text/src/RestructuredText/Directives/OptionDirective.php
This file contains 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,79 @@ | ||
<?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\OptionNode; | ||
use phpDocumentor\Guides\RestructuredText\Parser\Directive; | ||
use phpDocumentor\Guides\RestructuredText\Parser\Productions\Rule; | ||
use phpDocumentor\Guides\RestructuredText\TextRoles\GenericLinkProvider; | ||
|
||
use function array_map; | ||
use function explode; | ||
use function preg_replace; | ||
use function str_contains; | ||
|
||
/** | ||
* Describes a command line argument or switch. Option argument names should be enclosed in angle brackets. | ||
* | ||
* https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html#directive-option | ||
*/ | ||
final class OptionDirective extends SubDirective | ||
{ | ||
public const NAME = 'option'; | ||
|
||
/** @param Rule<CollectionNode> $startingRule */ | ||
public function __construct( | ||
protected Rule $startingRule, | ||
GenericLinkProvider $genericLinkProvider, | ||
private readonly AnchorReducer $anchorReducer, | ||
) { | ||
parent::__construct($startingRule); | ||
|
||
$genericLinkProvider->addGenericLink(self::NAME, OptionNode::LINK_TYPE); | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return self::NAME; | ||
} | ||
|
||
/** {@inheritDoc} | ||
* | ||
* @param Directive $directive | ||
*/ | ||
protected function processSub( | ||
CollectionNode $collectionNode, | ||
Directive $directive, | ||
): Node|null { | ||
$additionalIds = $this->getAdditionalIds($directive); | ||
|
||
$id = $this->anchorReducer->reduceAnchor($directive->getData()); | ||
|
||
return new OptionNode($id, $directive->getData(), $additionalIds, $collectionNode->getChildren()); | ||
} | ||
|
||
/** @return string[] */ | ||
private function getAdditionalIds(Directive $directive): array | ||
{ | ||
$additionalIds = []; | ||
if (str_contains($directive->getData(), ',')) { | ||
$additionalIds = explode(',', $directive->getData()); | ||
$additionalIds = array_map('trim', $additionalIds); | ||
$additionalIds = array_map(function ($item) { | ||
// remove additional information in brackets like <module> | ||
$pattern = '/<([^>]+)>/'; | ||
$item = preg_replace($pattern, '', $item); | ||
|
||
// only keep allowed signs | ||
return $this->anchorReducer->reduceAnchor($item ?? ''); | ||
}, $additionalIds); | ||
} | ||
|
||
return $additionalIds; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
packages/guides-restructured-text/src/RestructuredText/Nodes/OptionNode.php
This file contains 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,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Guides\RestructuredText\Nodes; | ||
|
||
use phpDocumentor\Guides\Nodes\CompoundNode; | ||
use phpDocumentor\Guides\Nodes\LinkTargetNode; | ||
use phpDocumentor\Guides\Nodes\MultipleLinkTargetsNode; | ||
use phpDocumentor\Guides\Nodes\Node; | ||
|
||
/** | ||
* Describes a command line argument or switch. Option argument names should be enclosed in angle brackets. | ||
* | ||
* https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html#directive-option | ||
* | ||
* @extends CompoundNode<Node> | ||
*/ | ||
class OptionNode extends CompoundNode implements LinkTargetNode, MultipleLinkTargetsNode | ||
{ | ||
public const LINK_TYPE = 'std:option'; | ||
|
||
/** | ||
* @param list<Node> $value | ||
* @param string[] $additionalIds | ||
*/ | ||
public function __construct( | ||
private readonly string $id, | ||
private readonly string $plainContent, | ||
private readonly array $additionalIds, | ||
array $value = [], | ||
) { | ||
parent::__construct($value); | ||
} | ||
|
||
public function getPlainContent(): string | ||
{ | ||
return $this->plainContent; | ||
} | ||
|
||
public function getLinkType(): string | ||
{ | ||
return self::LINK_TYPE; | ||
} | ||
|
||
public function getId(): string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getLinkText(): string | ||
{ | ||
return $this->plainContent; | ||
} | ||
|
||
/** @return string[] */ | ||
public function getAdditionalIds(): array | ||
{ | ||
return $this->additionalIds; | ||
} | ||
} |
This file contains 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 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
31 changes: 31 additions & 0 deletions
31
packages/guides-restructured-text/src/RestructuredText/TextRoles/GenericLinkProvider.php
This file contains 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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Guides\RestructuredText\TextRoles; | ||
|
||
use phpDocumentor\Guides\Nodes\SectionNode; | ||
|
||
final class GenericLinkProvider | ||
{ | ||
/** @var array<string, string> */ | ||
private array $textRoleLinkTypeMapping = [ | ||
'ref' => SectionNode::STD_LABEL, | ||
]; | ||
|
||
public function addGenericLink(string $textRole, string $linkType): void | ||
{ | ||
$this->textRoleLinkTypeMapping[$textRole] = $linkType; | ||
} | ||
|
||
/** @return string[] */ | ||
public function getTextRoleLinkTypeMapping(): array | ||
{ | ||
return $this->textRoleLinkTypeMapping; | ||
} | ||
|
||
public function getLinkType(string $textRole): string | ||
{ | ||
return $this->textRoleLinkTypeMapping[$textRole] ?? SectionNode::STD_LABEL; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...ages/guides-restructured-text/src/RestructuredText/TextRoles/GenericReferenceTextRole.php
This file contains 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,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Guides\RestructuredText\TextRoles; | ||
|
||
use phpDocumentor\Guides\Nodes\Inline\AbstractLinkInlineNode; | ||
use phpDocumentor\Guides\Nodes\Inline\ReferenceNode; | ||
use phpDocumentor\Guides\ReferenceResolvers\AnchorReducer; | ||
use Psr\Log\LoggerInterface; | ||
|
||
use function array_keys; | ||
use function preg_match; | ||
|
||
class GenericReferenceTextRole extends AbstractReferenceTextRole | ||
{ | ||
public function __construct( | ||
protected readonly LoggerInterface $logger, | ||
private readonly GenericLinkProvider $genericLinkProvider, | ||
private readonly AnchorReducer $anchorReducer, | ||
) { | ||
parent::__construct($this->logger); | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return 'ref'; | ||
} | ||
|
||
/** @inheritDoc */ | ||
public function getAliases(): array | ||
{ | ||
return array_keys($this->genericLinkProvider->getTextRoleLinkTypeMapping()); | ||
} | ||
|
||
/** @return ReferenceNode */ | ||
protected function createNode(string $referenceTarget, string|null $referenceName, string $role): AbstractLinkInlineNode | ||
{ | ||
$linkType = $this->genericLinkProvider->getLinkType($role); | ||
$pattern = '/^([a-zA-Z0-9]+):(.*$)/'; | ||
if (preg_match($pattern, $referenceTarget, $matches)) { | ||
$interlinkDomain = $matches[1]; | ||
$id = $this->anchorReducer->reduceAnchor($matches[2]); | ||
} else { | ||
$interlinkDomain = ''; | ||
$id = $this->anchorReducer->reduceAnchor($referenceTarget); | ||
} | ||
|
||
return new ReferenceNode($id, $referenceName ?? '', $interlinkDomain, $linkType); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.