Skip to content

Commit

Permalink
[FEATURE] Introduce exchangeable InterlinkParser
Browse files Browse the repository at this point in the history
In TYPO3 we want to allow additonal special signs, so that interlinks to composer names can be made.

Additionally, this also removed duplicate code
  • Loading branch information
linawolf committed Dec 30, 2023
1 parent bb3a291 commit 738079c
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@
use phpDocumentor\Guides\RestructuredText\Directives\VersionChangedDirective;
use phpDocumentor\Guides\RestructuredText\Directives\WarningDirective;
use phpDocumentor\Guides\RestructuredText\MarkupLanguageParser;
use phpDocumentor\Guides\RestructuredText\Parser\DefaultInterlinkParser;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContextFactory;
use phpDocumentor\Guides\RestructuredText\Parser\InlineParser;
use phpDocumentor\Guides\RestructuredText\Parser\InterlinkParser;
use phpDocumentor\Guides\RestructuredText\Parser\Productions\AnnotationRule;
use phpDocumentor\Guides\RestructuredText\Parser\Productions\BlockQuoteRule;
use phpDocumentor\Guides\RestructuredText\Parser\Productions\CommentRule;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Guides\RestructuredText\Parser;

use function preg_match;

final class DefaultInterlinkParser implements InterlinkParser
{
/** @see https://regex101.com/r/htMn5p/1 */
private const INTERLINK_REGEX = '/^([a-zA-Z0-9-_]+):(.*$)/';

/** @return array{interlink:?string,reference:string} */
public function extractInterlink(string $fullReference): array
{
if (!preg_match(self::INTERLINK_REGEX, $fullReference, $matches)) {
return ['interlink' => null, 'reference' => $fullReference];
}

$interlink = $matches[1] === '' ? null : $matches[1];
$reference = $matches[2];

return ['interlink' => $interlink, 'reference' => $reference];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Guides\RestructuredText\Parser;

interface InterlinkParser
{
/** @return array{interlink:?string,reference:string} */
public function extractInterlink(string $fullReference): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ abstract class AbstractReferenceTextRole implements TextRole
{
use EmbeddedUriParser;

/** @see https://regex101.com/r/htMn5p/1 */
public const INTERLINK_REGEX = '/^([a-zA-Z0-9-_]+):(.*$)/';

public function processNode(
DocumentParserContext $documentParserContext,
string $role,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@

use phpDocumentor\Guides\Nodes\Inline\AbstractLinkInlineNode;
use phpDocumentor\Guides\Nodes\Inline\DocReferenceNode;

use function preg_match;
use phpDocumentor\Guides\RestructuredText\Parser\InterlinkParser;

class DocReferenceTextRole extends AbstractReferenceTextRole
{
final public const NAME = 'doc';

public function __construct(
private readonly InterlinkParser $interlinkParser,
) {
}

public function getName(): string
{
return self::NAME;
Expand All @@ -27,15 +31,8 @@ public function getAliases(): array
/** @return DocReferenceNode */
protected function createNode(string $referenceTarget, string|null $referenceName, string $role): AbstractLinkInlineNode
{
$pattern = AbstractReferenceTextRole::INTERLINK_REGEX;
if (preg_match($pattern, $referenceTarget, $matches)) {
$interlinkDomain = $matches[1];
$path = $matches[2];
} else {
$interlinkDomain = '';
$path = $referenceTarget;
}

return new DocReferenceNode($path, $referenceName ?? '', $interlinkDomain);
$parsed = $this->interlinkParser->extractInterlink($referenceTarget);

return new DocReferenceNode($parsed['reference'], $referenceName ?? '', $parsed['interlink'] ?? '');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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\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,
) {
}

Expand All @@ -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);
$parsed = $this->interlinkParser->extractInterlink($referenceTarget);
$reference = $this->anchorReducer->reduceAnchor($parsed['reference']);

return new ReferenceNode($reference, $referenceName ?? '', $parsed['interlink'] ?? '', $linkType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function setUp(): void
new LiteralTextRole(),
[
new ReferenceTextRole(),
new DocReferenceTextRole(),
new DocReferenceTextRole(new DefaultInterlinkParser()),
],
);
$this->documentParserContext = new DocumentParserContext(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace phpDocumentor\Guides\RestructuredText\TextRoles;

use phpDocumentor\Guides\Nodes\Inline\DocReferenceNode;
use phpDocumentor\Guides\RestructuredText\Parser\DefaultInterlinkParser;
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\MockObject\MockObject;
Expand All @@ -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')]
Expand Down

0 comments on commit 738079c

Please sign in to comment.