|
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\Util\RegexHelper; |
|
20
|
|
|
|
|
21
|
|
|
class FencedCode extends AbstractBlock |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $info; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var int |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $length; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $char; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var int |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $offset; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param int $length |
|
45
|
|
|
* @param string $char |
|
46
|
|
|
* @param int $offset |
|
47
|
|
|
*/ |
|
48
|
108 |
|
public function __construct($length, $char, $offset) |
|
49
|
|
|
{ |
|
50
|
108 |
|
parent::__construct(); |
|
51
|
|
|
|
|
52
|
108 |
|
$this->length = $length; |
|
53
|
108 |
|
$this->char = $char; |
|
54
|
108 |
|
$this->offset = $offset; |
|
55
|
108 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getInfo() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->info; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return string[] |
|
67
|
|
|
*/ |
|
68
|
108 |
|
public function getInfoWords() |
|
69
|
|
|
{ |
|
70
|
108 |
|
return preg_split('/\s+/', $this->info); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
102 |
|
public function getChar() |
|
77
|
|
|
{ |
|
78
|
102 |
|
return $this->char; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $char |
|
83
|
|
|
* |
|
84
|
|
|
* @return $this |
|
85
|
|
|
*/ |
|
86
|
|
|
public function setChar($char) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->char = $char; |
|
89
|
|
|
|
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return int |
|
95
|
|
|
*/ |
|
96
|
90 |
|
public function getLength() |
|
97
|
|
|
{ |
|
98
|
90 |
|
return $this->length; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param int $length |
|
103
|
|
|
* |
|
104
|
|
|
* @return $this |
|
105
|
|
|
*/ |
|
106
|
84 |
|
public function setLength($length) |
|
107
|
|
|
{ |
|
108
|
84 |
|
$this->length = $length; |
|
109
|
|
|
|
|
110
|
84 |
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return int |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getOffset() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->offset; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param int $offset |
|
123
|
|
|
* |
|
124
|
|
|
* @return $this |
|
125
|
|
|
*/ |
|
126
|
|
|
public function setOffset($offset) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->offset = $offset; |
|
129
|
|
|
|
|
130
|
|
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Returns true if this block can contain the given block as a child node |
|
135
|
|
|
* |
|
136
|
|
|
* @param AbstractBlock $block |
|
137
|
|
|
* |
|
138
|
|
|
* @return bool |
|
139
|
|
|
*/ |
|
140
|
|
|
public function canContain(AbstractBlock $block) |
|
141
|
|
|
{ |
|
142
|
|
|
return false; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Returns true if block type can accept lines of text |
|
147
|
|
|
* |
|
148
|
|
|
* @return bool |
|
149
|
|
|
*/ |
|
150
|
108 |
|
public function acceptsLines() |
|
151
|
|
|
{ |
|
152
|
108 |
|
return true; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Whether this is a code block |
|
157
|
|
|
* |
|
158
|
|
|
* @return bool |
|
159
|
|
|
*/ |
|
160
|
96 |
|
public function isCode() |
|
161
|
|
|
{ |
|
162
|
96 |
|
return true; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
96 |
|
public function matchesNextLine(Cursor $cursor) |
|
166
|
|
|
{ |
|
167
|
96 |
|
if ($this->length === -1) { |
|
168
|
15 |
|
if ($cursor->isBlank()) { |
|
169
|
9 |
|
$this->lastLineBlank = true; |
|
170
|
3 |
|
} |
|
171
|
|
|
|
|
172
|
15 |
|
return false; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
// Skip optional spaces of fence offset |
|
176
|
96 |
|
$cursor->match('/^ {0,' . $this->offset . '}/'); |
|
177
|
|
|
|
|
178
|
96 |
|
return true; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
108 |
|
public function finalize(ContextInterface $context, $endLineNumber) |
|
182
|
|
|
{ |
|
183
|
108 |
|
parent::finalize($context, $endLineNumber); |
|
184
|
|
|
|
|
185
|
|
|
// first line becomes info string |
|
186
|
108 |
|
$this->info = RegexHelper::unescape(trim($this->strings->first())); |
|
187
|
|
|
|
|
188
|
108 |
|
if ($this->strings->count() === 1) { |
|
189
|
12 |
|
$this->finalStringContents = ''; |
|
190
|
4 |
|
} else { |
|
191
|
96 |
|
$this->finalStringContents = implode("\n", $this->strings->slice(1)) . "\n"; |
|
192
|
|
|
} |
|
193
|
108 |
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @param ContextInterface $context |
|
197
|
|
|
* @param Cursor $cursor |
|
198
|
|
|
*/ |
|
199
|
102 |
|
public function handleRemainingContents(ContextInterface $context, Cursor $cursor) |
|
200
|
|
|
{ |
|
201
|
|
|
/** @var FencedCode $container */ |
|
202
|
102 |
|
$container = $context->getContainer(); |
|
203
|
|
|
|
|
204
|
|
|
// check for closing code fence |
|
205
|
102 |
|
if ($cursor->getIndent() <= 3 && $cursor->getNextNonSpaceCharacter() === $container->getChar()) { |
|
206
|
90 |
|
$match = RegexHelper::matchAll('/^(?:`{3,}|~{3,})(?= *$)/', $cursor->getLine(), $cursor->getNextNonSpacePosition()); |
|
207
|
90 |
|
if (strlen($match[0]) >= $container->getLength()) { |
|
208
|
|
|
// don't add closing fence to container; instead, close it: |
|
209
|
84 |
|
$this->setLength(-1); // -1 means we've passed closer |
|
210
|
|
|
|
|
211
|
84 |
|
return; |
|
212
|
|
|
} |
|
213
|
5 |
|
} |
|
214
|
|
|
|
|
215
|
102 |
|
$context->getTip()->addLine($cursor->getRemainder()); |
|
216
|
102 |
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @param Cursor $cursor |
|
220
|
|
|
* @param int $currentLineNumber |
|
221
|
|
|
* |
|
222
|
|
|
* @return bool |
|
223
|
|
|
*/ |
|
224
|
102 |
|
public function shouldLastLineBeBlank(Cursor $cursor, $currentLineNumber) |
|
225
|
|
|
{ |
|
226
|
102 |
|
return false; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|