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
|
|
|
/** |
18
|
|
|
* Converts CommonMark-compatible Markdown to HTML. |
19
|
|
|
*/ |
20
|
|
|
class Converter |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The document parser instance. |
24
|
|
|
* |
25
|
|
|
* @var \League\CommonMark\DocParser |
26
|
|
|
*/ |
27
|
|
|
protected $docParser; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The html renderer instance. |
31
|
|
|
* |
32
|
|
|
* @var \League\CommonMark\ElementRendererInterface |
33
|
|
|
*/ |
34
|
|
|
protected $htmlRenderer; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create a new commonmark converter instance. |
38
|
|
|
* |
39
|
|
|
* @param DocParser $docParser |
40
|
|
|
* @param ElementRendererInterface $htmlRenderer |
41
|
|
|
*/ |
42
|
1863 |
|
public function __construct(DocParser $docParser, ElementRendererInterface $htmlRenderer) |
43
|
|
|
{ |
44
|
1863 |
|
$this->docParser = $docParser; |
45
|
1863 |
|
$this->htmlRenderer = $htmlRenderer; |
46
|
1863 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Converts CommonMark to HTML. |
50
|
|
|
* |
51
|
|
|
* @param string $commonMark |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
* |
55
|
|
|
* @api |
56
|
|
|
*/ |
57
|
1854 |
|
public function convertToHtml($commonMark) |
58
|
|
|
{ |
59
|
1854 |
|
$documentAST = $this->docParser->parse($commonMark); |
60
|
|
|
|
61
|
1854 |
|
return $this->htmlRenderer->renderBlock($documentAST); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Converts CommonMark to HTML. |
66
|
|
|
* |
67
|
|
|
* @see Converter::convertToHtml |
68
|
|
|
* |
69
|
|
|
* @param $commonMark |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
3 |
|
public function __invoke($commonMark) |
74
|
|
|
{ |
75
|
3 |
|
return $this->convertToHtml($commonMark); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|