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\Parser; |
16
|
|
|
|
17
|
|
|
use League\CommonMark\Environment\EnvironmentInterface; |
18
|
|
|
use League\CommonMark\Node\Block\AbstractBlock; |
19
|
|
|
use League\CommonMark\Node\Block\Document; |
20
|
|
|
use League\CommonMark\Reference\ReferenceParser; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Maintains the current state of the Markdown parser engine |
24
|
|
|
*/ |
25
|
|
|
class Context implements ContextInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var EnvironmentInterface |
29
|
|
|
*/ |
30
|
|
|
protected $environment; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Document |
34
|
|
|
*/ |
35
|
|
|
protected $doc; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var AbstractBlock|null |
39
|
|
|
*/ |
40
|
|
|
protected $tip; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var AbstractBlock |
44
|
|
|
*/ |
45
|
|
|
protected $container; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
protected $lineNumber; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $line; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var UnmatchedBlockCloser |
59
|
|
|
*/ |
60
|
|
|
protected $blockCloser; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var bool |
64
|
|
|
*/ |
65
|
|
|
protected $blocksParsed = false; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var ReferenceParser |
69
|
|
|
*/ |
70
|
|
|
protected $referenceParser; |
71
|
|
|
|
72
|
2526 |
|
public function __construct(Document $document, EnvironmentInterface $environment) |
73
|
|
|
{ |
74
|
2526 |
|
$this->doc = $document; |
75
|
2526 |
|
$this->tip = $this->doc; |
76
|
2526 |
|
$this->container = $this->doc; |
77
|
|
|
|
78
|
2526 |
|
$this->environment = $environment; |
79
|
|
|
|
80
|
2526 |
|
$this->referenceParser = new ReferenceParser($document->getReferenceMap()); |
81
|
|
|
|
82
|
2526 |
|
$this->blockCloser = new UnmatchedBlockCloser($this); |
83
|
2526 |
|
} |
84
|
|
|
|
85
|
2517 |
|
public function setNextLine(string $line): void |
86
|
|
|
{ |
87
|
2517 |
|
++$this->lineNumber; |
88
|
2517 |
|
$this->line = $line; |
89
|
2517 |
|
} |
90
|
|
|
|
91
|
2493 |
|
public function getDocument(): Document |
92
|
|
|
{ |
93
|
2493 |
|
return $this->doc; |
94
|
|
|
} |
95
|
|
|
|
96
|
2526 |
|
public function getTip(): ?AbstractBlock |
97
|
|
|
{ |
98
|
2526 |
|
return $this->tip; |
99
|
|
|
} |
100
|
|
|
|
101
|
2520 |
|
public function setTip(?AbstractBlock $block): void |
102
|
|
|
{ |
103
|
2520 |
|
$this->tip = $block; |
104
|
2520 |
|
} |
105
|
|
|
|
106
|
2511 |
|
public function getLineNumber(): int |
107
|
|
|
{ |
108
|
2511 |
|
return $this->lineNumber; |
109
|
|
|
} |
110
|
|
|
|
111
|
2493 |
|
public function getLine(): string |
112
|
|
|
{ |
113
|
2493 |
|
return $this->line; |
114
|
|
|
} |
115
|
|
|
|
116
|
2493 |
|
public function getBlockCloser(): UnmatchedBlockCloser |
117
|
|
|
{ |
118
|
2493 |
|
return $this->blockCloser; |
119
|
|
|
} |
120
|
|
|
|
121
|
2511 |
|
public function getContainer(): AbstractBlock |
122
|
|
|
{ |
123
|
2511 |
|
return $this->container; |
124
|
|
|
} |
125
|
|
|
|
126
|
2493 |
|
public function setContainer(AbstractBlock $container): void |
127
|
|
|
{ |
128
|
2493 |
|
$this->container = $container; |
129
|
2493 |
|
} |
130
|
|
|
|
131
|
2511 |
|
public function addBlock(AbstractBlock $block): void |
132
|
|
|
{ |
133
|
2511 |
|
$this->blockCloser->closeUnmatchedBlocks(); |
134
|
2511 |
|
$block->setStartLine($this->lineNumber); |
135
|
|
|
|
136
|
2511 |
|
while ($this->tip !== null && !$this->tip->canContain($block)) { |
137
|
123 |
|
$this->tip->finalize($this, $this->lineNumber); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// This should always be true |
141
|
2511 |
|
if ($this->tip !== null) { |
142
|
2511 |
|
$this->tip->appendChild($block); |
143
|
|
|
} |
144
|
|
|
|
145
|
2511 |
|
$this->tip = $block; |
146
|
2511 |
|
$this->container = $block; |
147
|
2511 |
|
} |
148
|
|
|
|
149
|
132 |
|
public function replaceContainerBlock(AbstractBlock $replacement): void |
150
|
|
|
{ |
151
|
132 |
|
$this->blockCloser->closeUnmatchedBlocks(); |
152
|
132 |
|
$this->container->replaceWith($replacement); |
153
|
|
|
|
154
|
132 |
|
if ($this->tip === $this->container) { |
155
|
132 |
|
$this->tip = $replacement; |
156
|
|
|
} |
157
|
|
|
|
158
|
132 |
|
$this->container = $replacement; |
159
|
132 |
|
} |
160
|
|
|
|
161
|
2493 |
|
public function getBlocksParsed(): bool |
162
|
|
|
{ |
163
|
2493 |
|
return $this->blocksParsed; |
164
|
|
|
} |
165
|
|
|
|
166
|
2493 |
|
public function setBlocksParsed(bool $bool): void |
167
|
|
|
{ |
168
|
2493 |
|
$this->blocksParsed = $bool; |
169
|
2493 |
|
} |
170
|
|
|
|
171
|
495 |
|
public function getReferenceParser(): ReferenceParser |
172
|
|
|
{ |
173
|
495 |
|
return $this->referenceParser; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|