|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the league/commonmark package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Colin O'Dell <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace League\CommonMark\Extension\Mention; |
|
15
|
|
|
|
|
16
|
|
|
use League\CommonMark\Environment\EnvironmentBuilderInterface; |
|
17
|
|
|
use League\CommonMark\Extension\ConfigurableExtensionInterface; |
|
18
|
|
|
use League\CommonMark\Extension\Mention\Generator\MentionGeneratorInterface; |
|
19
|
|
|
use League\Config\ConfigurationBuilderInterface; |
|
20
|
|
|
use League\Config\Exception\InvalidConfigurationException; |
|
21
|
|
|
use Nette\Schema\Expect; |
|
22
|
|
|
|
|
23
|
|
|
final class MentionExtension implements ConfigurableExtensionInterface |
|
24
|
|
|
{ |
|
25
|
18 |
|
public function configureSchema(ConfigurationBuilderInterface $builder): void |
|
26
|
|
|
{ |
|
27
|
18 |
|
$isAValidPartialRegex = static function (string $regex): bool { |
|
28
|
16 |
|
$regex = '/' . $regex . '/i'; |
|
29
|
|
|
|
|
30
|
16 |
|
return @\preg_match($regex, '') !== false; |
|
31
|
|
|
}; |
|
32
|
|
|
|
|
33
|
18 |
|
$builder->addSchema('mentions', Expect::arrayOf( |
|
34
|
18 |
|
Expect::structure([ |
|
35
|
18 |
|
'prefix' => Expect::string()->required(), |
|
36
|
18 |
|
'pattern' => Expect::string()->assert($isAValidPartialRegex, 'Pattern must not include starting/ending delimiters (like "/")')->required(), |
|
37
|
18 |
|
'generator' => Expect::anyOf( |
|
38
|
18 |
|
Expect::type(MentionGeneratorInterface::class), |
|
39
|
18 |
|
Expect::string(), |
|
40
|
18 |
|
Expect::type('callable') |
|
41
|
18 |
|
)->required(), |
|
42
|
|
|
]) |
|
43
|
|
|
)); |
|
44
|
18 |
|
} |
|
45
|
|
|
|
|
46
|
12 |
|
public function register(EnvironmentBuilderInterface $environment): void |
|
47
|
|
|
{ |
|
48
|
12 |
|
$mentions = $environment->getConfiguration()->get('mentions'); |
|
49
|
12 |
|
foreach ($mentions as $name => $mention) { |
|
50
|
10 |
|
if ($mention['generator'] instanceof MentionGeneratorInterface) { |
|
51
|
4 |
|
$environment->addInlineParser(new MentionParser($name, $mention['prefix'], $mention['pattern'], $mention['generator'])); |
|
52
|
6 |
|
} elseif (\is_string($mention['generator'])) { |
|
53
|
4 |
|
$environment->addInlineParser(MentionParser::createWithStringTemplate($name, $mention['prefix'], $mention['pattern'], $mention['generator'])); |
|
54
|
2 |
|
} elseif (\is_callable($mention['generator'])) { |
|
55
|
2 |
|
$environment->addInlineParser(MentionParser::createWithCallback($name, $mention['prefix'], $mention['pattern'], $mention['generator'])); |
|
56
|
|
|
} else { |
|
57
|
|
|
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)); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
12 |
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|