diff --git a/packages/guides-restructured-text/resources/config/guides-restructured-text.php b/packages/guides-restructured-text/resources/config/guides-restructured-text.php index bdd756b1e..d919b5c66 100644 --- a/packages/guides-restructured-text/resources/config/guides-restructured-text.php +++ b/packages/guides-restructured-text/resources/config/guides-restructured-text.php @@ -56,6 +56,8 @@ use phpDocumentor\Guides\RestructuredText\MarkupLanguageParser; use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContextFactory; use phpDocumentor\Guides\RestructuredText\Parser\InlineParser; +use phpDocumentor\Guides\RestructuredText\Parser\Interlink\DefaultInterlinkParser; +use phpDocumentor\Guides\RestructuredText\Parser\Interlink\InterlinkParser; use phpDocumentor\Guides\RestructuredText\Parser\Productions\AnnotationRule; use phpDocumentor\Guides\RestructuredText\Parser\Productions\BlockQuoteRule; use phpDocumentor\Guides\RestructuredText\Parser\Productions\CommentRule; @@ -232,6 +234,8 @@ ->set('phpdoc.guides.parser.rst.body_elements', RuleContainer::class) ->set('phpdoc.guides.parser.rst.structural_elements', RuleContainer::class) + ->set(InterlinkParser::class, DefaultInterlinkParser::class) + ->set(AnnotationRule::class) ->tag('phpdoc.guides.parser.rst.body_element', ['priority' => AnnotationRule::PRIORITY]) ->set(LinkRule::class) diff --git a/packages/guides-restructured-text/src/RestructuredText/Parser/Interlink/DefaultInterlinkParser.php b/packages/guides-restructured-text/src/RestructuredText/Parser/Interlink/DefaultInterlinkParser.php new file mode 100644 index 000000000..fd6285a8f --- /dev/null +++ b/packages/guides-restructured-text/src/RestructuredText/Parser/Interlink/DefaultInterlinkParser.php @@ -0,0 +1,22 @@ +interlinkParser->extractInterlink($referenceTarget); + + return new DocReferenceNode($interlinkData->reference, $referenceName ?? '', $interlinkData->interlink); } } diff --git a/packages/guides-restructured-text/src/RestructuredText/TextRoles/GenericReferenceTextRole.php b/packages/guides-restructured-text/src/RestructuredText/TextRoles/GenericReferenceTextRole.php index 43f9c47f0..677746fd5 100644 --- a/packages/guides-restructured-text/src/RestructuredText/TextRoles/GenericReferenceTextRole.php +++ b/packages/guides-restructured-text/src/RestructuredText/TextRoles/GenericReferenceTextRole.php @@ -7,15 +7,16 @@ use phpDocumentor\Guides\Nodes\Inline\AbstractLinkInlineNode; use phpDocumentor\Guides\Nodes\Inline\ReferenceNode; use phpDocumentor\Guides\ReferenceResolvers\AnchorReducer; +use phpDocumentor\Guides\RestructuredText\Parser\Interlink\InterlinkParser; use function array_keys; -use function preg_match; class GenericReferenceTextRole extends AbstractReferenceTextRole { public function __construct( private readonly GenericLinkProvider $genericLinkProvider, private readonly AnchorReducer $anchorReducer, + private readonly InterlinkParser $interlinkParser, ) { } @@ -34,15 +35,9 @@ public function getAliases(): array protected function createNode(string $referenceTarget, string|null $referenceName, string $role): AbstractLinkInlineNode { $linkType = $this->genericLinkProvider->getLinkType($role); - $pattern = '/^([a-zA-Z0-9]+):(.*$)/'; - if (preg_match(AbstractReferenceTextRole::INTERLINK_REGEX, $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); + $interlinkData = $this->interlinkParser->extractInterlink($referenceTarget); + $reference = $this->anchorReducer->reduceAnchor($interlinkData->reference); + + return new ReferenceNode($reference, $referenceName ?? '', $interlinkData->interlink, $linkType); } } diff --git a/packages/guides-restructured-text/tests/unit/Parser/InlineTokenParserTest.php b/packages/guides-restructured-text/tests/unit/Parser/InlineTokenParserTest.php index 9516dde2c..640a60fcf 100644 --- a/packages/guides-restructured-text/tests/unit/Parser/InlineTokenParserTest.php +++ b/packages/guides-restructured-text/tests/unit/Parser/InlineTokenParserTest.php @@ -17,6 +17,7 @@ use phpDocumentor\Guides\Nodes\InlineCompoundNode; use phpDocumentor\Guides\ParserContext; use phpDocumentor\Guides\RestructuredText\MarkupLanguageParser; +use phpDocumentor\Guides\RestructuredText\Parser\Interlink\DefaultInterlinkParser; use phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules\AnnotationRoleRule; use phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules\AnonymousPhraseRule; use phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules\AnonymousReferenceRule; @@ -53,7 +54,7 @@ public function setUp(): void new LiteralTextRole(), [ new ReferenceTextRole(), - new DocReferenceTextRole(), + new DocReferenceTextRole(new DefaultInterlinkParser()), ], ); $this->documentParserContext = new DocumentParserContext( diff --git a/packages/guides-restructured-text/tests/unit/TextRoles/DocReferenceTextRoleTest.php b/packages/guides-restructured-text/tests/unit/TextRoles/DocReferenceTextRoleTest.php index 700a53856..d0d38fe8d 100644 --- a/packages/guides-restructured-text/tests/unit/TextRoles/DocReferenceTextRoleTest.php +++ b/packages/guides-restructured-text/tests/unit/TextRoles/DocReferenceTextRoleTest.php @@ -6,6 +6,7 @@ use phpDocumentor\Guides\Nodes\Inline\DocReferenceNode; use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext; +use phpDocumentor\Guides\RestructuredText\Parser\Interlink\DefaultInterlinkParser; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -18,7 +19,7 @@ class DocReferenceTextRoleTest extends TestCase public function setUp(): void { $this->documentParserContext = $this->createMock(DocumentParserContext::class); - $this->docReferenceTextRole = new DocReferenceTextRole(); + $this->docReferenceTextRole = new DocReferenceTextRole(new DefaultInterlinkParser()); } #[DataProvider('docReferenceProvider')]