Commaaa2
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
@@ -14,78 +16,72 @@ namespace League\CommonMark\Extension\Mention;
|
||||
use League\CommonMark\Extension\Mention\Generator\CallbackGenerator;
|
||||
use League\CommonMark\Extension\Mention\Generator\MentionGeneratorInterface;
|
||||
use League\CommonMark\Extension\Mention\Generator\StringTemplateLinkGenerator;
|
||||
use League\CommonMark\Inline\Parser\InlineParserInterface;
|
||||
use League\CommonMark\InlineParserContext;
|
||||
use League\CommonMark\Parser\Inline\InlineParserInterface;
|
||||
use League\CommonMark\Parser\Inline\InlineParserMatch;
|
||||
use League\CommonMark\Parser\InlineParserContext;
|
||||
|
||||
final class MentionParser implements InlineParserInterface
|
||||
{
|
||||
/** @var string */
|
||||
private $symbol;
|
||||
/** @psalm-readonly */
|
||||
private string $name;
|
||||
|
||||
/** @var string */
|
||||
private $mentionRegex;
|
||||
/** @psalm-readonly */
|
||||
private string $prefix;
|
||||
|
||||
/** @var MentionGeneratorInterface */
|
||||
private $mentionGenerator;
|
||||
/** @psalm-readonly */
|
||||
private string $identifierPattern;
|
||||
|
||||
public function __construct(string $symbol, string $mentionRegex, MentionGeneratorInterface $mentionGenerator)
|
||||
/** @psalm-readonly */
|
||||
private MentionGeneratorInterface $mentionGenerator;
|
||||
|
||||
public function __construct(string $name, string $prefix, string $identifierPattern, MentionGeneratorInterface $mentionGenerator)
|
||||
{
|
||||
$this->symbol = $symbol;
|
||||
$this->mentionRegex = $mentionRegex;
|
||||
$this->mentionGenerator = $mentionGenerator;
|
||||
$this->name = $name;
|
||||
$this->prefix = $prefix;
|
||||
$this->identifierPattern = $identifierPattern;
|
||||
$this->mentionGenerator = $mentionGenerator;
|
||||
}
|
||||
|
||||
public function getCharacters(): array
|
||||
public function getMatchDefinition(): InlineParserMatch
|
||||
{
|
||||
return [$this->symbol];
|
||||
return InlineParserMatch::join(
|
||||
InlineParserMatch::string($this->prefix),
|
||||
InlineParserMatch::regex($this->identifierPattern)
|
||||
);
|
||||
}
|
||||
|
||||
public function parse(InlineParserContext $inlineContext): bool
|
||||
{
|
||||
$cursor = $inlineContext->getCursor();
|
||||
|
||||
// The symbol must not have any other characters immediately prior
|
||||
// The prefix must not have any other characters immediately prior
|
||||
$previousChar = $cursor->peek(-1);
|
||||
if ($previousChar !== null && \preg_match('/\w/', $previousChar)) {
|
||||
// peek() doesn't modify the cursor, so no need to restore state first
|
||||
return false;
|
||||
}
|
||||
|
||||
// Save the cursor state in case we need to rewind and bail
|
||||
$previousState = $cursor->saveState();
|
||||
[$prefix, $identifier] = $inlineContext->getSubMatches();
|
||||
|
||||
// Advance past the symbol to keep parsing simpler
|
||||
$cursor->advance();
|
||||
|
||||
// Parse the mention match value
|
||||
$identifier = $cursor->match($this->mentionRegex);
|
||||
if ($identifier === null) {
|
||||
// Regex failed to match; this isn't a valid mention
|
||||
$cursor->restoreState($previousState);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$mention = $this->mentionGenerator->generateMention(new Mention($this->symbol, $identifier));
|
||||
$mention = $this->mentionGenerator->generateMention(new Mention($this->name, $prefix, $identifier));
|
||||
|
||||
if ($mention === null) {
|
||||
$cursor->restoreState($previousState);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$cursor->advanceBy($inlineContext->getFullMatchLength());
|
||||
$inlineContext->getContainer()->appendChild($mention);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function createWithStringTemplate(string $symbol, string $mentionRegex, string $urlTemplate): MentionParser
|
||||
public static function createWithStringTemplate(string $name, string $prefix, string $mentionRegex, string $urlTemplate): MentionParser
|
||||
{
|
||||
return new self($symbol, $mentionRegex, new StringTemplateLinkGenerator($urlTemplate));
|
||||
return new self($name, $prefix, $mentionRegex, new StringTemplateLinkGenerator($urlTemplate));
|
||||
}
|
||||
|
||||
public static function createWithCallback(string $symbol, string $mentionRegex, callable $callback): MentionParser
|
||||
public static function createWithCallback(string $name, string $prefix, string $mentionRegex, callable $callback): MentionParser
|
||||
{
|
||||
return new self($symbol, $mentionRegex, new CallbackGenerator($callback));
|
||||
return new self($name, $prefix, $mentionRegex, new CallbackGenerator($callback));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user