| 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 |
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
|
|
|
11 |
* - (c) John MacFarlane
|
|
|
12 |
*
|
|
|
13 |
* For the full copyright and license information, please view the LICENSE
|
|
|
14 |
* file that was distributed with this source code.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
namespace League\CommonMark\Renderer\Block;
|
|
|
18 |
|
|
|
19 |
use League\CommonMark\Node\Block\Paragraph;
|
|
|
20 |
use League\CommonMark\Node\Block\TightBlockInterface;
|
|
|
21 |
use League\CommonMark\Node\Node;
|
|
|
22 |
use League\CommonMark\Renderer\ChildNodeRendererInterface;
|
|
|
23 |
use League\CommonMark\Renderer\NodeRendererInterface;
|
|
|
24 |
use League\CommonMark\Util\HtmlElement;
|
|
|
25 |
use League\CommonMark\Xml\XmlNodeRendererInterface;
|
|
|
26 |
|
|
|
27 |
final class ParagraphRenderer implements NodeRendererInterface, XmlNodeRendererInterface
|
|
|
28 |
{
|
|
|
29 |
/**
|
|
|
30 |
* @param Paragraph $node
|
|
|
31 |
*
|
|
|
32 |
* {@inheritDoc}
|
|
|
33 |
*
|
|
|
34 |
* @psalm-suppress MoreSpecificImplementedParamType
|
|
|
35 |
*/
|
|
|
36 |
public function render(Node $node, ChildNodeRendererInterface $childRenderer)
|
|
|
37 |
{
|
|
|
38 |
Paragraph::assertInstanceOf($node);
|
|
|
39 |
|
|
|
40 |
if ($this->inTightList($node)) {
|
|
|
41 |
return $childRenderer->renderNodes($node->children());
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
$attrs = $node->data->get('attributes');
|
|
|
45 |
|
|
|
46 |
return new HtmlElement('p', $attrs, $childRenderer->renderNodes($node->children()));
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
public function getXmlTagName(Node $node): string
|
|
|
50 |
{
|
|
|
51 |
return 'paragraph';
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* {@inheritDoc}
|
|
|
56 |
*/
|
|
|
57 |
public function getXmlAttributes(Node $node): array
|
|
|
58 |
{
|
|
|
59 |
return [];
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
private function inTightList(Paragraph $node): bool
|
|
|
63 |
{
|
|
|
64 |
// Only check up to two (2) levels above this for tightness
|
|
|
65 |
$i = 2;
|
|
|
66 |
while (($node = $node->parent()) && $i--) {
|
|
|
67 |
if ($node instanceof TightBlockInterface) {
|
|
|
68 |
return $node->isTight();
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
return false;
|
|
|
73 |
}
|
|
|
74 |
}
|