forked from TYPO3-Documentation/render-guides
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT3srcTextRole.php
More file actions
61 lines (52 loc) · 2.04 KB
/
T3srcTextRole.php
File metadata and controls
61 lines (52 loc) · 2.04 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
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace T3Docs\Typo3DocsTheme\TextRoles;
use phpDocumentor\Guides\Nodes\Inline\HyperLinkNode;
use phpDocumentor\Guides\Nodes\Inline\PlainTextInlineNode;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use phpDocumentor\Guides\RestructuredText\Parser\References\EmbeddedReferenceParser;
use phpDocumentor\Guides\RestructuredText\TextRoles\TextRole;
use T3Docs\Typo3DocsTheme\Inventory\Typo3VersionService;
final class T3srcTextRole implements TextRole
{
use EmbeddedReferenceParser;
final public const NAME = 't3src';
public function __construct(
private readonly Typo3VersionService $typo3VersionService,
) {}
public function getName(): string
{
return self::NAME;
}
/** @inheritDoc */
public function getAliases(): array
{
return [];
}
public function processNode(
DocumentParserContext $documentParserContext,
string $role,
string $content,
string $rawContent,
): HyperLinkNode {
$referenceData = $this->extractEmbeddedReference($content);
return $this->createNode($documentParserContext, $referenceData->reference, $referenceData->text, $role);
}
protected function createNode(
DocumentParserContext $documentParserContext,
string $referenceTarget,
string|null $referenceName,
string $role
): HyperLinkNode {
$typo3Version = $this->typo3VersionService->getPreferredVersion();
$fileLink = $referenceTarget;
if (str_starts_with($fileLink, 'EXT:')) {
$fileLink = str_replace('EXT:', 'typo3/sysext/', $fileLink);
}
if (!str_starts_with($fileLink, 'typo3/sysext/')) {
$fileLink = 'typo3/sysext/' . $fileLink;
}
$gitHubLink = sprintf('https://github.com/typo3/typo3/blob/%s/%s', $typo3Version, $fileLink);
$fileName = $referenceName ?? str_replace('typo3/sysext/', 'EXT:', $fileLink) . ' (GitHub)';
return new HyperLinkNode([new PlainTextInlineNode($fileName)], $gitHubLink);
}
}