| 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\Node\Block\Paragraph;
|
|
|
18 |
use League\CommonMark\Parser\Block\BlockStart;
|
|
|
19 |
use League\CommonMark\Parser\Block\BlockStartParserInterface;
|
|
|
20 |
use League\CommonMark\Parser\Cursor;
|
|
|
21 |
use League\CommonMark\Parser\MarkdownParserStateInterface;
|
|
|
22 |
use League\CommonMark\Util\RegexHelper;
|
|
|
23 |
|
|
|
24 |
final class HtmlBlockStartParser implements BlockStartParserInterface
|
|
|
25 |
{
|
|
|
26 |
public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
|
|
|
27 |
{
|
|
|
28 |
if ($cursor->isIndented() || $cursor->getNextNonSpaceCharacter() !== '<') {
|
|
|
29 |
return BlockStart::none();
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
$tmpCursor = clone $cursor;
|
|
|
33 |
$tmpCursor->advanceToNextNonSpaceOrTab();
|
|
|
34 |
$line = $tmpCursor->getRemainder();
|
|
|
35 |
|
|
|
36 |
for ($blockType = 1; $blockType <= 7; $blockType++) {
|
|
|
37 |
/** @psalm-var HtmlBlock::TYPE_* $blockType */
|
|
|
38 |
/** @phpstan-var HtmlBlock::TYPE_* $blockType */
|
|
|
39 |
$match = RegexHelper::matchAt(
|
|
|
40 |
RegexHelper::getHtmlBlockOpenRegex($blockType),
|
|
|
41 |
$line
|
|
|
42 |
);
|
|
|
43 |
|
|
|
44 |
if ($match !== null && ($blockType < 7 || $this->isType7BlockAllowed($cursor, $parserState))) {
|
|
|
45 |
return BlockStart::of(new HtmlBlockParser($blockType))->at($cursor);
|
|
|
46 |
}
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
return BlockStart::none();
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
private function isType7BlockAllowed(Cursor $cursor, MarkdownParserStateInterface $parserState): bool
|
|
|
53 |
{
|
|
|
54 |
// Type 7 blocks can't interrupt paragraphs
|
|
|
55 |
if ($parserState->getLastMatchedBlockParser()->getBlock() instanceof Paragraph) {
|
|
|
56 |
return false;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
// Even lazy ones
|
|
|
60 |
return ! $parserState->getActiveBlockParser()->canHaveLazyContinuationLines();
|
|
|
61 |
}
|
|
|
62 |
}
|