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