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 <[email protected]> |
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\CommonMark\Parser\Block; |
15
|
|
|
|
16
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Block\HtmlBlock; |
17
|
|
|
use League\CommonMark\Node\Block\AbstractBlock; |
18
|
|
|
use League\CommonMark\Parser\Block\AbstractBlockContinueParser; |
19
|
|
|
use League\CommonMark\Parser\Block\BlockContinue; |
20
|
|
|
use League\CommonMark\Parser\Block\BlockContinueParserInterface; |
21
|
|
|
use League\CommonMark\Parser\Cursor; |
22
|
|
|
use League\CommonMark\Util\RegexHelper; |
23
|
|
|
|
24
|
|
|
final class HtmlBlockParser extends AbstractBlockContinueParser |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var HtmlBlock |
28
|
|
|
* |
29
|
|
|
* @psalm-readonly |
30
|
|
|
*/ |
31
|
|
|
private $block; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
private $content = ''; |
35
|
|
|
|
36
|
|
|
/** @var bool */ |
37
|
|
|
private $finished = false; |
38
|
|
|
|
39
|
171 |
|
public function __construct(int $blockType) |
40
|
|
|
{ |
41
|
171 |
|
$this->block = new HtmlBlock($blockType); |
42
|
171 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return HtmlBlock |
46
|
|
|
*/ |
47
|
171 |
|
public function getBlock(): AbstractBlock |
48
|
|
|
{ |
49
|
171 |
|
return $this->block; |
50
|
|
|
} |
51
|
|
|
|
52
|
156 |
|
public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue |
53
|
|
|
{ |
54
|
156 |
|
if ($this->finished) { |
55
|
48 |
|
return BlockContinue::none(); |
56
|
|
|
} |
57
|
|
|
|
58
|
141 |
|
if ($cursor->isBlank() && \in_array($this->block->getType(), [HtmlBlock::TYPE_6_BLOCK_ELEMENT, HtmlBlock::TYPE_7_MISC_ELEMENT], true)) { |
59
|
48 |
|
return BlockContinue::none(); |
60
|
|
|
} |
61
|
|
|
|
62
|
120 |
|
return BlockContinue::at($cursor); |
63
|
|
|
} |
64
|
|
|
|
65
|
171 |
|
public function addLine(string $line): void |
66
|
|
|
{ |
67
|
171 |
|
if ($this->content !== '') { |
68
|
120 |
|
$this->content .= "\n"; |
69
|
|
|
} |
70
|
|
|
|
71
|
171 |
|
$this->content .= $line; |
72
|
|
|
|
73
|
|
|
// Check for end condition |
74
|
|
|
// phpcs:disable SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed |
75
|
171 |
|
if ($this->block->getType() >= HtmlBlock::TYPE_1_CODE_CONTAINER && $this->block->getType() <= HtmlBlock::TYPE_5_CDATA) { |
76
|
60 |
|
if (\preg_match(RegexHelper::getHtmlBlockCloseRegex($this->block->getType()), $line) === 1) { |
77
|
57 |
|
$this->finished = true; |
78
|
|
|
} |
79
|
|
|
} |
80
|
171 |
|
} |
81
|
|
|
|
82
|
171 |
|
public function closeBlock(): void |
83
|
|
|
{ |
84
|
171 |
|
$this->block->setLiteral($this->content); |
85
|
171 |
|
$this->content = ''; |
86
|
171 |
|
} |
87
|
|
|
} |
88
|
|
|
|