1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the league/commonmark package. |
5
|
|
|
* |
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js) |
9
|
|
|
* - (c) John MacFarlane |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace League\CommonMark; |
16
|
|
|
|
17
|
|
|
use League\CommonMark\Environment\ConfigurableEnvironmentInterface; |
18
|
|
|
use League\CommonMark\Environment\Environment; |
19
|
|
|
use League\CommonMark\Environment\EnvironmentInterface; |
20
|
|
|
use League\CommonMark\Parser\DocParser; |
21
|
|
|
use League\CommonMark\Parser\DocParserInterface; |
22
|
|
|
use League\CommonMark\Renderer\NodeRendererInterface; |
23
|
|
|
use League\CommonMark\Renderer\HtmlRenderer; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Converts CommonMark-compatible Markdown to HTML. |
27
|
|
|
*/ |
28
|
|
|
class CommonMarkConverter implements MarkdownConverterInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* The currently-installed version. |
32
|
|
|
* |
33
|
|
|
* This might be a typical `x.y.z` version, or `x.y-dev`. |
34
|
|
|
*/ |
35
|
|
|
public const VERSION = '2.0-dev'; |
36
|
|
|
|
37
|
|
|
/** @var EnvironmentInterface */ |
38
|
|
|
protected $environment; |
39
|
|
|
|
40
|
|
|
/** @var DocParserInterface */ |
41
|
|
|
protected $docParser; |
42
|
|
|
|
43
|
|
|
/** @var NodeRendererInterface */ |
44
|
|
|
protected $htmlRenderer; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Create a new commonmark converter instance. |
48
|
|
|
* |
49
|
|
|
* @param array<string, mixed> $config |
50
|
|
|
* @param EnvironmentInterface|null $environment |
51
|
|
|
*/ |
52
|
2397 |
|
public function __construct(array $config = [], EnvironmentInterface $environment = null) |
53
|
|
|
{ |
54
|
2397 |
|
if ($environment === null) { |
55
|
2040 |
|
$environment = Environment::createCommonMarkEnvironment(); |
56
|
|
|
} |
57
|
|
|
|
58
|
2397 |
|
if ($environment instanceof ConfigurableEnvironmentInterface) { |
59
|
2397 |
|
$environment->mergeConfig($config); |
60
|
|
|
} |
61
|
|
|
|
62
|
2397 |
|
$this->environment = $environment; |
63
|
|
|
|
64
|
2397 |
|
$this->docParser = new DocParser($environment); |
65
|
2397 |
|
$this->htmlRenderer = new HtmlRenderer($environment); |
66
|
2397 |
|
} |
67
|
|
|
|
68
|
9 |
|
public function getEnvironment(): EnvironmentInterface |
69
|
|
|
{ |
70
|
9 |
|
return $this->environment; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Converts CommonMark to HTML. |
75
|
|
|
* |
76
|
|
|
* @param string $commonMark |
77
|
|
|
* |
78
|
|
|
* @throws \RuntimeException |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
* |
82
|
|
|
* @api |
83
|
|
|
*/ |
84
|
2388 |
|
public function convertToHtml(string $commonMark): string |
85
|
|
|
{ |
86
|
2388 |
|
$documentAST = $this->docParser->parse($commonMark); |
87
|
|
|
|
88
|
2382 |
|
return $this->htmlRenderer->renderBlock($documentAST); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Converts CommonMark to HTML. |
93
|
|
|
* |
94
|
|
|
* @see Converter::convertToHtml |
95
|
|
|
* |
96
|
|
|
* @param string $commonMark |
97
|
|
|
* |
98
|
|
|
* @throws \RuntimeException |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
3 |
|
public function __invoke(string $commonMark): string |
103
|
|
|
{ |
104
|
3 |
|
return $this->convertToHtml($commonMark); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|