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; |
16
|
|
|
|
17
|
|
|
use League\CommonMark\Block\Element\AbstractBlock; |
18
|
|
|
use League\CommonMark\Block\Element\Document; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Maintains the current state of the Markdown parser engine |
22
|
|
|
*/ |
23
|
|
|
class Context implements ContextInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var EnvironmentInterface |
27
|
|
|
*/ |
28
|
|
|
protected $environment; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Document |
32
|
|
|
*/ |
33
|
|
|
protected $doc; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var AbstractBlock|null |
37
|
|
|
*/ |
38
|
|
|
protected $tip; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var AbstractBlock |
42
|
|
|
*/ |
43
|
|
|
protected $container; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
protected $lineNumber; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $line; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var UnmatchedBlockCloser |
57
|
|
|
*/ |
58
|
|
|
protected $blockCloser; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var bool |
62
|
|
|
*/ |
63
|
|
|
protected $blocksParsed = false; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var ReferenceParser |
67
|
|
|
*/ |
68
|
|
|
protected $referenceParser; |
69
|
|
|
|
70
|
2076 |
|
public function __construct(Document $document, EnvironmentInterface $environment) |
71
|
|
|
{ |
72
|
2076 |
|
$this->doc = $document; |
73
|
2076 |
|
$this->tip = $this->doc; |
74
|
2076 |
|
$this->container = $this->doc; |
75
|
|
|
|
76
|
2076 |
|
$this->environment = $environment; |
77
|
|
|
|
78
|
2076 |
|
$this->referenceParser = new ReferenceParser($document->getReferenceMap()); |
79
|
|
|
|
80
|
2076 |
|
$this->blockCloser = new UnmatchedBlockCloser($this); |
81
|
2076 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $line |
85
|
|
|
*/ |
86
|
2067 |
|
public function setNextLine(string $line) |
87
|
|
|
{ |
88
|
2067 |
|
++$this->lineNumber; |
89
|
2067 |
|
$this->line = $line; |
90
|
2067 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return Document |
94
|
|
|
*/ |
95
|
2067 |
|
public function getDocument(): Document |
96
|
|
|
{ |
97
|
2067 |
|
return $this->doc; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return AbstractBlock|null |
102
|
|
|
*/ |
103
|
2076 |
|
public function getTip(): ?AbstractBlock |
104
|
|
|
{ |
105
|
2076 |
|
return $this->tip; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param AbstractBlock|null $block |
110
|
|
|
* |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
2076 |
|
public function setTip(?AbstractBlock $block) |
114
|
|
|
{ |
115
|
2076 |
|
$this->tip = $block; |
116
|
|
|
|
117
|
2076 |
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return int |
122
|
|
|
*/ |
123
|
2067 |
|
public function getLineNumber(): int |
124
|
|
|
{ |
125
|
2067 |
|
return $this->lineNumber; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
2067 |
|
public function getLine(): string |
132
|
|
|
{ |
133
|
2067 |
|
return $this->line; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Finalize and close any unmatched blocks |
138
|
|
|
* |
139
|
|
|
* @return UnmatchedBlockCloser |
140
|
|
|
*/ |
141
|
2067 |
|
public function getBlockCloser(): UnmatchedBlockCloser |
142
|
|
|
{ |
143
|
2067 |
|
return $this->blockCloser; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return AbstractBlock |
148
|
|
|
*/ |
149
|
2067 |
|
public function getContainer(): AbstractBlock |
150
|
|
|
{ |
151
|
2067 |
|
return $this->container; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param AbstractBlock $container |
156
|
|
|
* |
157
|
|
|
* @return $this |
158
|
|
|
*/ |
159
|
2067 |
|
public function setContainer(AbstractBlock $container) |
160
|
|
|
{ |
161
|
2067 |
|
$this->container = $container; |
162
|
|
|
|
163
|
2067 |
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param AbstractBlock $block |
168
|
|
|
*/ |
169
|
2067 |
|
public function addBlock(AbstractBlock $block) |
170
|
|
|
{ |
171
|
2067 |
|
$this->getBlockCloser()->closeUnmatchedBlocks(); |
172
|
2067 |
|
$block->setStartLine($this->lineNumber); |
173
|
|
|
|
174
|
2067 |
|
while ($this->tip !== null && !$this->tip->canContain($block)) { |
175
|
114 |
|
$this->tip->finalize($this, $this->lineNumber); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
// This should always be true |
179
|
2067 |
|
if ($this->tip !== null) { |
180
|
2067 |
|
$this->tip->appendChild($block); |
181
|
|
|
} |
182
|
|
|
|
183
|
2067 |
|
$this->tip = $block; |
184
|
2067 |
|
$this->container = $block; |
185
|
2067 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param AbstractBlock $replacement |
189
|
|
|
*/ |
190
|
57 |
|
public function replaceContainerBlock(AbstractBlock $replacement) |
191
|
|
|
{ |
192
|
57 |
|
$this->getBlockCloser()->closeUnmatchedBlocks(); |
193
|
57 |
|
$this->getContainer()->replaceWith($replacement); |
194
|
|
|
|
195
|
57 |
|
if ($this->getTip() === $this->getContainer()) { |
196
|
57 |
|
$this->setTip($replacement); |
197
|
|
|
} |
198
|
|
|
|
199
|
57 |
|
$this->setContainer($replacement); |
200
|
57 |
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return bool |
204
|
|
|
*/ |
205
|
2067 |
|
public function getBlocksParsed(): bool |
206
|
|
|
{ |
207
|
2067 |
|
return $this->blocksParsed; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param bool $bool |
212
|
|
|
* |
213
|
|
|
* @return $this |
214
|
|
|
*/ |
215
|
2067 |
|
public function setBlocksParsed(bool $bool) |
216
|
|
|
{ |
217
|
2067 |
|
$this->blocksParsed = $bool; |
218
|
|
|
|
219
|
2067 |
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return ReferenceParser |
224
|
|
|
*/ |
225
|
480 |
|
public function getReferenceParser(): ReferenceParser |
226
|
|
|
{ |
227
|
480 |
|
return $this->referenceParser; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|