Skip to content

Commit b1c2e0a

Browse files
committed
[FEATURE] Introduce exchangeable InterlinkParser
In TYPO3 we want to allow additonal special signs, so that interlinks to composer names can be made. Additionally, this also removed duplicate code
1 parent 434b877 commit b1c2e0a

File tree

9 files changed

+69
-28
lines changed

9 files changed

+69
-28
lines changed

packages/guides-restructured-text/resources/config/guides-restructured-text.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
use phpDocumentor\Guides\RestructuredText\MarkupLanguageParser;
5757
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContextFactory;
5858
use phpDocumentor\Guides\RestructuredText\Parser\InlineParser;
59+
use phpDocumentor\Guides\RestructuredText\Parser\Interlink\DefaultInterlinkParser;
60+
use phpDocumentor\Guides\RestructuredText\Parser\Interlink\InterlinkParser;
5961
use phpDocumentor\Guides\RestructuredText\Parser\Productions\AnnotationRule;
6062
use phpDocumentor\Guides\RestructuredText\Parser\Productions\BlockQuoteRule;
6163
use phpDocumentor\Guides\RestructuredText\Parser\Productions\CommentRule;
@@ -232,6 +234,8 @@
232234
->set('phpdoc.guides.parser.rst.body_elements', RuleContainer::class)
233235
->set('phpdoc.guides.parser.rst.structural_elements', RuleContainer::class)
234236

237+
->set(InterlinkParser::class, DefaultInterlinkParser::class)
238+
235239
->set(AnnotationRule::class)
236240
->tag('phpdoc.guides.parser.rst.body_element', ['priority' => AnnotationRule::PRIORITY])
237241
->set(LinkRule::class)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Guides\RestructuredText\Parser\Interlink;
6+
7+
use function preg_match;
8+
9+
final class DefaultInterlinkParser implements InterlinkParser
10+
{
11+
/** @see https://regex101.com/r/htMn5p/1 */
12+
private const INTERLINK_REGEX = '/^([a-zA-Z0-9-_]+):(.*$)/';
13+
14+
public function extractInterlink(string $fullReference): InterlinkData
15+
{
16+
if (!preg_match(self::INTERLINK_REGEX, $fullReference, $matches)) {
17+
return new InterlinkData($fullReference, '');
18+
}
19+
20+
return new InterlinkData($matches[2], $matches[1] ?? '');
21+
}
22+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Guides\RestructuredText\Parser\Interlink;
6+
7+
class InterlinkData
8+
{
9+
public function __construct(
10+
public readonly string $reference,
11+
public readonly string $interlink,
12+
) {
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace phpDocumentor\Guides\RestructuredText\Parser\Interlink;
6+
7+
interface InterlinkParser
8+
{
9+
public function extractInterlink(string $fullReference): InterlinkData;
10+
}

packages/guides-restructured-text/src/RestructuredText/TextRoles/AbstractReferenceTextRole.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ abstract class AbstractReferenceTextRole implements TextRole
1313
{
1414
use EmbeddedUriParser;
1515

16-
/** @see https://regex101.com/r/htMn5p/1 */
17-
public const INTERLINK_REGEX = '/^([a-zA-Z0-9-_]+):(.*$)/';
18-
1916
public function processNode(
2017
DocumentParserContext $documentParserContext,
2118
string $role,

packages/guides-restructured-text/src/RestructuredText/TextRoles/DocReferenceTextRole.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66

77
use phpDocumentor\Guides\Nodes\Inline\AbstractLinkInlineNode;
88
use phpDocumentor\Guides\Nodes\Inline\DocReferenceNode;
9-
10-
use function preg_match;
9+
use phpDocumentor\Guides\RestructuredText\Parser\Interlink\InterlinkParser;
1110

1211
class DocReferenceTextRole extends AbstractReferenceTextRole
1312
{
1413
final public const NAME = 'doc';
1514

15+
public function __construct(
16+
private readonly InterlinkParser $interlinkParser,
17+
) {
18+
}
19+
1620
public function getName(): string
1721
{
1822
return self::NAME;
@@ -27,15 +31,8 @@ public function getAliases(): array
2731
/** @return DocReferenceNode */
2832
protected function createNode(string $referenceTarget, string|null $referenceName, string $role): AbstractLinkInlineNode
2933
{
30-
$pattern = AbstractReferenceTextRole::INTERLINK_REGEX;
31-
if (preg_match($pattern, $referenceTarget, $matches)) {
32-
$interlinkDomain = $matches[1];
33-
$path = $matches[2];
34-
} else {
35-
$interlinkDomain = '';
36-
$path = $referenceTarget;
37-
}
38-
39-
return new DocReferenceNode($path, $referenceName ?? '', $interlinkDomain);
34+
$interlinkData = $this->interlinkParser->extractInterlink($referenceTarget);
35+
36+
return new DocReferenceNode($interlinkData->reference, $referenceName ?? '', $interlinkData->interlink);
4037
}
4138
}

packages/guides-restructured-text/src/RestructuredText/TextRoles/GenericReferenceTextRole.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
use phpDocumentor\Guides\Nodes\Inline\AbstractLinkInlineNode;
88
use phpDocumentor\Guides\Nodes\Inline\ReferenceNode;
99
use phpDocumentor\Guides\ReferenceResolvers\AnchorReducer;
10+
use phpDocumentor\Guides\RestructuredText\Parser\Interlink\InterlinkParser;
1011

1112
use function array_keys;
12-
use function preg_match;
1313

1414
class GenericReferenceTextRole extends AbstractReferenceTextRole
1515
{
1616
public function __construct(
1717
private readonly GenericLinkProvider $genericLinkProvider,
1818
private readonly AnchorReducer $anchorReducer,
19+
private readonly InterlinkParser $interlinkParser,
1920
) {
2021
}
2122

@@ -34,15 +35,9 @@ public function getAliases(): array
3435
protected function createNode(string $referenceTarget, string|null $referenceName, string $role): AbstractLinkInlineNode
3536
{
3637
$linkType = $this->genericLinkProvider->getLinkType($role);
37-
$pattern = '/^([a-zA-Z0-9]+):(.*$)/';
38-
if (preg_match(AbstractReferenceTextRole::INTERLINK_REGEX, $referenceTarget, $matches)) {
39-
$interlinkDomain = $matches[1];
40-
$id = $this->anchorReducer->reduceAnchor($matches[2]);
41-
} else {
42-
$interlinkDomain = '';
43-
$id = $this->anchorReducer->reduceAnchor($referenceTarget);
44-
}
45-
46-
return new ReferenceNode($id, $referenceName ?? '', $interlinkDomain, $linkType);
38+
$interlinkData = $this->interlinkParser->extractInterlink($referenceTarget);
39+
$reference = $this->anchorReducer->reduceAnchor($interlinkData->reference);
40+
41+
return new ReferenceNode($reference, $referenceName ?? '', $interlinkData->interlink, $linkType);
4742
}
4843
}

packages/guides-restructured-text/tests/unit/Parser/InlineTokenParserTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use phpDocumentor\Guides\Nodes\InlineCompoundNode;
1818
use phpDocumentor\Guides\ParserContext;
1919
use phpDocumentor\Guides\RestructuredText\MarkupLanguageParser;
20+
use phpDocumentor\Guides\RestructuredText\Parser\Interlink\DefaultInterlinkParser;
2021
use phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules\AnnotationRoleRule;
2122
use phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules\AnonymousPhraseRule;
2223
use phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules\AnonymousReferenceRule;
@@ -53,7 +54,7 @@ public function setUp(): void
5354
new LiteralTextRole(),
5455
[
5556
new ReferenceTextRole(),
56-
new DocReferenceTextRole(),
57+
new DocReferenceTextRole(new DefaultInterlinkParser()),
5758
],
5859
);
5960
$this->documentParserContext = new DocumentParserContext(

packages/guides-restructured-text/tests/unit/TextRoles/DocReferenceTextRoleTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use phpDocumentor\Guides\Nodes\Inline\DocReferenceNode;
88
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
9+
use phpDocumentor\Guides\RestructuredText\Parser\Interlink\DefaultInterlinkParser;
910
use PHPUnit\Framework\Attributes\DataProvider;
1011
use PHPUnit\Framework\MockObject\MockObject;
1112
use PHPUnit\Framework\TestCase;
@@ -18,7 +19,7 @@ class DocReferenceTextRoleTest extends TestCase
1819
public function setUp(): void
1920
{
2021
$this->documentParserContext = $this->createMock(DocumentParserContext::class);
21-
$this->docReferenceTextRole = new DocReferenceTextRole();
22+
$this->docReferenceTextRole = new DocReferenceTextRole(new DefaultInterlinkParser());
2223
}
2324

2425
#[DataProvider('docReferenceProvider')]

0 commit comments

Comments
 (0)