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 bool |
36
|
|
|
*/ |
37
|
|
|
protected $open = true; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool |
41
|
|
|
*/ |
42
|
|
|
protected $lastLineBlank = false; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var int |
46
|
|
|
*/ |
47
|
|
|
protected $startLine; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var int |
51
|
|
|
*/ |
52
|
|
|
protected $endLine; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Node|null $node |
56
|
|
|
*/ |
57
|
2037 |
|
protected function setParent(Node $node = null) |
58
|
|
|
{ |
59
|
2037 |
|
if ($node && !$node instanceof self) { |
60
|
|
|
throw new \InvalidArgumentException('Parent of block must also be block (can not be inline)'); |
61
|
|
|
} |
62
|
|
|
|
63
|
2037 |
|
parent::setParent($node); |
64
|
2037 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
2031 |
|
public function isContainer(): bool |
70
|
|
|
{ |
71
|
2031 |
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
30 |
|
public function hasChildren(): bool |
78
|
|
|
{ |
79
|
30 |
|
return $this->firstChild !== null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns true if this block can contain the given block as a child node |
84
|
|
|
* |
85
|
|
|
* @param AbstractBlock $block |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
abstract public function canContain(AbstractBlock $block): bool; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Whether this is a code block |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
abstract public function isCode(): bool; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Cursor $cursor |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
abstract public function matchesNextLine(Cursor $cursor): bool; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param int $startLine |
107
|
|
|
* |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
2043 |
|
public function setStartLine(int $startLine) |
111
|
|
|
{ |
112
|
2043 |
|
$this->startLine = $startLine; |
113
|
2043 |
|
if (empty($this->endLine)) { |
114
|
2043 |
|
$this->endLine = $startLine; |
115
|
|
|
} |
116
|
|
|
|
117
|
2043 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return int |
122
|
|
|
*/ |
123
|
|
|
public function getStartLine(): int |
124
|
|
|
{ |
125
|
|
|
return $this->startLine; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param int $endLine |
130
|
|
|
* |
131
|
|
|
* @return $this |
132
|
|
|
*/ |
133
|
|
|
public function setEndLine(int $endLine) |
134
|
|
|
{ |
135
|
|
|
$this->endLine = $endLine; |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return int |
142
|
|
|
*/ |
143
|
|
|
public function getEndLine(): int |
144
|
|
|
{ |
145
|
|
|
return $this->endLine; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Whether the block ends with a blank line |
150
|
|
|
* |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
246 |
|
public function endsWithBlankLine(): bool |
154
|
|
|
{ |
155
|
246 |
|
return $this->lastLineBlank; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param bool $blank |
160
|
|
|
*/ |
161
|
2028 |
|
public function setLastLineBlank(bool $blank) |
162
|
|
|
{ |
163
|
2028 |
|
$this->lastLineBlank = $blank; |
164
|
2028 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Determines whether the last line should be marked as blank |
168
|
|
|
* |
169
|
|
|
* @param Cursor $cursor |
170
|
|
|
* @param int $currentLineNumber |
171
|
|
|
* |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
1779 |
|
public function shouldLastLineBeBlank(Cursor $cursor, int $currentLineNumber): bool |
175
|
|
|
{ |
176
|
1779 |
|
return $cursor->isBlank(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Whether the block is open for modifications |
181
|
|
|
* |
182
|
|
|
* @return bool |
183
|
|
|
*/ |
184
|
1125 |
|
public function isOpen(): bool |
185
|
|
|
{ |
186
|
1125 |
|
return $this->open; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Finalize the block; mark it closed for modification |
191
|
|
|
* |
192
|
|
|
* @param ContextInterface $context |
193
|
|
|
* @param int $endLineNumber |
194
|
|
|
*/ |
195
|
2037 |
|
public function finalize(ContextInterface $context, int $endLineNumber) |
196
|
|
|
{ |
197
|
2037 |
|
if (!$this->open) { |
198
|
|
|
return; |
199
|
|
|
} |
200
|
|
|
|
201
|
2037 |
|
$this->open = false; |
202
|
2037 |
|
$this->endLine = $endLineNumber; |
203
|
|
|
|
204
|
2037 |
|
$context->setTip($context->getTip()->parent()); |
205
|
2037 |
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param string $key |
209
|
|
|
* @param mixed $default |
210
|
|
|
* |
211
|
|
|
* @return mixed |
212
|
|
|
*/ |
213
|
1992 |
|
public function getData(string $key, $default = null) |
214
|
|
|
{ |
215
|
1992 |
|
return \array_key_exists($key, $this->data) ? $this->data[$key] : $default; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|