|
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
|
|
|
use League\CommonMark\Util\ArrayCollection; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Block-level element |
|
24
|
|
|
*/ |
|
25
|
|
|
abstract class AbstractBlock extends Node |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Used for storage of arbitrary data. |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
public $data = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var ArrayCollection|string[] |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $strings; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $finalStringContents = ''; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var bool |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $open = true; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var bool |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $lastLineBlank = false; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var int |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $startLine; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var int |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $endLine; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Constructor |
|
66
|
|
|
*/ |
|
67
|
1962 |
|
public function __construct() |
|
68
|
|
|
{ |
|
69
|
1962 |
|
$this->strings = new ArrayCollection(); |
|
70
|
1962 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param Node|null $node |
|
74
|
|
|
*/ |
|
75
|
1854 |
|
protected function setParent(Node $node = null) |
|
76
|
|
|
{ |
|
77
|
1854 |
|
if ($node && !$node instanceof self) { |
|
78
|
|
|
throw new \InvalidArgumentException('Parent of block must also be block (can not be inline)'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1854 |
|
parent::setParent($node); |
|
82
|
1854 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
1854 |
|
public function isContainer() |
|
88
|
|
|
{ |
|
89
|
1854 |
|
return true; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
1854 |
|
public function hasChildren() |
|
96
|
|
|
{ |
|
97
|
1854 |
|
return !is_null($this->firstChild); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Returns true if this block can contain the given block as a child node |
|
102
|
|
|
* |
|
103
|
|
|
* @param AbstractBlock $block |
|
104
|
|
|
* |
|
105
|
|
|
* @return bool |
|
106
|
|
|
*/ |
|
107
|
|
|
abstract public function canContain(AbstractBlock $block); |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns true if block type can accept lines of text |
|
111
|
|
|
* |
|
112
|
|
|
* @return bool |
|
113
|
|
|
*/ |
|
114
|
|
|
abstract public function acceptsLines(); |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Whether this is a code block |
|
118
|
|
|
* |
|
119
|
|
|
* @return bool |
|
120
|
|
|
*/ |
|
121
|
|
|
abstract public function isCode(); |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param Cursor $cursor |
|
125
|
|
|
* |
|
126
|
|
|
* @return bool |
|
127
|
|
|
*/ |
|
128
|
|
|
abstract public function matchesNextLine(Cursor $cursor); |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param ContextInterface $context |
|
132
|
|
|
* @param Cursor $cursor |
|
133
|
|
|
*/ |
|
134
|
|
|
public function handleRemainingContents(ContextInterface $context, Cursor $cursor) |
|
135
|
|
|
{ |
|
136
|
|
|
// create paragraph container for line |
|
137
|
|
|
$context->addBlock(new Paragraph()); |
|
138
|
|
|
$cursor->advanceToFirstNonSpace(); |
|
139
|
|
|
$context->getTip()->addLine($cursor->getRemainder()); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param int $startLine |
|
144
|
|
|
* |
|
145
|
|
|
* @return $this |
|
146
|
|
|
*/ |
|
147
|
1860 |
|
public function setStartLine($startLine) |
|
148
|
|
|
{ |
|
149
|
1860 |
|
$this->startLine = $startLine; |
|
150
|
1860 |
|
if (empty($this->endLine)) { |
|
151
|
1860 |
|
$this->endLine = $startLine; |
|
152
|
1860 |
|
} |
|
153
|
|
|
|
|
154
|
1860 |
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return int |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getStartLine() |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->startLine; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function setEndLine($endLine) |
|
166
|
|
|
{ |
|
167
|
|
|
$this->endLine = $endLine; |
|
168
|
|
|
|
|
169
|
|
|
return $this; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @return int |
|
174
|
|
|
*/ |
|
175
|
|
|
public function getEndLine() |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->endLine; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Whether the block ends with a blank line |
|
182
|
|
|
* |
|
183
|
|
|
* @return bool |
|
184
|
|
|
*/ |
|
185
|
633 |
|
public function endsWithBlankLine() |
|
186
|
|
|
{ |
|
187
|
633 |
|
return $this->lastLineBlank; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @param bool $blank |
|
192
|
|
|
*/ |
|
193
|
1854 |
|
public function setLastLineBlank($blank) |
|
194
|
|
|
{ |
|
195
|
1854 |
|
$this->lastLineBlank = $blank; |
|
196
|
1854 |
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Determines whether the last line should be marked as blank |
|
200
|
|
|
* |
|
201
|
|
|
* @param Cursor $cursor |
|
202
|
|
|
* @param int $currentLineNumber |
|
203
|
|
|
* |
|
204
|
|
|
* @return bool |
|
205
|
|
|
*/ |
|
206
|
1629 |
|
public function shouldLastLineBeBlank(Cursor $cursor, $currentLineNumber) |
|
207
|
|
|
{ |
|
208
|
1629 |
|
return $cursor->isBlank(); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @return string[] |
|
213
|
|
|
*/ |
|
214
|
1749 |
|
public function getStrings() |
|
215
|
|
|
{ |
|
216
|
1749 |
|
return $this->strings->toArray(); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @param string $line |
|
221
|
|
|
*/ |
|
222
|
1842 |
|
public function addLine($line) |
|
223
|
|
|
{ |
|
224
|
1842 |
|
if (!$this->acceptsLines()) { |
|
225
|
|
|
throw new \LogicException('You cannot add lines to a block which cannot accept them'); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
1842 |
|
$this->strings->add($line); |
|
229
|
1842 |
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Whether the block is open for modifications |
|
233
|
|
|
* |
|
234
|
|
|
* @return bool |
|
235
|
|
|
*/ |
|
236
|
1026 |
|
public function isOpen() |
|
237
|
|
|
{ |
|
238
|
1026 |
|
return $this->open; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Finalize the block; mark it closed for modification |
|
243
|
|
|
* |
|
244
|
|
|
* @param ContextInterface $context |
|
245
|
|
|
* @param int $endLineNumber |
|
246
|
|
|
*/ |
|
247
|
1854 |
|
public function finalize(ContextInterface $context, $endLineNumber) |
|
248
|
|
|
{ |
|
249
|
1854 |
|
if (!$this->open) { |
|
250
|
|
|
return; // TODO: Throw AlreadyClosedException? |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
1854 |
|
$this->open = false; |
|
254
|
1854 |
|
$this->endLine = $endLineNumber; |
|
255
|
|
|
|
|
256
|
1854 |
|
$context->setTip($context->getTip()->parent()); |
|
257
|
1854 |
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* @return string |
|
261
|
|
|
*/ |
|
262
|
1824 |
|
public function getStringContent() |
|
263
|
|
|
{ |
|
264
|
1824 |
|
return $this->finalStringContents; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* @param string $key |
|
269
|
|
|
* @param mixed $default |
|
270
|
|
|
* |
|
271
|
|
|
* @return mixed |
|
272
|
|
|
*/ |
|
273
|
1821 |
|
public function getData($key, $default = null) |
|
274
|
|
|
{ |
|
275
|
1821 |
|
return array_key_exists($key, $this->data) ? $this->data[$key] : $default; |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
|