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\Renderer\Block; |
16
|
|
|
|
17
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock; |
18
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem; |
19
|
|
|
use League\CommonMark\Node\Block\Paragraph; |
20
|
|
|
use League\CommonMark\Node\Node; |
21
|
|
|
use League\CommonMark\Renderer\ChildNodeRendererInterface; |
22
|
|
|
use League\CommonMark\Renderer\NodeRendererInterface; |
23
|
|
|
use League\CommonMark\Util\HtmlElement; |
24
|
|
|
|
25
|
|
|
final class ParagraphRenderer implements NodeRendererInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param Paragraph $node |
29
|
|
|
* @param ChildNodeRendererInterface $childRenderer |
30
|
|
|
* |
31
|
|
|
* @return HtmlElement|string |
32
|
|
|
*/ |
33
|
2028 |
|
public function render(Node $node, ChildNodeRendererInterface $childRenderer) |
34
|
|
|
{ |
35
|
2028 |
|
if (!($node instanceof Paragraph)) { |
36
|
3 |
|
throw new \InvalidArgumentException('Incompatible node type: ' . \get_class($node)); |
37
|
|
|
} |
38
|
|
|
|
39
|
2025 |
|
if ($this->inTightList($node)) { |
40
|
165 |
|
return $childRenderer->renderNodes($node->children()); |
41
|
|
|
} |
42
|
|
|
|
43
|
1908 |
|
$attrs = $node->getData('attributes', []); |
44
|
|
|
|
45
|
1908 |
|
return new HtmlElement('p', $attrs, $childRenderer->renderNodes($node->children())); |
46
|
|
|
} |
47
|
|
|
|
48
|
2025 |
|
private function inTightList(Paragraph $node): bool |
49
|
|
|
{ |
50
|
2025 |
|
$parent = $node->parent(); |
51
|
2025 |
|
if (!$parent instanceof ListItem) { |
52
|
1815 |
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
279 |
|
$gramps = $parent->parent(); |
56
|
279 |
|
if (!$gramps instanceof ListBlock) { |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
279 |
|
return $gramps->isTight(); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|