|
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\Block\Element; |
|
16
|
|
|
|
|
17
|
|
|
use League\CommonMark\ContextInterface; |
|
18
|
|
|
use League\CommonMark\Cursor; |
|
19
|
|
|
use League\CommonMark\Node\Node; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Block-level element |
|
23
|
|
|
* |
|
24
|
|
|
* @method parent() ?AbstractBlock |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract class AbstractBlock extends Node |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Used for storage of arbitrary data. |
|
30
|
|
|
* |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
public $data = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var bool |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $open = true; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var bool |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $lastLineBlank = false; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var int |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $startLine; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var int |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $endLine; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param Node|null $node |
|
57
|
|
|
*/ |
|
58
|
2517 |
|
protected function setParent(Node $node = null) |
|
59
|
|
|
{ |
|
60
|
2517 |
|
if ($node && !$node instanceof self) { |
|
61
|
3 |
|
throw new \InvalidArgumentException('Parent of block must also be block (can not be inline)'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2514 |
|
parent::setParent($node); |
|
65
|
2514 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return bool |
|
69
|
|
|
*/ |
|
70
|
2466 |
|
public function isContainer(): bool |
|
71
|
|
|
{ |
|
72
|
2466 |
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
225 |
|
public function hasChildren(): bool |
|
79
|
|
|
{ |
|
80
|
225 |
|
return $this->firstChild !== null; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns true if this block can contain the given block as a child node |
|
85
|
|
|
* |
|
86
|
|
|
* @param AbstractBlock $block |
|
87
|
|
|
* |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
|
|
abstract public function canContain(AbstractBlock $block): bool; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Whether this is a code block |
|
94
|
|
|
* |
|
95
|
|
|
* Code blocks are extra-greedy - they'll try to consume all subsequent |
|
96
|
|
|
* lines of content without calling matchesNextLine() each time. |
|
97
|
|
|
* |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
|
|
abstract public function isCode(): bool; |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param Cursor $cursor |
|
104
|
|
|
* |
|
105
|
|
|
* @return bool |
|
106
|
|
|
*/ |
|
107
|
|
|
abstract public function matchesNextLine(Cursor $cursor): bool; |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param int $startLine |
|
111
|
|
|
* |
|
112
|
|
|
* @return $this |
|
113
|
|
|
*/ |
|
114
|
2535 |
|
public function setStartLine(int $startLine) |
|
115
|
|
|
{ |
|
116
|
2535 |
|
$this->startLine = $startLine; |
|
117
|
2535 |
|
if (empty($this->endLine)) { |
|
118
|
2535 |
|
$this->endLine = $startLine; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
2535 |
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return int |
|
126
|
|
|
*/ |
|
127
|
3 |
|
public function getStartLine(): int |
|
128
|
|
|
{ |
|
129
|
3 |
|
return $this->startLine; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param int $endLine |
|
134
|
|
|
* |
|
135
|
|
|
* @return $this |
|
136
|
|
|
*/ |
|
137
|
3 |
|
public function setEndLine(int $endLine) |
|
138
|
|
|
{ |
|
139
|
3 |
|
$this->endLine = $endLine; |
|
140
|
|
|
|
|
141
|
3 |
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return int |
|
146
|
|
|
*/ |
|
147
|
6 |
|
public function getEndLine(): int |
|
148
|
|
|
{ |
|
149
|
6 |
|
return $this->endLine; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Whether the block ends with a blank line |
|
154
|
|
|
* |
|
155
|
|
|
* @return bool |
|
156
|
|
|
*/ |
|
157
|
2457 |
|
public function endsWithBlankLine(): bool |
|
158
|
|
|
{ |
|
159
|
2457 |
|
return $this->lastLineBlank; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param bool $blank |
|
164
|
|
|
*/ |
|
165
|
744 |
|
public function setLastLineBlank(bool $blank) |
|
166
|
|
|
{ |
|
167
|
744 |
|
$this->lastLineBlank = $blank; |
|
168
|
744 |
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Determines whether the last line should be marked as blank |
|
172
|
|
|
* |
|
173
|
|
|
* @param Cursor $cursor |
|
174
|
|
|
* @param int $currentLineNumber |
|
175
|
|
|
* |
|
176
|
|
|
* @return bool |
|
177
|
|
|
*/ |
|
178
|
2193 |
|
public function shouldLastLineBeBlank(Cursor $cursor, int $currentLineNumber): bool |
|
179
|
|
|
{ |
|
180
|
2193 |
|
return $cursor->isBlank(); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Whether the block is open for modifications |
|
185
|
|
|
* |
|
186
|
|
|
* @return bool |
|
187
|
|
|
*/ |
|
188
|
1272 |
|
public function isOpen(): bool |
|
189
|
|
|
{ |
|
190
|
1272 |
|
return $this->open; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Finalize the block; mark it closed for modification |
|
195
|
|
|
* |
|
196
|
|
|
* @param ContextInterface $context |
|
197
|
|
|
* @param int $endLineNumber |
|
198
|
|
|
*/ |
|
199
|
2490 |
|
public function finalize(ContextInterface $context, int $endLineNumber) |
|
200
|
|
|
{ |
|
201
|
2490 |
|
if (!$this->open) { |
|
202
|
3 |
|
return; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
2490 |
|
$this->open = false; |
|
206
|
2490 |
|
$this->endLine = $endLineNumber; |
|
207
|
|
|
|
|
208
|
|
|
// This should almost always be true |
|
209
|
2490 |
|
if ($context->getTip() !== null) { |
|
210
|
2490 |
|
$context->setTip($context->getTip()->parent()); |
|
211
|
|
|
} |
|
212
|
2490 |
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @param string $key |
|
216
|
|
|
* @param mixed $default |
|
217
|
|
|
* |
|
218
|
|
|
* @return mixed |
|
219
|
|
|
*/ |
|
220
|
2421 |
|
public function getData(string $key, $default = null) |
|
221
|
|
|
{ |
|
222
|
2421 |
|
return \array_key_exists($key, $this->data) ? $this->data[$key] : $default; |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|