| 148 |
lars |
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 <colinodell@gmail.com>
|
|
|
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\FrontMatter;
|
|
|
15 |
|
|
|
16 |
use League\CommonMark\Environment\EnvironmentBuilderInterface;
|
|
|
17 |
use League\CommonMark\Event\DocumentPreParsedEvent;
|
|
|
18 |
use League\CommonMark\Event\DocumentRenderedEvent;
|
|
|
19 |
use League\CommonMark\Extension\ExtensionInterface;
|
|
|
20 |
use League\CommonMark\Extension\FrontMatter\Data\FrontMatterDataParserInterface;
|
|
|
21 |
use League\CommonMark\Extension\FrontMatter\Data\LibYamlFrontMatterParser;
|
|
|
22 |
use League\CommonMark\Extension\FrontMatter\Data\SymfonyYamlFrontMatterParser;
|
|
|
23 |
use League\CommonMark\Extension\FrontMatter\Listener\FrontMatterPostRenderListener;
|
|
|
24 |
use League\CommonMark\Extension\FrontMatter\Listener\FrontMatterPreParser;
|
|
|
25 |
|
|
|
26 |
final class FrontMatterExtension implements ExtensionInterface
|
|
|
27 |
{
|
|
|
28 |
/** @psalm-readonly */
|
|
|
29 |
private FrontMatterParserInterface $frontMatterParser;
|
|
|
30 |
|
|
|
31 |
public function __construct(?FrontMatterDataParserInterface $dataParser = null)
|
|
|
32 |
{
|
|
|
33 |
$this->frontMatterParser = new FrontMatterParser($dataParser ?? LibYamlFrontMatterParser::capable() ?? new SymfonyYamlFrontMatterParser());
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
public function getFrontMatterParser(): FrontMatterParserInterface
|
|
|
37 |
{
|
|
|
38 |
return $this->frontMatterParser;
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
public function register(EnvironmentBuilderInterface $environment): void
|
|
|
42 |
{
|
|
|
43 |
$environment->addEventListener(DocumentPreParsedEvent::class, new FrontMatterPreParser($this->frontMatterParser));
|
|
|
44 |
$environment->addEventListener(DocumentRenderedEvent::class, new FrontMatterPostRenderListener(), -500);
|
|
|
45 |
}
|
|
|
46 |
}
|