|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the league/commonmark package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js) |
|
9
|
|
|
* - (c) John MacFarlane |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace League\CommonMark\Extension\CommonMark\Node\Block; |
|
16
|
|
|
|
|
17
|
|
|
use League\CommonMark\Node\Block\AbstractBlock; |
|
18
|
|
|
use League\CommonMark\Node\Block\AbstractStringContainerBlock; |
|
19
|
|
|
use League\CommonMark\Parser\ContextInterface; |
|
20
|
|
|
use League\CommonMark\Parser\Cursor; |
|
21
|
|
|
|
|
22
|
|
|
class IndentedCode extends AbstractStringContainerBlock |
|
23
|
|
|
{ |
|
24
|
3 |
|
public function canContain(AbstractBlock $block): bool |
|
25
|
|
|
{ |
|
26
|
3 |
|
return false; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
3 |
|
public function isCode(): bool |
|
30
|
|
|
{ |
|
31
|
3 |
|
return true; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function matchesNextLine(Cursor $cursor): bool |
|
35
|
|
|
{ |
|
36
|
|
|
if ($cursor->isIndented()) { |
|
37
|
|
|
$cursor->advanceBy(Cursor::INDENT_LEVEL, true); |
|
38
|
|
|
} elseif ($cursor->isBlank()) { |
|
39
|
|
|
$cursor->advanceToNextNonSpaceOrTab(); |
|
40
|
|
|
} else { |
|
41
|
|
|
return false; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return true; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
3 |
|
public function finalize(ContextInterface $context, int $endLineNumber): void |
|
48
|
|
|
{ |
|
49
|
3 |
|
parent::finalize($context, $endLineNumber); |
|
50
|
|
|
|
|
51
|
3 |
|
$reversed = \array_reverse($this->strings->toArray(), true); |
|
52
|
3 |
|
foreach ($reversed as $index => $line) { |
|
53
|
3 |
|
if ($line === '' || $line === "\n" || \preg_match('/^(\n *)$/', $line)) { |
|
54
|
|
|
unset($reversed[$index]); |
|
55
|
|
|
} else { |
|
56
|
3 |
|
break; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
3 |
|
$fixed = \array_reverse($reversed); |
|
60
|
3 |
|
$tmp = \implode("\n", $fixed); |
|
61
|
3 |
|
if (\substr($tmp, -1) !== "\n") { |
|
62
|
3 |
|
$tmp .= "\n"; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
3 |
|
$this->finalStringContents = $tmp; |
|
66
|
3 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function handleRemainingContents(ContextInterface $context, Cursor $cursor): void |
|
69
|
|
|
{ |
|
70
|
|
|
/** @var self $tip */ |
|
71
|
|
|
$tip = $context->getTip(); |
|
72
|
|
|
$tip->addLine($cursor->getRemainder()); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|