1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This is part of the webuni/commonmark-twig-renderer package. |
5
|
|
|
* |
6
|
|
|
* (c) Martin Hasoň <[email protected]> |
7
|
|
|
* (c) Webuni s.r.o. <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Webuni\CommonMark\TwigRenderer; |
14
|
|
|
|
15
|
|
|
use League\CommonMark\Node\Node; |
16
|
|
|
use Twig\Extension\AbstractExtension; |
17
|
|
|
use Twig\TwigFilter; |
18
|
|
|
|
19
|
|
|
class CommonMarkExtension extends AbstractExtension |
20
|
|
|
{ |
21
|
|
|
private static $cache = []; |
22
|
|
|
|
23
|
|
|
public function getFilters(): array |
24
|
|
|
{ |
25
|
|
|
return [ |
26
|
|
|
new TwigFilter('commonmark_block_name', [$this, 'getBlockName']), |
27
|
|
|
new TwigFilter('preg_replace', [$this, 'pregReplace']), |
28
|
|
|
]; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getBlockName(Node $node): string |
32
|
|
|
{ |
33
|
|
|
$class = get_class($node); |
34
|
|
|
if (!isset(self::$cache[$class])) { |
35
|
|
|
$ref = new \ReflectionClass($class); |
36
|
|
|
self::$cache[$class] = strtolower(preg_replace('/((?<=[a-z]|\d)[A-Z]|(?<!^)[A-Z](?=[a-z]))/', '_\\1', $ref->getShortName())); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return self::$cache[$class]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function pregReplace($subject, $pattern, $replacement = '', $limit = -1): string |
43
|
|
|
{ |
44
|
|
|
return preg_replace($pattern, $replacement, $subject, $limit); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|