Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TASK] Apply breaking changes of guides to textroles #34

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 25 additions & 56 deletions src/TextRoles/PhpComponentTextRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>
Expand All @@ -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 */
Expand All @@ -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];
}
}