forked from TYPO3-Documentation/render-guides
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewhelperTextRole.php
More file actions
49 lines (40 loc) · 1.8 KB
/
ViewhelperTextRole.php
File metadata and controls
49 lines (40 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
declare(strict_types=1);
namespace T3Docs\Typo3DocsTheme\TextRoles;
use phpDocumentor\Guides\Nodes\Inline\InlineNodeInterface;
use phpDocumentor\Guides\Nodes\Inline\PlainTextInlineNode;
use phpDocumentor\Guides\Nodes\Inline\ReferenceNode;
use phpDocumentor\Guides\ReferenceResolvers\AnchorNormalizer;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use Psr\Log\LoggerInterface;
final class ViewhelperTextRole extends CustomLinkTextRole
{
private const TYPE = 'viewhelper';
/**
* @see https://regex101.com/r/Fj8X5Y/1
*/
public function __construct(
LoggerInterface $logger,
private readonly AnchorNormalizer $anchorNormalizer,
) {
parent::__construct($logger, $anchorNormalizer);
}
protected function createNode(DocumentParserContext $documentParserContext, string $referenceTarget, string|null $referenceName, string $role): ReferenceNode
{
$children = $referenceName ? [new PlainTextInlineNode($referenceName)] : [];
if (preg_match(self::INTERLINK_NAME_REGEX, $referenceTarget, $matches)) {
return $this->createNodeWithInterlink($documentParserContext, $matches[2], $matches[1], $children);
}
return $this->createNodeWithInterlink($documentParserContext, $referenceTarget, '', $children);
}
/** @param list<InlineNodeInterface> $children */
private function createNodeWithInterlink(DocumentParserContext $documentParserContext, string $referenceTarget, string $interlinkDomain, array $children): ReferenceNode
{
$id = $this->anchorNormalizer->reduceAnchor($referenceTarget);
return new ReferenceNode($id, $children, $interlinkDomain, 'typo3:' . $this->getName());
}
public function getName(): string
{
return self::TYPE;
}
}