From d21e5c647ee2ed12a0192d27345aa9c9e194dbb5 Mon Sep 17 00:00:00 2001 From: "lina.wolf" Date: Wed, 20 Dec 2023 20:06:15 +0100 Subject: [PATCH] [TASK] Apply breaking changes of guides to textroles With https://github.com/phpDocumentor/guides/pull/761 the textroles where changed to not rely on the lexer anymore. This makes it necessary to switch to a regex. --- src/TextRoles/PhpComponentTextRole.php | 81 ++++++++------------------ 1 file changed, 25 insertions(+), 56 deletions(-) diff --git a/src/TextRoles/PhpComponentTextRole.php b/src/TextRoles/PhpComponentTextRole.php index fec2af3..66b6643 100644 --- a/src/TextRoles/PhpComponentTextRole.php +++ b/src/TextRoles/PhpComponentTextRole.php @@ -19,16 +19,15 @@ abstract class PhpComponentTextRole implements TextRole * @see https://regex101.com/r/OyN05v/1 */ protected const INTERLINK_NAME_REGEX = '/^([a-zA-Z0-9]+):([^:]+.*$)/'; - - private readonly InlineLexer $lexer; + /** + * @see https://regex101.com/r/mqBxQj/1 + */ + protected const TEXTROLE_LINK_REGEX = '/^(.*?)(?:(?:\s|^)<([^<]+)>)?$/s'; public function __construct( protected readonly LoggerInterface $logger, private readonly AnchorReducer $anchorReducer, - ) { - // Do not inject the $lexer. It contains a state. - $this->lexer = new InlineLexer(); - } + ) {} /** * @return list @@ -44,57 +43,9 @@ public function processNode( string $content, string $rawContent, ): AbstractLinkInlineNode { - $referenceTarget = null; - $value = null; - - $part = ''; - $this->lexer->setInput($rawContent); - $this->lexer->moveNext(); - $this->lexer->moveNext(); - while ($this->lexer->token instanceof Token) { - $token = $this->lexer->token; - switch ($token->type) { - case InlineLexer::EMBEDED_URL_START: - $value = trim($part); - $part = ''; - - break; - case InlineLexer::EMBEDED_URL_END: - if ($value === null) { - // not inside the embedded URL - $part .= $token->value; - break; - } - - if ($this->lexer->peek() !== null) { - $this->logger->warning( - sprintf( - 'Reference contains unexpected content after closing `>`: "%s"', - $content, - ), - $documentParserContext->getLoggerInformation(), - ); - } - - $referenceTarget = $part; - $part = ''; - - break 2; - default: - $part .= $token->value; - } - - $this->lexer->moveNext(); - } + $parsed = $this->extractEmbeddedUri($rawContent); - $value .= trim($part); - - if ($referenceTarget === null) { - $referenceTarget = $value; - $value = null; - } - - return $this->createNode($documentParserContext, $referenceTarget, $value, $role); + return $this->createNode($documentParserContext, $parsed['uri'], $parsed['text'], $role); } /** @return ReferenceNode */ @@ -110,4 +61,22 @@ protected function createNode(DocumentParserContext $documentParserContext, stri return new ReferenceNode($id, $referenceName ?? '', $interlinkDomain, 'php:' . $this->getName()); } + + /** @return array{text:?string,uri:string} */ + private function extractEmbeddedUri(string $text): array + { + preg_match(self::TEXTROLE_LINK_REGEX, $text, $matches); + + $text = $matches[1] === '' ? null : $matches[1]; + $uri = $matches[1]; + + if (isset($matches[2])) { + // there is an embedded URI, text and URI are different + $uri = $matches[2]; + } else { + $text = null; + } + + return ['text' => $text, 'uri' => $uri]; + } }