|
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\IndentedCode; |
|
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\ArrayCollection; |
|
23
|
|
|
|
|
24
|
|
|
final class IndentedCodeParser extends AbstractBlockContinueParser |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var IndentedCode |
|
28
|
|
|
* |
|
29
|
|
|
* @psalm-readonly |
|
30
|
|
|
*/ |
|
31
|
|
|
private $block; |
|
32
|
|
|
|
|
33
|
|
|
/** @var ArrayCollection<string> */ |
|
34
|
|
|
protected $strings; |
|
35
|
|
|
|
|
36
|
156 |
|
public function __construct() |
|
37
|
|
|
{ |
|
38
|
156 |
|
$this->block = new IndentedCode(); |
|
39
|
156 |
|
$this->strings = new ArrayCollection(); |
|
40
|
156 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return IndentedCode |
|
44
|
|
|
*/ |
|
45
|
156 |
|
public function getBlock(): AbstractBlock |
|
46
|
|
|
{ |
|
47
|
156 |
|
return $this->block; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
96 |
|
public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue |
|
51
|
|
|
{ |
|
52
|
96 |
|
if ($cursor->isIndented()) { |
|
53
|
42 |
|
$cursor->advanceBy(Cursor::INDENT_LEVEL, true); |
|
54
|
|
|
|
|
55
|
42 |
|
return BlockContinue::at($cursor); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
75 |
|
if ($cursor->isBlank()) { |
|
59
|
63 |
|
$cursor->advanceToNextNonSpaceOrTab(); |
|
60
|
|
|
|
|
61
|
63 |
|
return BlockContinue::at($cursor); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
54 |
|
return BlockContinue::none(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
156 |
|
public function addLine(string $line): void |
|
68
|
|
|
{ |
|
69
|
156 |
|
$this->strings[] = $line; |
|
70
|
156 |
|
} |
|
71
|
|
|
|
|
72
|
156 |
|
public function closeBlock(): void |
|
73
|
|
|
{ |
|
74
|
156 |
|
$reversed = \array_reverse($this->strings->toArray(), true); |
|
75
|
156 |
|
foreach ($reversed as $index => $line) { |
|
76
|
156 |
|
if ($line !== '' && $line !== "\n" && ! \preg_match('/^(\n *)$/', $line)) { |
|
77
|
156 |
|
break; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
48 |
|
unset($reversed[$index]); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
156 |
|
$fixed = \array_reverse($reversed); |
|
84
|
156 |
|
$tmp = \implode("\n", $fixed); |
|
85
|
156 |
|
if (\substr($tmp, -1) !== "\n") { |
|
86
|
156 |
|
$tmp .= "\n"; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
156 |
|
$this->block->setLiteral($tmp); |
|
90
|
156 |
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|