refactor: susun semula struktur folder — Laravel source ke src/
This commit is contained in:
54
vendor/league/commonmark/src/Extension/Mention/Generator/CallbackGenerator.php
vendored
Normal file
54
vendor/league/commonmark/src/Extension/Mention/Generator/CallbackGenerator.php
vendored
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
* (c) Colin O'Dell <colinodell@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace League\CommonMark\Extension\Mention\Generator;
|
||||
|
||||
use League\CommonMark\Exception\LogicException;
|
||||
use League\CommonMark\Extension\Mention\Mention;
|
||||
use League\CommonMark\Node\Inline\AbstractInline;
|
||||
|
||||
final class CallbackGenerator implements MentionGeneratorInterface
|
||||
{
|
||||
/**
|
||||
* A callback function which sets the URL on the passed mention and returns the mention, return a new AbstractInline based object or null if the mention is not a match
|
||||
*
|
||||
* @var callable(Mention): ?AbstractInline
|
||||
*/
|
||||
private $callback;
|
||||
|
||||
public function __construct(callable $callback)
|
||||
{
|
||||
$this->callback = $callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws LogicException
|
||||
*/
|
||||
public function generateMention(Mention $mention): ?AbstractInline
|
||||
{
|
||||
$result = \call_user_func($this->callback, $mention);
|
||||
if ($result === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($result instanceof AbstractInline && ! ($result instanceof Mention)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
if ($result instanceof Mention && $result->hasUrl()) {
|
||||
return $mention;
|
||||
}
|
||||
|
||||
throw new LogicException('CallbackGenerator callable must set the URL on the passed mention and return the mention, return a new AbstractInline based object or null if the mention is not a match');
|
||||
}
|
||||
}
|
||||
22
vendor/league/commonmark/src/Extension/Mention/Generator/MentionGeneratorInterface.php
vendored
Normal file
22
vendor/league/commonmark/src/Extension/Mention/Generator/MentionGeneratorInterface.php
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
* (c) Colin O'Dell <colinodell@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace League\CommonMark\Extension\Mention\Generator;
|
||||
|
||||
use League\CommonMark\Extension\Mention\Mention;
|
||||
use League\CommonMark\Node\Inline\AbstractInline;
|
||||
|
||||
interface MentionGeneratorInterface
|
||||
{
|
||||
public function generateMention(Mention $mention): ?AbstractInline;
|
||||
}
|
||||
34
vendor/league/commonmark/src/Extension/Mention/Generator/StringTemplateLinkGenerator.php
vendored
Normal file
34
vendor/league/commonmark/src/Extension/Mention/Generator/StringTemplateLinkGenerator.php
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
* (c) Colin O'Dell <colinodell@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace League\CommonMark\Extension\Mention\Generator;
|
||||
|
||||
use League\CommonMark\Extension\Mention\Mention;
|
||||
use League\CommonMark\Node\Inline\AbstractInline;
|
||||
|
||||
final class StringTemplateLinkGenerator implements MentionGeneratorInterface
|
||||
{
|
||||
private string $urlTemplate;
|
||||
|
||||
public function __construct(string $urlTemplate)
|
||||
{
|
||||
$this->urlTemplate = $urlTemplate;
|
||||
}
|
||||
|
||||
public function generateMention(Mention $mention): ?AbstractInline
|
||||
{
|
||||
$mention->setUrl(\sprintf($this->urlTemplate, $mention->getIdentifier()));
|
||||
|
||||
return $mention;
|
||||
}
|
||||
}
|
||||
93
vendor/league/commonmark/src/Extension/Mention/Mention.php
vendored
Normal file
93
vendor/league/commonmark/src/Extension/Mention/Mention.php
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
* (c) Colin O'Dell <colinodell@gmail.com>
|
||||
*
|
||||
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
|
||||
* - (c) John MacFarlane
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace League\CommonMark\Extension\Mention;
|
||||
|
||||
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
|
||||
use League\CommonMark\Node\Inline\Text;
|
||||
|
||||
class Mention extends Link
|
||||
{
|
||||
private string $name;
|
||||
|
||||
private string $prefix;
|
||||
|
||||
private string $identifier;
|
||||
|
||||
public function __construct(string $name, string $prefix, string $identifier, ?string $label = null)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->prefix = $prefix;
|
||||
$this->identifier = $identifier;
|
||||
|
||||
parent::__construct('', $label ?? \sprintf('%s%s', $prefix, $identifier));
|
||||
}
|
||||
|
||||
public function getLabel(): ?string
|
||||
{
|
||||
if (($labelNode = $this->findLabelNode()) === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $labelNode->getLiteral();
|
||||
}
|
||||
|
||||
public function getIdentifier(): string
|
||||
{
|
||||
return $this->identifier;
|
||||
}
|
||||
|
||||
public function getName(): ?string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getPrefix(): string
|
||||
{
|
||||
return $this->prefix;
|
||||
}
|
||||
|
||||
public function hasUrl(): bool
|
||||
{
|
||||
return $this->url !== '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setLabel(string $label): self
|
||||
{
|
||||
if (($labelNode = $this->findLabelNode()) === null) {
|
||||
$labelNode = new Text();
|
||||
$this->prependChild($labelNode);
|
||||
}
|
||||
|
||||
$labelNode->setLiteral($label);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function findLabelNode(): ?Text
|
||||
{
|
||||
foreach ($this->children() as $child) {
|
||||
if ($child instanceof Text) {
|
||||
return $child;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
61
vendor/league/commonmark/src/Extension/Mention/MentionExtension.php
vendored
Normal file
61
vendor/league/commonmark/src/Extension/Mention/MentionExtension.php
vendored
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
* (c) Colin O'Dell <colinodell@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace League\CommonMark\Extension\Mention;
|
||||
|
||||
use League\CommonMark\Environment\EnvironmentBuilderInterface;
|
||||
use League\CommonMark\Extension\ConfigurableExtensionInterface;
|
||||
use League\CommonMark\Extension\Mention\Generator\MentionGeneratorInterface;
|
||||
use League\Config\ConfigurationBuilderInterface;
|
||||
use League\Config\Exception\InvalidConfigurationException;
|
||||
use Nette\Schema\Expect;
|
||||
|
||||
final class MentionExtension implements ConfigurableExtensionInterface
|
||||
{
|
||||
public function configureSchema(ConfigurationBuilderInterface $builder): void
|
||||
{
|
||||
$isAValidPartialRegex = static function (string $regex): bool {
|
||||
$regex = '/' . $regex . '/i';
|
||||
|
||||
return @\preg_match($regex, '') !== false;
|
||||
};
|
||||
|
||||
$builder->addSchema('mentions', Expect::arrayOf(
|
||||
Expect::structure([
|
||||
'prefix' => Expect::string()->required(),
|
||||
'pattern' => Expect::string()->assert($isAValidPartialRegex, 'Pattern must not include starting/ending delimiters (like "/")')->required(),
|
||||
'generator' => Expect::anyOf(
|
||||
Expect::type(MentionGeneratorInterface::class),
|
||||
Expect::string(),
|
||||
Expect::type('callable')
|
||||
)->required(),
|
||||
])
|
||||
));
|
||||
}
|
||||
|
||||
public function register(EnvironmentBuilderInterface $environment): void
|
||||
{
|
||||
$mentions = $environment->getConfiguration()->get('mentions');
|
||||
foreach ($mentions as $name => $mention) {
|
||||
if ($mention['generator'] instanceof MentionGeneratorInterface) {
|
||||
$environment->addInlineParser(new MentionParser($name, $mention['prefix'], $mention['pattern'], $mention['generator']));
|
||||
} elseif (\is_string($mention['generator'])) {
|
||||
$environment->addInlineParser(MentionParser::createWithStringTemplate($name, $mention['prefix'], $mention['pattern'], $mention['generator']));
|
||||
} elseif (\is_callable($mention['generator'])) {
|
||||
$environment->addInlineParser(MentionParser::createWithCallback($name, $mention['prefix'], $mention['pattern'], $mention['generator']));
|
||||
} else {
|
||||
throw new InvalidConfigurationException(\sprintf('The "generator" provided for the "%s" MentionParser configuration must be a string template, callable, or an object that implements %s.', $name, MentionGeneratorInterface::class));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
87
vendor/league/commonmark/src/Extension/Mention/MentionParser.php
vendored
Normal file
87
vendor/league/commonmark/src/Extension/Mention/MentionParser.php
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the league/commonmark package.
|
||||
*
|
||||
* (c) Colin O'Dell <colinodell@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
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\Parser\Inline\InlineParserInterface;
|
||||
use League\CommonMark\Parser\Inline\InlineParserMatch;
|
||||
use League\CommonMark\Parser\InlineParserContext;
|
||||
|
||||
final class MentionParser implements InlineParserInterface
|
||||
{
|
||||
/** @psalm-readonly */
|
||||
private string $name;
|
||||
|
||||
/** @psalm-readonly */
|
||||
private string $prefix;
|
||||
|
||||
/** @psalm-readonly */
|
||||
private string $identifierPattern;
|
||||
|
||||
/** @psalm-readonly */
|
||||
private MentionGeneratorInterface $mentionGenerator;
|
||||
|
||||
public function __construct(string $name, string $prefix, string $identifierPattern, MentionGeneratorInterface $mentionGenerator)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->prefix = $prefix;
|
||||
$this->identifierPattern = $identifierPattern;
|
||||
$this->mentionGenerator = $mentionGenerator;
|
||||
}
|
||||
|
||||
public function getMatchDefinition(): InlineParserMatch
|
||||
{
|
||||
return InlineParserMatch::join(
|
||||
InlineParserMatch::string($this->prefix),
|
||||
InlineParserMatch::regex($this->identifierPattern)
|
||||
);
|
||||
}
|
||||
|
||||
public function parse(InlineParserContext $inlineContext): bool
|
||||
{
|
||||
$cursor = $inlineContext->getCursor();
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
[$prefix, $identifier] = $inlineContext->getSubMatches();
|
||||
|
||||
$mention = $this->mentionGenerator->generateMention(new Mention($this->name, $prefix, $identifier));
|
||||
|
||||
if ($mention === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$cursor->advanceBy($inlineContext->getFullMatchLength());
|
||||
$inlineContext->getContainer()->appendChild($mention);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function createWithStringTemplate(string $name, string $prefix, string $mentionRegex, string $urlTemplate): MentionParser
|
||||
{
|
||||
return new self($name, $prefix, $mentionRegex, new StringTemplateLinkGenerator($urlTemplate));
|
||||
}
|
||||
|
||||
public static function createWithCallback(string $name, string $prefix, string $mentionRegex, callable $callback): MentionParser
|
||||
{
|
||||
return new self($name, $prefix, $mentionRegex, new CallbackGenerator($callback));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user