1
|
|
|
<?php |
2
|
|
|
namespace Parsedown; |
3
|
|
|
# |
4
|
|
|
# |
5
|
|
|
# Parsedown |
6
|
|
|
# http://parsedown.org |
7
|
|
|
# |
8
|
|
|
# (c) Emanuil Rusev |
9
|
|
|
# http://erusev.com |
10
|
|
|
# |
11
|
|
|
# For the full license information, view the LICENSE file that was distributed |
12
|
|
|
# with this source code. |
13
|
|
|
# |
14
|
|
|
# |
15
|
|
|
|
16
|
|
|
class Parsedown |
17
|
|
|
{ |
18
|
|
|
# ~ |
19
|
|
|
|
20
|
|
|
const version = '1.6.0'; |
21
|
|
|
|
22
|
|
|
# ~ |
23
|
|
|
|
24
|
|
|
function text($text) |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
# make sure no definitions are set |
27
|
|
|
$this->DefinitionData = array(); |
28
|
|
|
|
29
|
|
|
# standardize line breaks |
30
|
|
|
$text = str_replace(array("\r\n", "\r"), "\n", $text); |
31
|
|
|
|
32
|
|
|
# remove surrounding line breaks |
33
|
|
|
$text = trim($text, "\n"); |
34
|
|
|
|
35
|
|
|
# split text into lines |
36
|
|
|
$lines = explode("\n", $text); |
37
|
|
|
|
38
|
|
|
# iterate through lines to identify blocks |
39
|
|
|
$markup = $this->lines($lines); |
40
|
|
|
|
41
|
|
|
# trim line breaks |
42
|
|
|
$markup = trim($markup, "\n"); |
43
|
|
|
|
44
|
|
|
return $markup; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
# |
48
|
|
|
# Setters |
49
|
|
|
# |
50
|
|
|
|
51
|
|
|
function setBreaksEnabled($breaksEnabled) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$this->breaksEnabled = $breaksEnabled; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected $breaksEnabled; |
59
|
|
|
|
60
|
|
|
function setMarkupEscaped($markupEscaped) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$this->markupEscaped = $markupEscaped; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected $markupEscaped; |
68
|
|
|
|
69
|
|
|
function setUrlsLinked($urlsLinked) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$this->urlsLinked = $urlsLinked; |
72
|
|
|
|
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected $urlsLinked = true; |
77
|
|
|
|
78
|
|
|
# |
79
|
|
|
# Lines |
80
|
|
|
# |
81
|
|
|
|
82
|
|
|
protected $BlockTypes = array( |
83
|
|
|
'#' => array('Header'), |
84
|
|
|
'*' => array('Rule', 'List'), |
85
|
|
|
'+' => array('List'), |
86
|
|
|
'-' => array('SetextHeader', 'Table', 'Rule', 'List'), |
87
|
|
|
'0' => array('List'), |
88
|
|
|
'1' => array('List'), |
89
|
|
|
'2' => array('List'), |
90
|
|
|
'3' => array('List'), |
91
|
|
|
'4' => array('List'), |
92
|
|
|
'5' => array('List'), |
93
|
|
|
'6' => array('List'), |
94
|
|
|
'7' => array('List'), |
95
|
|
|
'8' => array('List'), |
96
|
|
|
'9' => array('List'), |
97
|
|
|
':' => array('Table'), |
98
|
|
|
'<' => array('Comment', 'Markup'), |
99
|
|
|
'=' => array('SetextHeader'), |
100
|
|
|
'>' => array('Quote'), |
101
|
|
|
'[' => array('Reference'), |
102
|
|
|
'_' => array('Rule'), |
103
|
|
|
'`' => array('FencedCode'), |
104
|
|
|
'|' => array('Table'), |
105
|
|
|
'~' => array('FencedCode'), |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
# ~ |
109
|
|
|
|
110
|
|
|
protected $unmarkedBlockTypes = array( |
111
|
|
|
'Code', |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
# |
115
|
|
|
# Blocks |
116
|
|
|
# |
117
|
|
|
|
118
|
|
|
protected function lines(array $lines) |
119
|
|
|
{ |
120
|
|
|
$CurrentBlock = null; |
121
|
|
|
|
122
|
|
|
foreach ($lines as $line) |
123
|
|
|
{ |
124
|
|
|
if (chop($line) === '') |
125
|
|
|
{ |
126
|
|
|
if (isset($CurrentBlock)) |
127
|
|
|
{ |
128
|
|
|
$CurrentBlock['interrupted'] = true; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
continue; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (strpos($line, "\t") !== false) |
135
|
|
|
{ |
136
|
|
|
$parts = explode("\t", $line); |
137
|
|
|
|
138
|
|
|
$line = $parts[0]; |
139
|
|
|
|
140
|
|
|
unset($parts[0]); |
141
|
|
|
|
142
|
|
|
foreach ($parts as $part) |
143
|
|
|
{ |
144
|
|
|
$shortage = 4 - mb_strlen($line, 'utf-8') % 4; |
145
|
|
|
|
146
|
|
|
$line .= str_repeat(' ', $shortage); |
147
|
|
|
$line .= $part; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$indent = 0; |
152
|
|
|
|
153
|
|
|
while (isset($line[$indent]) and $line[$indent] === ' ') |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
$indent ++; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$text = $indent > 0 ? substr($line, $indent) : $line; |
159
|
|
|
|
160
|
|
|
# ~ |
161
|
|
|
|
162
|
|
|
$Line = array('body' => $line, 'indent' => $indent, 'text' => $text); |
163
|
|
|
|
164
|
|
|
# ~ |
165
|
|
|
|
166
|
|
|
if (isset($CurrentBlock['continuable'])) |
167
|
|
|
{ |
168
|
|
|
$Block = $this->{'block'.$CurrentBlock['type'].'Continue'}($Line, $CurrentBlock); |
169
|
|
|
|
170
|
|
|
if (isset($Block)) |
171
|
|
|
{ |
172
|
|
|
$CurrentBlock = $Block; |
173
|
|
|
|
174
|
|
|
continue; |
175
|
|
|
} |
176
|
|
|
else |
177
|
|
|
{ |
178
|
|
|
if ($this->isBlockCompletable($CurrentBlock['type'])) |
179
|
|
|
{ |
180
|
|
|
$CurrentBlock = $this->{'block'.$CurrentBlock['type'].'Complete'}($CurrentBlock); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
# ~ |
186
|
|
|
|
187
|
|
|
$marker = $text[0]; |
188
|
|
|
|
189
|
|
|
# ~ |
190
|
|
|
|
191
|
|
|
$blockTypes = $this->unmarkedBlockTypes; |
192
|
|
|
|
193
|
|
|
if (isset($this->BlockTypes[$marker])) |
194
|
|
|
{ |
195
|
|
|
foreach ($this->BlockTypes[$marker] as $blockType) |
196
|
|
|
{ |
197
|
|
|
$blockTypes []= $blockType; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
# |
202
|
|
|
# ~ |
203
|
|
|
|
204
|
|
|
foreach ($blockTypes as $blockType) |
205
|
|
|
{ |
206
|
|
|
$Block = $this->{'block'.$blockType}($Line, $CurrentBlock); |
207
|
|
|
|
208
|
|
|
if (isset($Block)) |
209
|
|
|
{ |
210
|
|
|
$Block['type'] = $blockType; |
211
|
|
|
|
212
|
|
|
if ( ! isset($Block['identified'])) |
213
|
|
|
{ |
214
|
|
|
$Blocks []= $CurrentBlock; |
|
|
|
|
215
|
|
|
|
216
|
|
|
$Block['identified'] = true; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if ($this->isBlockContinuable($blockType)) |
220
|
|
|
{ |
221
|
|
|
$Block['continuable'] = true; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$CurrentBlock = $Block; |
225
|
|
|
|
226
|
|
|
continue 2; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
# ~ |
231
|
|
|
|
232
|
|
|
if (isset($CurrentBlock) and ! isset($CurrentBlock['type']) and ! isset($CurrentBlock['interrupted'])) |
|
|
|
|
233
|
|
|
{ |
234
|
|
|
$CurrentBlock['element']['text'] .= "\n".$text; |
235
|
|
|
} |
236
|
|
|
else |
237
|
|
|
{ |
238
|
|
|
$Blocks []= $CurrentBlock; |
|
|
|
|
239
|
|
|
|
240
|
|
|
$CurrentBlock = $this->paragraph($Line); |
241
|
|
|
|
242
|
|
|
$CurrentBlock['identified'] = true; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
# ~ |
247
|
|
|
|
248
|
|
|
if (isset($CurrentBlock['continuable']) and $this->isBlockCompletable($CurrentBlock['type'])) |
|
|
|
|
249
|
|
|
{ |
250
|
|
|
$CurrentBlock = $this->{'block'.$CurrentBlock['type'].'Complete'}($CurrentBlock); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
# ~ |
254
|
|
|
|
255
|
|
|
$Blocks []= $CurrentBlock; |
256
|
|
|
|
257
|
|
|
unset($Blocks[0]); |
258
|
|
|
|
259
|
|
|
# ~ |
260
|
|
|
|
261
|
|
|
$markup = ''; |
262
|
|
|
|
263
|
|
|
foreach ($Blocks as $Block) |
264
|
|
|
{ |
265
|
|
|
if (isset($Block['hidden'])) |
266
|
|
|
{ |
267
|
|
|
continue; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
$markup .= "\n"; |
271
|
|
|
$markup .= isset($Block['markup']) ? $Block['markup'] : $this->element($Block['element']); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
$markup .= "\n"; |
275
|
|
|
|
276
|
|
|
# ~ |
277
|
|
|
|
278
|
|
|
return $markup; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
protected function isBlockContinuable($Type) |
282
|
|
|
{ |
283
|
|
|
return method_exists($this, 'block'.$Type.'Continue'); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
protected function isBlockCompletable($Type) |
287
|
|
|
{ |
288
|
|
|
return method_exists($this, 'block'.$Type.'Complete'); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
# |
292
|
|
|
# Code |
293
|
|
|
|
294
|
|
|
protected function blockCode($Line, $Block = null) |
295
|
|
|
{ |
296
|
|
|
if (isset($Block) and ! isset($Block['type']) and ! isset($Block['interrupted'])) |
|
|
|
|
297
|
|
|
{ |
298
|
|
|
return; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
if ($Line['indent'] >= 4) |
302
|
|
|
{ |
303
|
|
|
$text = substr($Line['body'], 4); |
304
|
|
|
|
305
|
|
|
$Block = array( |
306
|
|
|
'element' => array( |
307
|
|
|
'name' => 'pre', |
308
|
|
|
'handler' => 'element', |
309
|
|
|
'text' => array( |
310
|
|
|
'name' => 'code', |
311
|
|
|
'text' => $text, |
312
|
|
|
), |
313
|
|
|
), |
314
|
|
|
); |
315
|
|
|
|
316
|
|
|
return $Block; |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
protected function blockCodeContinue($Line, $Block) |
321
|
|
|
{ |
322
|
|
|
if ($Line['indent'] >= 4) |
323
|
|
|
{ |
324
|
|
|
if (isset($Block['interrupted'])) |
325
|
|
|
{ |
326
|
|
|
$Block['element']['text']['text'] .= "\n"; |
327
|
|
|
|
328
|
|
|
unset($Block['interrupted']); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
$Block['element']['text']['text'] .= "\n"; |
332
|
|
|
|
333
|
|
|
$text = substr($Line['body'], 4); |
334
|
|
|
|
335
|
|
|
$Block['element']['text']['text'] .= $text; |
336
|
|
|
|
337
|
|
|
return $Block; |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
341
|
|
View Code Duplication |
protected function blockCodeComplete($Block) |
|
|
|
|
342
|
|
|
{ |
343
|
|
|
$text = $Block['element']['text']['text']; |
344
|
|
|
|
345
|
|
|
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8'); |
346
|
|
|
|
347
|
|
|
$Block['element']['text']['text'] = $text; |
348
|
|
|
|
349
|
|
|
return $Block; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
# |
353
|
|
|
# Comment |
354
|
|
|
|
355
|
|
|
protected function blockComment($Line) |
356
|
|
|
{ |
357
|
|
|
if ($this->markupEscaped) |
358
|
|
|
{ |
359
|
|
|
return; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
if (isset($Line['text'][3]) and $Line['text'][3] === '-' and $Line['text'][2] === '-' and $Line['text'][1] === '!') |
|
|
|
|
363
|
|
|
{ |
364
|
|
|
$Block = array( |
365
|
|
|
'markup' => $Line['body'], |
366
|
|
|
); |
367
|
|
|
|
368
|
|
|
if (preg_match('/-->$/', $Line['text'])) |
369
|
|
|
{ |
370
|
|
|
$Block['closed'] = true; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
return $Block; |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
protected function blockCommentContinue($Line, array $Block) |
378
|
|
|
{ |
379
|
|
|
if (isset($Block['closed'])) |
380
|
|
|
{ |
381
|
|
|
return; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
$Block['markup'] .= "\n" . $Line['body']; |
385
|
|
|
|
386
|
|
|
if (preg_match('/-->$/', $Line['text'])) |
387
|
|
|
{ |
388
|
|
|
$Block['closed'] = true; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
return $Block; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
# |
395
|
|
|
# Fenced Code |
396
|
|
|
|
397
|
|
|
protected function blockFencedCode($Line) |
398
|
|
|
{ |
399
|
|
|
if (preg_match('/^['.$Line['text'][0].']{3,}[ ]*([\w-]+)?[ ]*$/', $Line['text'], $matches)) |
400
|
|
|
{ |
401
|
|
|
$Element = array( |
402
|
|
|
'name' => 'code', |
403
|
|
|
'text' => '', |
404
|
|
|
); |
405
|
|
|
|
406
|
|
|
if (isset($matches[1])) |
407
|
|
|
{ |
408
|
|
|
$class = 'language-'.$matches[1]; |
409
|
|
|
|
410
|
|
|
$Element['attributes'] = array( |
411
|
|
|
'class' => $class, |
412
|
|
|
); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
$Block = array( |
416
|
|
|
'char' => $Line['text'][0], |
417
|
|
|
'element' => array( |
418
|
|
|
'name' => 'pre', |
419
|
|
|
'handler' => 'element', |
420
|
|
|
'text' => $Element, |
421
|
|
|
), |
422
|
|
|
); |
423
|
|
|
|
424
|
|
|
return $Block; |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
protected function blockFencedCodeContinue($Line, $Block) |
429
|
|
|
{ |
430
|
|
|
if (isset($Block['complete'])) |
431
|
|
|
{ |
432
|
|
|
return; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
if (isset($Block['interrupted'])) |
436
|
|
|
{ |
437
|
|
|
$Block['element']['text']['text'] .= "\n"; |
438
|
|
|
|
439
|
|
|
unset($Block['interrupted']); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
if (preg_match('/^'.$Block['char'].'{3,}[ ]*$/', $Line['text'])) |
443
|
|
|
{ |
444
|
|
|
$Block['element']['text']['text'] = substr($Block['element']['text']['text'], 1); |
445
|
|
|
|
446
|
|
|
$Block['complete'] = true; |
447
|
|
|
|
448
|
|
|
return $Block; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
$Block['element']['text']['text'] .= "\n".$Line['body'];; |
452
|
|
|
|
453
|
|
|
return $Block; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
View Code Duplication |
protected function blockFencedCodeComplete($Block) |
|
|
|
|
457
|
|
|
{ |
458
|
|
|
$text = $Block['element']['text']['text']; |
459
|
|
|
|
460
|
|
|
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8'); |
461
|
|
|
|
462
|
|
|
$Block['element']['text']['text'] = $text; |
463
|
|
|
|
464
|
|
|
return $Block; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
# |
468
|
|
|
# Header |
469
|
|
|
|
470
|
|
|
protected function blockHeader($Line) |
471
|
|
|
{ |
472
|
|
|
if (isset($Line['text'][1])) |
473
|
|
|
{ |
474
|
|
|
$level = 1; |
475
|
|
|
|
476
|
|
|
while (isset($Line['text'][$level]) and $Line['text'][$level] === '#') |
|
|
|
|
477
|
|
|
{ |
478
|
|
|
$level ++; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
if ($level > 6) |
482
|
|
|
{ |
483
|
|
|
return; |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
$text = trim($Line['text'], '# '); |
487
|
|
|
|
488
|
|
|
$Block = array( |
489
|
|
|
'element' => array( |
490
|
|
|
'name' => 'h' . min(6, $level), |
491
|
|
|
'text' => $text, |
492
|
|
|
'handler' => 'line', |
493
|
|
|
), |
494
|
|
|
); |
495
|
|
|
|
496
|
|
|
return $Block; |
497
|
|
|
} |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
# |
501
|
|
|
# List |
502
|
|
|
|
503
|
|
|
protected function blockList($Line) |
504
|
|
|
{ |
505
|
|
|
list($name, $pattern) = $Line['text'][0] <= '-' ? array('ul', '[*+-]') : array('ol', '[0-9]+[.]'); |
506
|
|
|
|
507
|
|
|
if (preg_match('/^('.$pattern.'[ ]+)(.*)/', $Line['text'], $matches)) |
508
|
|
|
{ |
509
|
|
|
$Block = array( |
510
|
|
|
'indent' => $Line['indent'], |
511
|
|
|
'pattern' => $pattern, |
512
|
|
|
'element' => array( |
513
|
|
|
'name' => $name, |
514
|
|
|
'handler' => 'elements', |
515
|
|
|
), |
516
|
|
|
); |
517
|
|
|
|
518
|
|
|
$Block['li'] = array( |
519
|
|
|
'name' => 'li', |
520
|
|
|
'handler' => 'li', |
521
|
|
|
'text' => array( |
522
|
|
|
$matches[2], |
523
|
|
|
), |
524
|
|
|
); |
525
|
|
|
|
526
|
|
|
$Block['element']['text'] []= & $Block['li']; |
527
|
|
|
|
528
|
|
|
return $Block; |
529
|
|
|
} |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
protected function blockListContinue($Line, array $Block) |
533
|
|
|
{ |
534
|
|
|
if ($Block['indent'] === $Line['indent'] and preg_match('/^'.$Block['pattern'].'(?:[ ]+(.*)|$)/', $Line['text'], $matches)) |
|
|
|
|
535
|
|
|
{ |
536
|
|
|
if (isset($Block['interrupted'])) |
537
|
|
|
{ |
538
|
|
|
$Block['li']['text'] []= ''; |
539
|
|
|
|
540
|
|
|
unset($Block['interrupted']); |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
unset($Block['li']); |
544
|
|
|
|
545
|
|
|
$text = isset($matches[1]) ? $matches[1] : ''; |
546
|
|
|
|
547
|
|
|
$Block['li'] = array( |
548
|
|
|
'name' => 'li', |
549
|
|
|
'handler' => 'li', |
550
|
|
|
'text' => array( |
551
|
|
|
$text, |
552
|
|
|
), |
553
|
|
|
); |
554
|
|
|
|
555
|
|
|
$Block['element']['text'] []= & $Block['li']; |
556
|
|
|
|
557
|
|
|
return $Block; |
558
|
|
|
} |
559
|
|
|
|
560
|
|
|
if ($Line['text'][0] === '[' and $this->blockReference($Line)) |
|
|
|
|
561
|
|
|
{ |
562
|
|
|
return $Block; |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
if ( ! isset($Block['interrupted'])) |
566
|
|
|
{ |
567
|
|
|
$text = preg_replace('/^[ ]{0,4}/', '', $Line['body']); |
568
|
|
|
|
569
|
|
|
$Block['li']['text'] []= $text; |
570
|
|
|
|
571
|
|
|
return $Block; |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
if ($Line['indent'] > 0) |
575
|
|
|
{ |
576
|
|
|
$Block['li']['text'] []= ''; |
577
|
|
|
|
578
|
|
|
$text = preg_replace('/^[ ]{0,4}/', '', $Line['body']); |
579
|
|
|
|
580
|
|
|
$Block['li']['text'] []= $text; |
581
|
|
|
|
582
|
|
|
unset($Block['interrupted']); |
583
|
|
|
|
584
|
|
|
return $Block; |
585
|
|
|
} |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
# |
589
|
|
|
# Quote |
590
|
|
|
|
591
|
|
|
protected function blockQuote($Line) |
592
|
|
|
{ |
593
|
|
|
if (preg_match('/^>[ ]?(.*)/', $Line['text'], $matches)) |
594
|
|
|
{ |
595
|
|
|
$Block = array( |
596
|
|
|
'element' => array( |
597
|
|
|
'name' => 'blockquote', |
598
|
|
|
'handler' => 'lines', |
599
|
|
|
'text' => (array) $matches[1], |
600
|
|
|
), |
601
|
|
|
); |
602
|
|
|
|
603
|
|
|
return $Block; |
604
|
|
|
} |
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
protected function blockQuoteContinue($Line, array $Block) |
608
|
|
|
{ |
609
|
|
|
if ($Line['text'][0] === '>' and preg_match('/^>[ ]?(.*)/', $Line['text'], $matches)) |
|
|
|
|
610
|
|
|
{ |
611
|
|
View Code Duplication |
if (isset($Block['interrupted'])) |
|
|
|
|
612
|
|
|
{ |
613
|
|
|
$Block['element']['text'] []= ''; |
614
|
|
|
|
615
|
|
|
unset($Block['interrupted']); |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
$Block['element']['text'] []= $matches[1]; |
619
|
|
|
|
620
|
|
|
return $Block; |
621
|
|
|
} |
622
|
|
|
|
623
|
|
View Code Duplication |
if ( ! isset($Block['interrupted'])) |
|
|
|
|
624
|
|
|
{ |
625
|
|
|
$Block['element']['text'] []= $Line['text']; |
626
|
|
|
|
627
|
|
|
return $Block; |
628
|
|
|
} |
629
|
|
|
} |
630
|
|
|
|
631
|
|
|
# |
632
|
|
|
# Rule |
633
|
|
|
|
634
|
|
|
protected function blockRule($Line) |
635
|
|
|
{ |
636
|
|
|
if (preg_match('/^(['.$Line['text'][0].'])([ ]*\1){2,}[ ]*$/', $Line['text'])) |
637
|
|
|
{ |
638
|
|
|
$Block = array( |
639
|
|
|
'element' => array( |
640
|
|
|
'name' => 'hr' |
641
|
|
|
), |
642
|
|
|
); |
643
|
|
|
|
644
|
|
|
return $Block; |
645
|
|
|
} |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
# |
649
|
|
|
# Setext |
650
|
|
|
|
651
|
|
|
protected function blockSetextHeader($Line, array $Block = null) |
652
|
|
|
{ |
653
|
|
|
if ( ! isset($Block) or isset($Block['type']) or isset($Block['interrupted'])) |
|
|
|
|
654
|
|
|
{ |
655
|
|
|
return; |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
if (chop($Line['text'], $Line['text'][0]) === '') |
659
|
|
|
{ |
660
|
|
|
$Block['element']['name'] = $Line['text'][0] === '=' ? 'h1' : 'h2'; |
661
|
|
|
|
662
|
|
|
return $Block; |
663
|
|
|
} |
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
# |
667
|
|
|
# Markup |
668
|
|
|
|
669
|
|
|
protected function blockMarkup($Line) |
670
|
|
|
{ |
671
|
|
|
if ($this->markupEscaped) |
672
|
|
|
{ |
673
|
|
|
return; |
674
|
|
|
} |
675
|
|
|
|
676
|
|
|
if (preg_match('/^<(\w*)(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*(\/)?>/', $Line['text'], $matches)) |
677
|
|
|
{ |
678
|
|
|
$element = strtolower($matches[1]); |
679
|
|
|
|
680
|
|
|
if (in_array($element, $this->textLevelElements)) |
681
|
|
|
{ |
682
|
|
|
return; |
683
|
|
|
} |
684
|
|
|
|
685
|
|
|
$Block = array( |
686
|
|
|
'name' => $matches[1], |
687
|
|
|
'depth' => 0, |
688
|
|
|
'markup' => $Line['text'], |
689
|
|
|
); |
690
|
|
|
|
691
|
|
|
$length = strlen($matches[0]); |
692
|
|
|
|
693
|
|
|
$remainder = substr($Line['text'], $length); |
694
|
|
|
|
695
|
|
|
if (trim($remainder) === '') |
696
|
|
|
{ |
697
|
|
|
if (isset($matches[2]) or in_array($matches[1], $this->voidElements)) |
|
|
|
|
698
|
|
|
{ |
699
|
|
|
$Block['closed'] = true; |
700
|
|
|
|
701
|
|
|
$Block['void'] = true; |
702
|
|
|
} |
703
|
|
|
} |
704
|
|
|
else |
705
|
|
|
{ |
706
|
|
|
if (isset($matches[2]) or in_array($matches[1], $this->voidElements)) |
|
|
|
|
707
|
|
|
{ |
708
|
|
|
return; |
709
|
|
|
} |
710
|
|
|
|
711
|
|
|
if (preg_match('/<\/'.$matches[1].'>[ ]*$/i', $remainder)) |
712
|
|
|
{ |
713
|
|
|
$Block['closed'] = true; |
714
|
|
|
} |
715
|
|
|
} |
716
|
|
|
|
717
|
|
|
return $Block; |
718
|
|
|
} |
719
|
|
|
} |
720
|
|
|
|
721
|
|
|
protected function blockMarkupContinue($Line, array $Block) |
722
|
|
|
{ |
723
|
|
|
if (isset($Block['closed'])) |
724
|
|
|
{ |
725
|
|
|
return; |
726
|
|
|
} |
727
|
|
|
|
728
|
|
|
if (preg_match('/^<'.$Block['name'].'(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*>/i', $Line['text'])) # open |
729
|
|
|
{ |
730
|
|
|
$Block['depth'] ++; |
731
|
|
|
} |
732
|
|
|
|
733
|
|
|
if (preg_match('/(.*?)<\/'.$Block['name'].'>[ ]*$/i', $Line['text'], $matches)) # close |
734
|
|
|
{ |
735
|
|
|
if ($Block['depth'] > 0) |
736
|
|
|
{ |
737
|
|
|
$Block['depth'] --; |
738
|
|
|
} |
739
|
|
|
else |
740
|
|
|
{ |
741
|
|
|
$Block['closed'] = true; |
742
|
|
|
} |
743
|
|
|
} |
744
|
|
|
|
745
|
|
|
if (isset($Block['interrupted'])) |
746
|
|
|
{ |
747
|
|
|
$Block['markup'] .= "\n"; |
748
|
|
|
|
749
|
|
|
unset($Block['interrupted']); |
750
|
|
|
} |
751
|
|
|
|
752
|
|
|
$Block['markup'] .= "\n".$Line['body']; |
753
|
|
|
|
754
|
|
|
return $Block; |
755
|
|
|
} |
756
|
|
|
|
757
|
|
|
# |
758
|
|
|
# Reference |
759
|
|
|
|
760
|
|
|
protected function blockReference($Line) |
761
|
|
|
{ |
762
|
|
|
if (preg_match('/^\[(.+?)\]:[ ]*<?(\S+?)>?(?:[ ]+["\'(](.+)["\')])?[ ]*$/', $Line['text'], $matches)) |
763
|
|
|
{ |
764
|
|
|
$id = strtolower($matches[1]); |
765
|
|
|
|
766
|
|
|
$Data = array( |
767
|
|
|
'url' => $matches[2], |
768
|
|
|
'title' => null, |
769
|
|
|
); |
770
|
|
|
|
771
|
|
|
if (isset($matches[3])) |
772
|
|
|
{ |
773
|
|
|
$Data['title'] = $matches[3]; |
774
|
|
|
} |
775
|
|
|
|
776
|
|
|
$this->DefinitionData['Reference'][$id] = $Data; |
777
|
|
|
|
778
|
|
|
$Block = array( |
779
|
|
|
'hidden' => true, |
780
|
|
|
); |
781
|
|
|
|
782
|
|
|
return $Block; |
783
|
|
|
} |
784
|
|
|
} |
785
|
|
|
|
786
|
|
|
# |
787
|
|
|
# Table |
788
|
|
|
|
789
|
|
|
protected function blockTable($Line, array $Block = null) |
790
|
|
|
{ |
791
|
|
|
if ( ! isset($Block) or isset($Block['type']) or isset($Block['interrupted'])) |
|
|
|
|
792
|
|
|
{ |
793
|
|
|
return; |
794
|
|
|
} |
795
|
|
|
|
796
|
|
|
if (strpos($Block['element']['text'], '|') !== false and chop($Line['text'], ' -:|') === '') |
|
|
|
|
797
|
|
|
{ |
798
|
|
|
$alignments = array(); |
799
|
|
|
|
800
|
|
|
$divider = $Line['text']; |
801
|
|
|
|
802
|
|
|
$divider = trim($divider); |
803
|
|
|
$divider = trim($divider, '|'); |
804
|
|
|
|
805
|
|
|
$dividerCells = explode('|', $divider); |
806
|
|
|
|
807
|
|
|
foreach ($dividerCells as $dividerCell) |
808
|
|
|
{ |
809
|
|
|
$dividerCell = trim($dividerCell); |
810
|
|
|
|
811
|
|
|
if ($dividerCell === '') |
812
|
|
|
{ |
813
|
|
|
continue; |
814
|
|
|
} |
815
|
|
|
|
816
|
|
|
$alignment = null; |
817
|
|
|
|
818
|
|
|
if ($dividerCell[0] === ':') |
819
|
|
|
{ |
820
|
|
|
$alignment = 'left'; |
821
|
|
|
} |
822
|
|
|
|
823
|
|
|
if (substr($dividerCell, - 1) === ':') |
824
|
|
|
{ |
825
|
|
|
$alignment = $alignment === 'left' ? 'center' : 'right'; |
826
|
|
|
} |
827
|
|
|
|
828
|
|
|
$alignments []= $alignment; |
829
|
|
|
} |
830
|
|
|
|
831
|
|
|
# ~ |
832
|
|
|
|
833
|
|
|
$HeaderElements = array(); |
834
|
|
|
|
835
|
|
|
$header = $Block['element']['text']; |
836
|
|
|
|
837
|
|
|
$header = trim($header); |
838
|
|
|
$header = trim($header, '|'); |
839
|
|
|
|
840
|
|
|
$headerCells = explode('|', $header); |
841
|
|
|
|
842
|
|
|
foreach ($headerCells as $index => $headerCell) |
843
|
|
|
{ |
844
|
|
|
$headerCell = trim($headerCell); |
845
|
|
|
|
846
|
|
|
$HeaderElement = array( |
847
|
|
|
'name' => 'th', |
848
|
|
|
'text' => $headerCell, |
849
|
|
|
'handler' => 'line', |
850
|
|
|
); |
851
|
|
|
|
852
|
|
|
if (isset($alignments[$index])) |
853
|
|
|
{ |
854
|
|
|
$alignment = $alignments[$index]; |
855
|
|
|
|
856
|
|
|
$HeaderElement['attributes'] = array( |
857
|
|
|
'style' => 'text-align: '.$alignment.';', |
858
|
|
|
); |
859
|
|
|
} |
860
|
|
|
|
861
|
|
|
$HeaderElements []= $HeaderElement; |
862
|
|
|
} |
863
|
|
|
|
864
|
|
|
# ~ |
865
|
|
|
|
866
|
|
|
$Block = array( |
867
|
|
|
'alignments' => $alignments, |
868
|
|
|
'identified' => true, |
869
|
|
|
'element' => array( |
870
|
|
|
'name' => 'table', |
871
|
|
|
'handler' => 'elements', |
872
|
|
|
), |
873
|
|
|
); |
874
|
|
|
|
875
|
|
|
$Block['element']['text'] []= array( |
876
|
|
|
'name' => 'thead', |
877
|
|
|
'handler' => 'elements', |
878
|
|
|
); |
879
|
|
|
|
880
|
|
|
$Block['element']['text'] []= array( |
881
|
|
|
'name' => 'tbody', |
882
|
|
|
'handler' => 'elements', |
883
|
|
|
'text' => array(), |
884
|
|
|
); |
885
|
|
|
|
886
|
|
|
$Block['element']['text'][0]['text'] []= array( |
887
|
|
|
'name' => 'tr', |
888
|
|
|
'handler' => 'elements', |
889
|
|
|
'text' => $HeaderElements, |
890
|
|
|
); |
891
|
|
|
|
892
|
|
|
return $Block; |
893
|
|
|
} |
894
|
|
|
} |
895
|
|
|
|
896
|
|
|
protected function blockTableContinue($Line, array $Block) |
897
|
|
|
{ |
898
|
|
|
if (isset($Block['interrupted'])) |
899
|
|
|
{ |
900
|
|
|
return; |
901
|
|
|
} |
902
|
|
|
|
903
|
|
|
if ($Line['text'][0] === '|' or strpos($Line['text'], '|')) |
|
|
|
|
904
|
|
|
{ |
905
|
|
|
$Elements = array(); |
906
|
|
|
|
907
|
|
|
$row = $Line['text']; |
908
|
|
|
|
909
|
|
|
$row = trim($row); |
910
|
|
|
$row = trim($row, '|'); |
911
|
|
|
|
912
|
|
|
preg_match_all('/(?:(\\\\[|])|[^|`]|`[^`]+`|`)+/', $row, $matches); |
913
|
|
|
|
914
|
|
|
foreach ($matches[0] as $index => $cell) |
915
|
|
|
{ |
916
|
|
|
$cell = trim($cell); |
917
|
|
|
|
918
|
|
|
$Element = array( |
919
|
|
|
'name' => 'td', |
920
|
|
|
'handler' => 'line', |
921
|
|
|
'text' => $cell, |
922
|
|
|
); |
923
|
|
|
|
924
|
|
|
if (isset($Block['alignments'][$index])) |
925
|
|
|
{ |
926
|
|
|
$Element['attributes'] = array( |
927
|
|
|
'style' => 'text-align: '.$Block['alignments'][$index].';', |
928
|
|
|
); |
929
|
|
|
} |
930
|
|
|
|
931
|
|
|
$Elements []= $Element; |
932
|
|
|
} |
933
|
|
|
|
934
|
|
|
$Element = array( |
935
|
|
|
'name' => 'tr', |
936
|
|
|
'handler' => 'elements', |
937
|
|
|
'text' => $Elements, |
938
|
|
|
); |
939
|
|
|
|
940
|
|
|
$Block['element']['text'][1]['text'] []= $Element; |
941
|
|
|
|
942
|
|
|
return $Block; |
943
|
|
|
} |
944
|
|
|
} |
945
|
|
|
|
946
|
|
|
# |
947
|
|
|
# ~ |
948
|
|
|
# |
949
|
|
|
|
950
|
|
|
protected function paragraph($Line) |
951
|
|
|
{ |
952
|
|
|
$Block = array( |
953
|
|
|
'element' => array( |
954
|
|
|
'name' => 'p', |
955
|
|
|
'text' => $Line['text'], |
956
|
|
|
'handler' => 'line', |
957
|
|
|
), |
958
|
|
|
); |
959
|
|
|
|
960
|
|
|
return $Block; |
961
|
|
|
} |
962
|
|
|
|
963
|
|
|
# |
964
|
|
|
# Inline Elements |
965
|
|
|
# |
966
|
|
|
|
967
|
|
|
protected $InlineTypes = array( |
968
|
|
|
'"' => array('SpecialCharacter'), |
969
|
|
|
'!' => array('Image'), |
970
|
|
|
'&' => array('SpecialCharacter'), |
971
|
|
|
'*' => array('Emphasis'), |
972
|
|
|
':' => array('Url'), |
973
|
|
|
'<' => array('UrlTag', 'EmailTag', 'Markup', 'SpecialCharacter'), |
974
|
|
|
'>' => array('SpecialCharacter'), |
975
|
|
|
'[' => array('Link'), |
976
|
|
|
'_' => array('Emphasis'), |
977
|
|
|
'`' => array('Code'), |
978
|
|
|
'~' => array('Strikethrough'), |
979
|
|
|
'\\' => array('EscapeSequence'), |
980
|
|
|
); |
981
|
|
|
|
982
|
|
|
# ~ |
983
|
|
|
|
984
|
|
|
protected $inlineMarkerList = '!"*_&[:<>`~\\'; |
985
|
|
|
|
986
|
|
|
# |
987
|
|
|
# ~ |
988
|
|
|
# |
989
|
|
|
|
990
|
|
|
public function line($text) |
991
|
|
|
{ |
992
|
|
|
$markup = ''; |
993
|
|
|
|
994
|
|
|
# $excerpt is based on the first occurrence of a marker |
995
|
|
|
|
996
|
|
|
while ($excerpt = strpbrk($text, $this->inlineMarkerList)) |
997
|
|
|
{ |
998
|
|
|
$marker = $excerpt[0]; |
999
|
|
|
|
1000
|
|
|
$markerPosition = strpos($text, $marker); |
1001
|
|
|
|
1002
|
|
|
$Excerpt = array('text' => $excerpt, 'context' => $text); |
1003
|
|
|
|
1004
|
|
|
foreach ($this->InlineTypes[$marker] as $inlineType) |
1005
|
|
|
{ |
1006
|
|
|
$Inline = $this->{'inline'.$inlineType}($Excerpt); |
1007
|
|
|
|
1008
|
|
|
if ( ! isset($Inline)) |
1009
|
|
|
{ |
1010
|
|
|
continue; |
1011
|
|
|
} |
1012
|
|
|
|
1013
|
|
|
# makes sure that the inline belongs to "our" marker |
1014
|
|
|
|
1015
|
|
|
if (isset($Inline['position']) and $Inline['position'] > $markerPosition) |
|
|
|
|
1016
|
|
|
{ |
1017
|
|
|
continue; |
1018
|
|
|
} |
1019
|
|
|
|
1020
|
|
|
# sets a default inline position |
1021
|
|
|
|
1022
|
|
|
if ( ! isset($Inline['position'])) |
1023
|
|
|
{ |
1024
|
|
|
$Inline['position'] = $markerPosition; |
1025
|
|
|
} |
1026
|
|
|
|
1027
|
|
|
# the text that comes before the inline |
1028
|
|
|
$unmarkedText = substr($text, 0, $Inline['position']); |
1029
|
|
|
|
1030
|
|
|
# compile the unmarked text |
1031
|
|
|
$markup .= $this->unmarkedText($unmarkedText); |
1032
|
|
|
|
1033
|
|
|
# compile the inline |
1034
|
|
|
$markup .= isset($Inline['markup']) ? $Inline['markup'] : $this->element($Inline['element']); |
1035
|
|
|
|
1036
|
|
|
# remove the examined text |
1037
|
|
|
$text = substr($text, $Inline['position'] + $Inline['extent']); |
1038
|
|
|
|
1039
|
|
|
continue 2; |
1040
|
|
|
} |
1041
|
|
|
|
1042
|
|
|
# the marker does not belong to an inline |
1043
|
|
|
|
1044
|
|
|
$unmarkedText = substr($text, 0, $markerPosition + 1); |
1045
|
|
|
|
1046
|
|
|
$markup .= $this->unmarkedText($unmarkedText); |
1047
|
|
|
|
1048
|
|
|
$text = substr($text, $markerPosition + 1); |
1049
|
|
|
} |
1050
|
|
|
|
1051
|
|
|
$markup .= $this->unmarkedText($text); |
1052
|
|
|
|
1053
|
|
|
return $markup; |
1054
|
|
|
} |
1055
|
|
|
|
1056
|
|
|
# |
1057
|
|
|
# ~ |
1058
|
|
|
# |
1059
|
|
|
|
1060
|
|
|
protected function inlineCode($Excerpt) |
1061
|
|
|
{ |
1062
|
|
|
$marker = $Excerpt['text'][0]; |
1063
|
|
|
|
1064
|
|
|
if (preg_match('/^('.$marker.'+)[ ]*(.+?)[ ]*(?<!'.$marker.')\1(?!'.$marker.')/s', $Excerpt['text'], $matches)) |
1065
|
|
|
{ |
1066
|
|
|
$text = $matches[2]; |
1067
|
|
|
$text = htmlspecialchars($text, ENT_NOQUOTES, 'UTF-8'); |
1068
|
|
|
$text = preg_replace("/[ ]*\n/", ' ', $text); |
1069
|
|
|
|
1070
|
|
|
return array( |
1071
|
|
|
'extent' => strlen($matches[0]), |
1072
|
|
|
'element' => array( |
1073
|
|
|
'name' => 'code', |
1074
|
|
|
'text' => $text, |
1075
|
|
|
), |
1076
|
|
|
); |
1077
|
|
|
} |
1078
|
|
|
} |
1079
|
|
|
|
1080
|
|
|
protected function inlineEmailTag($Excerpt) |
1081
|
|
|
{ |
1082
|
|
|
if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<((mailto:)?\S+?@\S+?)>/i', $Excerpt['text'], $matches)) |
|
|
|
|
1083
|
|
|
{ |
1084
|
|
|
$url = $matches[1]; |
1085
|
|
|
|
1086
|
|
|
if ( ! isset($matches[2])) |
1087
|
|
|
{ |
1088
|
|
|
$url = 'mailto:' . $url; |
1089
|
|
|
} |
1090
|
|
|
|
1091
|
|
|
return array( |
1092
|
|
|
'extent' => strlen($matches[0]), |
1093
|
|
|
'element' => array( |
1094
|
|
|
'name' => 'a', |
1095
|
|
|
'text' => $matches[1], |
1096
|
|
|
'attributes' => array( |
1097
|
|
|
'href' => $url, |
1098
|
|
|
), |
1099
|
|
|
), |
1100
|
|
|
); |
1101
|
|
|
} |
1102
|
|
|
} |
1103
|
|
|
|
1104
|
|
|
protected function inlineEmphasis($Excerpt) |
1105
|
|
|
{ |
1106
|
|
|
if ( ! isset($Excerpt['text'][1])) |
1107
|
|
|
{ |
1108
|
|
|
return; |
1109
|
|
|
} |
1110
|
|
|
|
1111
|
|
|
$marker = $Excerpt['text'][0]; |
1112
|
|
|
|
1113
|
|
|
if ($Excerpt['text'][1] === $marker and preg_match($this->StrongRegex[$marker], $Excerpt['text'], $matches)) |
|
|
|
|
1114
|
|
|
{ |
1115
|
|
|
$emphasis = 'strong'; |
1116
|
|
|
} |
1117
|
|
|
elseif (preg_match($this->EmRegex[$marker], $Excerpt['text'], $matches)) |
1118
|
|
|
{ |
1119
|
|
|
$emphasis = 'em'; |
1120
|
|
|
} |
1121
|
|
|
else |
1122
|
|
|
{ |
1123
|
|
|
return; |
1124
|
|
|
} |
1125
|
|
|
|
1126
|
|
|
return array( |
1127
|
|
|
'extent' => strlen($matches[0]), |
1128
|
|
|
'element' => array( |
1129
|
|
|
'name' => $emphasis, |
1130
|
|
|
'handler' => 'line', |
1131
|
|
|
'text' => $matches[1], |
1132
|
|
|
), |
1133
|
|
|
); |
1134
|
|
|
} |
1135
|
|
|
|
1136
|
|
|
protected function inlineEscapeSequence($Excerpt) |
1137
|
|
|
{ |
1138
|
|
|
if (isset($Excerpt['text'][1]) and in_array($Excerpt['text'][1], $this->specialCharacters)) |
|
|
|
|
1139
|
|
|
{ |
1140
|
|
|
return array( |
1141
|
|
|
'markup' => $Excerpt['text'][1], |
1142
|
|
|
'extent' => 2, |
1143
|
|
|
); |
1144
|
|
|
} |
1145
|
|
|
} |
1146
|
|
|
|
1147
|
|
|
protected function inlineImage($Excerpt) |
1148
|
|
|
{ |
1149
|
|
View Code Duplication |
if ( ! isset($Excerpt['text'][1]) or $Excerpt['text'][1] !== '[') |
|
|
|
|
1150
|
|
|
{ |
1151
|
|
|
return; |
1152
|
|
|
} |
1153
|
|
|
|
1154
|
|
|
$Excerpt['text']= substr($Excerpt['text'], 1); |
1155
|
|
|
|
1156
|
|
|
$Link = $this->inlineLink($Excerpt); |
1157
|
|
|
|
1158
|
|
|
if ($Link === null) |
1159
|
|
|
{ |
1160
|
|
|
return; |
1161
|
|
|
} |
1162
|
|
|
|
1163
|
|
|
$Inline = array( |
1164
|
|
|
'extent' => $Link['extent'] + 1, |
1165
|
|
|
'element' => array( |
1166
|
|
|
'name' => 'img', |
1167
|
|
|
'attributes' => array( |
1168
|
|
|
'src' => $Link['element']['attributes']['href'], |
1169
|
|
|
'alt' => $Link['element']['text'], |
1170
|
|
|
), |
1171
|
|
|
), |
1172
|
|
|
); |
1173
|
|
|
|
1174
|
|
|
$Inline['element']['attributes'] += $Link['element']['attributes']; |
1175
|
|
|
|
1176
|
|
|
unset($Inline['element']['attributes']['href']); |
1177
|
|
|
|
1178
|
|
|
return $Inline; |
1179
|
|
|
} |
1180
|
|
|
|
1181
|
|
|
protected function inlineLink($Excerpt) |
1182
|
|
|
{ |
1183
|
|
|
$Element = array( |
1184
|
|
|
'name' => 'a', |
1185
|
|
|
'handler' => 'line', |
1186
|
|
|
'text' => null, |
1187
|
|
|
'attributes' => array( |
1188
|
|
|
'href' => null, |
1189
|
|
|
'title' => null, |
1190
|
|
|
), |
1191
|
|
|
); |
1192
|
|
|
|
1193
|
|
|
$extent = 0; |
1194
|
|
|
|
1195
|
|
|
$remainder = $Excerpt['text']; |
1196
|
|
|
|
1197
|
|
|
if (preg_match('/\[((?:[^][]|(?R))*)\]/', $remainder, $matches)) |
1198
|
|
|
{ |
1199
|
|
|
$Element['text'] = $matches[1]; |
1200
|
|
|
|
1201
|
|
|
$extent += strlen($matches[0]); |
1202
|
|
|
|
1203
|
|
|
$remainder = substr($remainder, $extent); |
1204
|
|
|
} |
1205
|
|
|
else |
1206
|
|
|
{ |
1207
|
|
|
return; |
1208
|
|
|
} |
1209
|
|
|
|
1210
|
|
|
if (preg_match('/^[(]((?:[^ ()]|[(][^ )]+[)])+)(?:[ ]+("[^"]*"|\'[^\']*\'))?[)]/', $remainder, $matches)) |
1211
|
|
|
{ |
1212
|
|
|
$Element['attributes']['href'] = $matches[1]; |
1213
|
|
|
|
1214
|
|
|
if (isset($matches[2])) |
1215
|
|
|
{ |
1216
|
|
|
$Element['attributes']['title'] = substr($matches[2], 1, - 1); |
1217
|
|
|
} |
1218
|
|
|
|
1219
|
|
|
$extent += strlen($matches[0]); |
1220
|
|
|
} |
1221
|
|
|
else |
1222
|
|
|
{ |
1223
|
|
|
if (preg_match('/^\s*\[(.*?)\]/', $remainder, $matches)) |
1224
|
|
|
{ |
1225
|
|
|
$definition = strlen($matches[1]) ? $matches[1] : $Element['text']; |
1226
|
|
|
$definition = strtolower($definition); |
1227
|
|
|
|
1228
|
|
|
$extent += strlen($matches[0]); |
1229
|
|
|
} |
1230
|
|
|
else |
1231
|
|
|
{ |
1232
|
|
|
$definition = strtolower($Element['text']); |
1233
|
|
|
} |
1234
|
|
|
|
1235
|
|
|
if ( ! isset($this->DefinitionData['Reference'][$definition])) |
1236
|
|
|
{ |
1237
|
|
|
return; |
1238
|
|
|
} |
1239
|
|
|
|
1240
|
|
|
$Definition = $this->DefinitionData['Reference'][$definition]; |
1241
|
|
|
|
1242
|
|
|
$Element['attributes']['href'] = $Definition['url']; |
1243
|
|
|
$Element['attributes']['title'] = $Definition['title']; |
1244
|
|
|
} |
1245
|
|
|
|
1246
|
|
|
$Element['attributes']['href'] = str_replace(array('&', '<'), array('&', '<'), $Element['attributes']['href']); |
1247
|
|
|
|
1248
|
|
|
return array( |
1249
|
|
|
'extent' => $extent, |
1250
|
|
|
'element' => $Element, |
1251
|
|
|
); |
1252
|
|
|
} |
1253
|
|
|
|
1254
|
|
|
protected function inlineMarkup($Excerpt) |
1255
|
|
|
{ |
1256
|
|
|
if ($this->markupEscaped or strpos($Excerpt['text'], '>') === false) |
|
|
|
|
1257
|
|
|
{ |
1258
|
|
|
return; |
1259
|
|
|
} |
1260
|
|
|
|
1261
|
|
View Code Duplication |
if ($Excerpt['text'][1] === '/' and preg_match('/^<\/\w*[ ]*>/s', $Excerpt['text'], $matches)) |
|
|
|
|
1262
|
|
|
{ |
1263
|
|
|
return array( |
1264
|
|
|
'markup' => $matches[0], |
1265
|
|
|
'extent' => strlen($matches[0]), |
1266
|
|
|
); |
1267
|
|
|
} |
1268
|
|
|
|
1269
|
|
View Code Duplication |
if ($Excerpt['text'][1] === '!' and preg_match('/^<!---?[^>-](?:-?[^-])*-->/s', $Excerpt['text'], $matches)) |
|
|
|
|
1270
|
|
|
{ |
1271
|
|
|
return array( |
1272
|
|
|
'markup' => $matches[0], |
1273
|
|
|
'extent' => strlen($matches[0]), |
1274
|
|
|
); |
1275
|
|
|
} |
1276
|
|
|
|
1277
|
|
View Code Duplication |
if ($Excerpt['text'][1] !== ' ' and preg_match('/^<\w*(?:[ ]*'.$this->regexHtmlAttribute.')*[ ]*\/?>/s', $Excerpt['text'], $matches)) |
|
|
|
|
1278
|
|
|
{ |
1279
|
|
|
return array( |
1280
|
|
|
'markup' => $matches[0], |
1281
|
|
|
'extent' => strlen($matches[0]), |
1282
|
|
|
); |
1283
|
|
|
} |
1284
|
|
|
} |
1285
|
|
|
|
1286
|
|
|
protected function inlineSpecialCharacter($Excerpt) |
1287
|
|
|
{ |
1288
|
|
|
if ($Excerpt['text'][0] === '&' and ! preg_match('/^&#?\w+;/', $Excerpt['text'])) |
|
|
|
|
1289
|
|
|
{ |
1290
|
|
|
return array( |
1291
|
|
|
'markup' => '&', |
1292
|
|
|
'extent' => 1, |
1293
|
|
|
); |
1294
|
|
|
} |
1295
|
|
|
|
1296
|
|
|
$SpecialCharacter = array('>' => 'gt', '<' => 'lt', '"' => 'quot'); |
1297
|
|
|
|
1298
|
|
|
if (isset($SpecialCharacter[$Excerpt['text'][0]])) |
1299
|
|
|
{ |
1300
|
|
|
return array( |
1301
|
|
|
'markup' => '&'.$SpecialCharacter[$Excerpt['text'][0]].';', |
1302
|
|
|
'extent' => 1, |
1303
|
|
|
); |
1304
|
|
|
} |
1305
|
|
|
} |
1306
|
|
|
|
1307
|
|
|
protected function inlineStrikethrough($Excerpt) |
1308
|
|
|
{ |
1309
|
|
|
if ( ! isset($Excerpt['text'][1])) |
1310
|
|
|
{ |
1311
|
|
|
return; |
1312
|
|
|
} |
1313
|
|
|
|
1314
|
|
|
if ($Excerpt['text'][1] === '~' and preg_match('/^~~(?=\S)(.+?)(?<=\S)~~/', $Excerpt['text'], $matches)) |
|
|
|
|
1315
|
|
|
{ |
1316
|
|
|
return array( |
1317
|
|
|
'extent' => strlen($matches[0]), |
1318
|
|
|
'element' => array( |
1319
|
|
|
'name' => 'del', |
1320
|
|
|
'text' => $matches[1], |
1321
|
|
|
'handler' => 'line', |
1322
|
|
|
), |
1323
|
|
|
); |
1324
|
|
|
} |
1325
|
|
|
} |
1326
|
|
|
|
1327
|
|
|
protected function inlineUrl($Excerpt) |
1328
|
|
|
{ |
1329
|
|
View Code Duplication |
if ($this->urlsLinked !== true or ! isset($Excerpt['text'][2]) or $Excerpt['text'][2] !== '/') |
|
|
|
|
1330
|
|
|
{ |
1331
|
|
|
return; |
1332
|
|
|
} |
1333
|
|
|
|
1334
|
|
|
if (preg_match('/\bhttps?:[\/]{2}[^\s<]+\b\/*/ui', $Excerpt['context'], $matches, PREG_OFFSET_CAPTURE)) |
1335
|
|
|
{ |
1336
|
|
|
$Inline = array( |
1337
|
|
|
'extent' => strlen($matches[0][0]), |
1338
|
|
|
'position' => $matches[0][1], |
1339
|
|
|
'element' => array( |
1340
|
|
|
'name' => 'a', |
1341
|
|
|
'text' => $matches[0][0], |
1342
|
|
|
'attributes' => array( |
1343
|
|
|
'href' => $matches[0][0], |
1344
|
|
|
), |
1345
|
|
|
), |
1346
|
|
|
); |
1347
|
|
|
|
1348
|
|
|
return $Inline; |
1349
|
|
|
} |
1350
|
|
|
} |
1351
|
|
|
|
1352
|
|
|
protected function inlineUrlTag($Excerpt) |
1353
|
|
|
{ |
1354
|
|
|
if (strpos($Excerpt['text'], '>') !== false and preg_match('/^<(\w+:\/{2}[^ >]+)>/i', $Excerpt['text'], $matches)) |
|
|
|
|
1355
|
|
|
{ |
1356
|
|
|
$url = str_replace(array('&', '<'), array('&', '<'), $matches[1]); |
1357
|
|
|
|
1358
|
|
|
return array( |
1359
|
|
|
'extent' => strlen($matches[0]), |
1360
|
|
|
'element' => array( |
1361
|
|
|
'name' => 'a', |
1362
|
|
|
'text' => $url, |
1363
|
|
|
'attributes' => array( |
1364
|
|
|
'href' => $url, |
1365
|
|
|
), |
1366
|
|
|
), |
1367
|
|
|
); |
1368
|
|
|
} |
1369
|
|
|
} |
1370
|
|
|
|
1371
|
|
|
# ~ |
1372
|
|
|
|
1373
|
|
|
protected function unmarkedText($text) |
1374
|
|
|
{ |
1375
|
|
|
if ($this->breaksEnabled) |
1376
|
|
|
{ |
1377
|
|
|
$text = preg_replace('/[ ]*\n/', "<br />\n", $text); |
1378
|
|
|
} |
1379
|
|
|
else |
1380
|
|
|
{ |
1381
|
|
|
$text = preg_replace('/(?:[ ][ ]+|[ ]*\\\\)\n/', "<br />\n", $text); |
1382
|
|
|
$text = str_replace(" \n", "\n", $text); |
1383
|
|
|
} |
1384
|
|
|
|
1385
|
|
|
return $text; |
1386
|
|
|
} |
1387
|
|
|
|
1388
|
|
|
# |
1389
|
|
|
# Handlers |
1390
|
|
|
# |
1391
|
|
|
|
1392
|
|
|
protected function element(array $Element) |
1393
|
|
|
{ |
1394
|
|
|
$markup = '<'.$Element['name']; |
1395
|
|
|
|
1396
|
|
|
if (isset($Element['attributes'])) |
1397
|
|
|
{ |
1398
|
|
|
foreach ($Element['attributes'] as $name => $value) |
1399
|
|
|
{ |
1400
|
|
|
if ($value === null) |
1401
|
|
|
{ |
1402
|
|
|
continue; |
1403
|
|
|
} |
1404
|
|
|
|
1405
|
|
|
$markup .= ' '.$name.'="'.$value.'"'; |
1406
|
|
|
} |
1407
|
|
|
} |
1408
|
|
|
|
1409
|
|
|
if (isset($Element['text'])) |
1410
|
|
|
{ |
1411
|
|
|
$markup .= '>'; |
1412
|
|
|
|
1413
|
|
|
if (isset($Element['handler'])) |
1414
|
|
|
{ |
1415
|
|
|
$markup .= $this->{$Element['handler']}($Element['text']); |
1416
|
|
|
} |
1417
|
|
|
else |
1418
|
|
|
{ |
1419
|
|
|
$markup .= $Element['text']; |
1420
|
|
|
} |
1421
|
|
|
|
1422
|
|
|
$markup .= '</'.$Element['name'].'>'; |
1423
|
|
|
} |
1424
|
|
|
else |
1425
|
|
|
{ |
1426
|
|
|
$markup .= ' />'; |
1427
|
|
|
} |
1428
|
|
|
|
1429
|
|
|
return $markup; |
1430
|
|
|
} |
1431
|
|
|
|
1432
|
|
|
protected function elements(array $Elements) |
1433
|
|
|
{ |
1434
|
|
|
$markup = ''; |
1435
|
|
|
|
1436
|
|
|
foreach ($Elements as $Element) |
1437
|
|
|
{ |
1438
|
|
|
$markup .= "\n" . $this->element($Element); |
1439
|
|
|
} |
1440
|
|
|
|
1441
|
|
|
$markup .= "\n"; |
1442
|
|
|
|
1443
|
|
|
return $markup; |
1444
|
|
|
} |
1445
|
|
|
|
1446
|
|
|
# ~ |
1447
|
|
|
|
1448
|
|
|
protected function li($lines) |
1449
|
|
|
{ |
1450
|
|
|
$markup = $this->lines($lines); |
1451
|
|
|
|
1452
|
|
|
$trimmedMarkup = trim($markup); |
1453
|
|
|
|
1454
|
|
|
if ( ! in_array('', $lines) and substr($trimmedMarkup, 0, 3) === '<p>') |
|
|
|
|
1455
|
|
|
{ |
1456
|
|
|
$markup = $trimmedMarkup; |
1457
|
|
|
$markup = substr($markup, 3); |
1458
|
|
|
|
1459
|
|
|
$position = strpos($markup, "</p>"); |
1460
|
|
|
|
1461
|
|
|
$markup = substr_replace($markup, '', $position, 4); |
1462
|
|
|
} |
1463
|
|
|
|
1464
|
|
|
return $markup; |
1465
|
|
|
} |
1466
|
|
|
|
1467
|
|
|
# |
1468
|
|
|
# Deprecated Methods |
1469
|
|
|
# |
1470
|
|
|
|
1471
|
|
|
function parse($text) |
|
|
|
|
1472
|
|
|
{ |
1473
|
|
|
$markup = $this->text($text); |
1474
|
|
|
|
1475
|
|
|
return $markup; |
1476
|
|
|
} |
1477
|
|
|
|
1478
|
|
|
# |
1479
|
|
|
# Static Methods |
1480
|
|
|
# |
1481
|
|
|
|
1482
|
|
|
static function instance($name = 'default') |
|
|
|
|
1483
|
|
|
{ |
1484
|
|
|
if (isset(self::$instances[$name])) |
1485
|
|
|
{ |
1486
|
|
|
return self::$instances[$name]; |
1487
|
|
|
} |
1488
|
|
|
|
1489
|
|
|
$instance = new static(); |
1490
|
|
|
|
1491
|
|
|
self::$instances[$name] = $instance; |
1492
|
|
|
|
1493
|
|
|
return $instance; |
1494
|
|
|
} |
1495
|
|
|
|
1496
|
|
|
private static $instances = array(); |
1497
|
|
|
|
1498
|
|
|
# |
1499
|
|
|
# Fields |
1500
|
|
|
# |
1501
|
|
|
|
1502
|
|
|
protected $DefinitionData; |
1503
|
|
|
|
1504
|
|
|
# |
1505
|
|
|
# Read-Only |
1506
|
|
|
|
1507
|
|
|
protected $specialCharacters = array( |
1508
|
|
|
'\\', '`', '*', '_', '{', '}', '[', ']', '(', ')', '>', '#', '+', '-', '.', '!', '|', |
1509
|
|
|
); |
1510
|
|
|
|
1511
|
|
|
protected $StrongRegex = array( |
1512
|
|
|
'*' => '/^[*]{2}((?:\\\\\*|[^*]|[*][^*]*[*])+?)[*]{2}(?![*])/s', |
1513
|
|
|
'_' => '/^__((?:\\\\_|[^_]|_[^_]*_)+?)__(?!_)/us', |
1514
|
|
|
); |
1515
|
|
|
|
1516
|
|
|
protected $EmRegex = array( |
1517
|
|
|
'*' => '/^[*]((?:\\\\\*|[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s', |
1518
|
|
|
'_' => '/^_((?:\\\\_|[^_]|__[^_]*__)+?)_(?!_)\b/us', |
1519
|
|
|
); |
1520
|
|
|
|
1521
|
|
|
protected $regexHtmlAttribute = '[a-zA-Z_:][\w:.-]*(?:\s*=\s*(?:[^"\'=<>`\s]+|"[^"]*"|\'[^\']*\'))?'; |
1522
|
|
|
|
1523
|
|
|
protected $voidElements = array( |
1524
|
|
|
'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', |
1525
|
|
|
); |
1526
|
|
|
|
1527
|
|
|
protected $textLevelElements = array( |
1528
|
|
|
'a', 'br', 'bdo', 'abbr', 'blink', 'nextid', 'acronym', 'basefont', |
1529
|
|
|
'b', 'em', 'big', 'cite', 'small', 'spacer', 'listing', |
1530
|
|
|
'i', 'rp', 'del', 'code', 'strike', 'marquee', |
1531
|
|
|
'q', 'rt', 'ins', 'font', 'strong', |
1532
|
|
|
's', 'tt', 'sub', 'mark', |
1533
|
|
|
'u', 'xm', 'sup', 'nobr', |
1534
|
|
|
'var', 'ruby', |
1535
|
|
|
'wbr', 'span', |
1536
|
|
|
'time', |
1537
|
|
|
); |
1538
|
|
|
} |
1539
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.