forked from cebe/markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Markdown.php
818 lines (737 loc) · 20.6 KB
/
Markdown.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
<?php
/**
* @copyright Copyright (c) 2014 Carsten Brandt
* @license https://github.com/cebe/markdown/blob/master/LICENSE
* @link https://github.com/cebe/markdown#readme
*/
namespace cebe\markdown;
// work around https://github.com/facebook/hhvm/issues/1120
defined('ENT_HTML401') || define('ENT_HTML401', 0);
/**
* Markdown parser for the [initial markdown spec](http://daringfireball.net/projects/markdown/syntax).
*
* @author Carsten Brandt <[email protected]>
*/
class Markdown extends Parser
{
/**
* @var boolean whether to format markup according to HTML5 spec.
* Defaults to `false` which means that markup is formatted as HTML4.
*/
public $html5 = false;
/**
* @var bool enable support `start` attribute of ordered lists. This means that lists
* will start with the number you actually type in markdown and not the HTML generated one.
* Defaults to `false` which means that numeration of all ordered lists(<ol>) starts with 1.
*/
public $keepListStartNumber = false;
/**
* @var array these are "escapeable" characters. When using one of these prefixed with a
* backslash, the character will be outputted without the backslash and is not interpreted
* as markdown.
*/
protected $escapeCharacters = [
'\\', // backslash
'`', // backtick
'*', // asterisk
'_', // underscore
'{', '}', // curly braces
'[', ']', // square brackets
'(', ')', // parentheses
'#', // hash mark
'+', // plus sign
'-', // minus sign (hyphen)
'.', // dot
'!', // exclamation mark
'<', '>',
];
/**
* @var array a list of defined references in this document.
*/
protected $references = [];
/**
* @var array HTML elements considered as inline elements.
* @see http://www.w3.org/wiki/HTML/Elements#Text-level_semantics
*/
protected $inlineHtmlElements = [
'a', 'abbr', 'acronym',
'b', 'basefont', 'bdo', 'big', 'br', 'button', 'blink',
'cite', 'code',
'del', 'dfn',
'em',
'font',
'i', 'img', 'ins', 'input', 'iframe',
'kbd',
'label', 'listing',
'map', 'mark',
'nobr',
'object',
'q',
'rp', 'rt', 'ruby',
's', 'samp', 'script', 'select', 'small', 'spacer', 'span', 'strong', 'sub', 'sup',
'tt', 'var',
'u',
'wbr',
'time',
];
/**
* @var array HTML elements known to be self-closing.
*/
protected $selfClosingHtmlElements = [
'br', 'hr', 'img', 'input', 'nobr',
];
/**
* @inheritDoc
*/
protected function inlineMarkers()
{
return [
'&' => 'parseEntity',
'![' => 'parseImage',
'*' => 'parseEmphStrong',
'_' => 'parseEmphStrong',
'<' => 'parseLt',
'>' => 'parseGt',
'[' => 'parseLink',
'\\' => 'parseEscape',
'`' => 'parseCode',
];
}
/**
* @inheritDoc
*/
protected function prepare()
{
// reset references
$this->references = [];
}
/**
* @inheritDoc
*/
protected function identifyLine($lines, $current)
{
$line = $lines[$current];
switch($line[0])
{
case '<': // HTML block
if ($this->identifyHtmlBlock($lines, $current)) {
return 'html';
}
break;
case '>': // quote
if (!isset($line[1]) || $line[1] === ' ' || $line[1] === "\t") {
return 'quote';
}
break;
case '_':
// at least 3 of -, * or _ on one line make a hr
if (preg_match('/^(_)\s*\1\s*\1(\1|\s)*$/', $line)) {
return 'hr';
}
break;
case '-':
case '+':
case '*':
// at least 3 of -, * or _ on one line make a hr
if (preg_match('/^([\-\*])\s*\1\s*\1(\1|\s)*$/', $line)) {
return 'hr';
}
if (isset($line[1]) && ($line[1] === ' ' || $line[1] === "\t")) {
return 'ul';
}
break;
case '#':
return 'headline';
case '[': // reference
if ($this->identifyReference($line)) {
return 'reference';
}
break;
case "\t":
return 'code';
case ' ':
// indentation >= 4 is code
if (strncmp($line, ' ', 4) === 0) {
return 'code';
}
// at least 3 of -, * or _ on one line make a hr
if (preg_match('/^ {0,3}([\-\*_])\s*\1\s*\1(\1|\s)*$/', $line)) {
return 'hr';
}
// could be indented list
if (preg_match('/^ {0,3}[\-\+\*][ \t]/', $line)) {
return 'ul';
}
// could be indented reference
if ($this->identifyReference($line)) {
return 'reference';
}
// no break;
default:
if (preg_match('/^ {0,3}\d+\.[ \t]/', $line)) {
return 'ol';
}
}
if ($this->identifyHeadline($lines, $current)) {
return 'headline';
}
return 'paragraph';
}
protected function identifyHeadline($lines, $current)
{
return (
!empty($lines[$current + 1]) &&
($lines[$current + 1][0] === '=' || $lines[$current + 1][0] === '-') &&
preg_match('/^(\-+|=+)\s*$/', $lines[$current + 1])
);
}
protected function identifyHtmlBlock($lines, $current)
{
$line = $lines[$current];
if (isset($line[1]) && $line[1] == ' ') {
return false; // no html tag
}
if (strncmp($line, '<!--', 4) === 0) {
return true; // a html comment
}
$gtPos = strpos($lines[$current], '>');
$spacePos = strpos($lines[$current], ' ');
if ($gtPos === false && $spacePos === false) {
return false; // no html tag
} elseif ($spacePos === false) {
$tag = rtrim(substr($line, 1, $gtPos - 1), '/');
} else {
$tag = rtrim(substr($line, 1, min($gtPos, $spacePos) - 1), '/');
}
if (!ctype_alnum($tag) || in_array(strtolower($tag), $this->inlineHtmlElements)) {
return false; // no html tag or inline html tag
}
return true;
}
protected function identifyReference($line)
{
return preg_match('/^ {0,3}\[(.+?)\]:\s*([^\s]+?)(?:\s+[\'"](.+?)[\'"])?\s*$/', $line);
}
/**
* Consume lines for a paragraph
*/
protected function consumeParagraph($lines, $current)
{
// consume until newline or intended line
$block = [
'type' => 'paragraph',
'content' => [],
];
for ($i = $current, $count = count($lines); $i < $count; $i++) {
if (ltrim($lines[$i]) !== '' && $lines[$i][0] != "\t" && strncmp($lines[$i], ' ', 4) !== 0 && !$this->identifyHeadline($lines, $i)) {
$block['content'][] = $lines[$i];
} else {
break;
}
}
return [$block, --$i];
}
/**
* Consume lines for a blockquote element
*/
protected function consumeQuote($lines, $current)
{
// consume until newline
$block = [
'type' => 'quote',
'content' => [],
'simple' => true,
];
for ($i = $current, $count = count($lines); $i < $count; $i++) {
$line = $lines[$i];
if (ltrim($line) !== '') {
if ($line[0] == '>' && !isset($line[1])) {
$line = '';
} elseif (strncmp($line, '> ', 2) === 0) {
$line = substr($line, 2);
}
$block['content'][] = $line;
} else {
break;
}
}
return [$block, $i];
}
/**
* Consume lines for a code block element
*/
protected function consumeCode($lines, $current)
{
// consume until newline
$block = [
'type' => 'code',
'content' => [],
];
for ($i = $current, $count = count($lines); $i < $count; $i++) {
$line = $lines[$i];
// a line is considered to belong to this code block as long as it is intended by 4 spaces or a tab
if (isset($line[0]) && ($line[0] === "\t" || strncmp($lines[$i], ' ', 4) === 0)) {
$line = $line[0] === "\t" ? substr($line, 1) : substr($line, 4);
$block['content'][] = $line;
// but also if it is empty and the next line is intended by 4 spaces or a tab
} elseif ((empty($line) || rtrim($line) === '') && isset($lines[$i + 1][0]) &&
($lines[$i + 1][0] === "\t" || strncmp($lines[$i + 1], ' ', 4) === 0)) {
if (!empty($line)) {
$line = $line[0] === "\t" ? substr($line, 1) : substr($line, 4);
}
$block['content'][] = $line;
} else {
break;
}
}
return [$block, --$i];
}
/**
* Consume lines for an ordered list
*/
protected function consumeOl($lines, $current)
{
// consume until newline
$block = [
'type' => 'list',
'list' => 'ol',
'attr' => [],
'items' => [],
];
return $this->consumeList($lines, $current, $block, 'ol');
}
/**
* Consume lines for an unordered list
*/
protected function consumeUl($lines, $current)
{
// consume until newline
$block = [
'type' => 'list',
'list' => 'ul',
'items' => [],
];
return $this->consumeList($lines, $current, $block, 'ul');
}
private function consumeList($lines, $current, $block, $type)
{
$item = 0;
$indent = '';
$len = 0;
// track the indentation of list markers, if indented more than previous element
// a list marker is considered to be long to a lower level
$leadSpace = 3;
for ($i = $current, $count = count($lines); $i < $count; $i++) {
$line = $lines[$i];
// match list marker on the beginning of the line
if (preg_match($type == 'ol' ? '/^( {0,'.$leadSpace.'})(\d+)\.[ \t]+/' : '/^( {0,'.$leadSpace.'})[\-\+\*][ \t]+/', $line, $matches)) {
if (($len = substr_count($matches[0], "\t")) > 0) {
$indent = str_repeat("\t", $len);
$line = substr($line, strlen($matches[0]));
} else {
$len = strlen($matches[0]);
$indent = str_repeat(' ', $len);
$line = substr($line, $len);
}
if ($i === $current) {
$leadSpace = strlen($matches[1]) + 1;
}
if ($type == 'ol' && $this->keepListStartNumber) {
// attr `start` for ol
if (!isset($block['attr']['start']) && isset($matches[2])) {
$block['attr']['start'] = $matches[2];
}
}
$block['items'][++$item][] = $line;
} elseif (ltrim($line) === '') {
// next line after empty one is also a list or indented -> lazy list
if (isset($lines[$i + 1][0]) && (
$this->identifyLine($lines, $i + 1) === $type ||
(strncmp($lines[$i + 1], $indent, $len) === 0 || !empty($lines[$i + 1]) && $lines[$i + 1][0] == "\t"))) {
$block['items'][$item][] = $line;
$block['lazyItems'][$item] = true;
} else {
break;
}
} else {
if ($line[0] === "\t") {
$line = substr($line, 1);
} elseif (strncmp($line, $indent, $len) === 0) {
$line = substr($line, $len);
}
$block['items'][$item][] = $line;
}
}
// make last item lazy if item before was lazy
if (isset($block['lazyItems'][$item - 1])) {
$block['lazyItems'][$item] = true;
}
return [$block, $i];
}
/**
* Consume lines for a headline
*/
protected function consumeHeadline($lines, $current)
{
if ($lines[$current][0] === '#') {
$level = 1;
while (isset($lines[$current][$level]) && $lines[$current][$level] === '#' && $level < 6) {
$level++;
}
$block = [
'type' => 'headline',
'content' => trim($lines[$current], "# \t"),
'level' => $level,
];
return [$block, $current];
}
$block = [
'type' => 'headline',
'content' => $lines[$current],
'level' => $lines[$current + 1][0] === '=' ? 1 : 2,
];
return [$block, $current + 1];
}
/**
* Consume lines for an HTML block
*/
protected function consumeHtml($lines, $current)
{
$block = [
'type' => 'html',
'content' => [],
];
if (strncmp($lines[$current], '<!--', 4) === 0) { // html comment
for ($i = $current, $count = count($lines); $i < $count; $i++) {
$line = $lines[$i];
$block['content'][] = $line;
if (strpos($line, '-->') !== false) {
break;
}
}
} else {
$tag = rtrim(substr($lines[$current], 1, min(strpos($lines[$current], '>'), strpos($lines[$current] . ' ', ' ')) - 1), '/');
$level = 0;
if (in_array($tag, $this->selfClosingHtmlElements)) {
$level--;
}
for ($i = $current, $count = count($lines); $i < $count; $i++) {
$line = $lines[$i];
$block['content'][] = $line;
$level += substr_count($line, "<$tag") - substr_count($line, "</$tag>");
if ($level <= 0) {
break;
}
}
}
return [$block, $i];
}
/**
* Consume a horizontal rule
*/
protected function consumeHr($lines, $current)
{
$block = [
'type' => 'hr',
];
return [$block, $current];
}
/**
* Consume link references
*/
protected function consumeReference($lines, $current)
{
while (isset($lines[$current]) && preg_match('/^ {0,3}\[(.+?)\]:\s*(.+?)(?:\s+[\(\'"](.+?)[\)\'"])?\s*$/', $lines[$current], $matches)) {
$label = strtolower($matches[1]);
$this->references[$label] = [
'url' => $matches[2],
];
if (isset($matches[3])) {
$this->references[$label]['title'] = $matches[3];
} else {
// title may be on the next line
if (isset($lines[$current + 1]) && preg_match('/^\s+[\(\'"](.+?)[\)\'"]\s*$/', $lines[$current + 1], $matches)) {
$this->references[$label]['title'] = $matches[1];
$current++;
}
}
$current++;
}
return [false, --$current];
}
// rendering
/**
* Renders a blockquote
*/
protected function renderQuote($block)
{
return '<blockquote>' . $this->parseBlocks($block['content']) . '</blockquote>';
}
/**
* Renders a code block
*/
protected function renderCode($block)
{
$class = isset($block['language']) ? ' class="language-' . $block['language'] . '"' : '';
return "<pre><code$class>" . htmlspecialchars(implode("\n", $block['content']) . "\n", ENT_NOQUOTES, 'UTF-8') . '</code></pre>';
}
/**
* Renders a list
*/
protected function renderList($block)
{
$type = $block['list'];
if (!empty($block['attr'])) {
$output = "<$type " . $this->generateHtmlAttributes($block['attr']) . ">\n";
} else {
$output = "<$type>\n";
}
foreach ($block['items'] as $item => $itemLines) {
$output .= '<li>';
if (!isset($block['lazyItems'][$item])) {
$firstPar = [];
while (!empty($itemLines) && rtrim($itemLines[0]) !== '' && $this->identifyLine($itemLines, 0) === 'paragraph') {
$firstPar[] = array_shift($itemLines);
}
$output .= $this->parseInline(implode("\n", $firstPar));
}
if (!empty($itemLines)) {
$output .= $this->parseBlocks($itemLines);
}
$output .= "</li>\n";
}
return $output . "</$type>";
}
/**
* Renders a headline
*/
protected function renderHeadline($block)
{
$tag = 'h' . $block['level'];
return "<$tag>" . $this->parseInline($block['content']) . "</$tag>";
}
/**
* Renders an HTML block
*/
protected function renderHtml($block)
{
return implode("\n", $block['content']);
}
/**
* Renders a horizontal rule
*/
protected function renderHr($block)
{
return $this->html5 ? '<hr>' : '<hr />';
}
// inline parsing
/**
* @inheritdocs
*
* Parses a newline indicated by two spaces on the end of a markdown line.
*/
protected function parsePlainText($text)
{
return str_replace(" \n", $this->html5 ? "<br>\n" : "<br />\n", $text);
}
/**
* Parses an & or a html entity definition.
*/
protected function parseEntity($text)
{
// html entities e.g. © © ©
if (preg_match('/^&#?[\w\d]+;/', $text, $matches)) {
return [$matches[0], strlen($matches[0])];
} else {
return ['&', 1];
}
}
/**
* Parses inline HTML.
*/
protected function parseLt($text)
{
if (strpos($text, '>') !== false) {
if (!in_array('parseLink', $this->context)) { // do not allow links in links
if (preg_match('/^<([^\s]*?@[^\s]*?\.\w+?)>/', $text, $matches)) {
// email address
$email = htmlspecialchars($matches[1], ENT_NOQUOTES, 'UTF-8');
return [
"<a href=\"mailto:$email\">$email</a>", // TODO encode mail with entities
strlen($matches[0])
];
} elseif (preg_match('/^<([a-z]{3,}:\/\/[^\s]+?)>/', $text, $matches)) {
// URL
$url = htmlspecialchars($matches[1], ENT_COMPAT | ENT_HTML401, 'UTF-8');
$text = htmlspecialchars(urldecode($matches[1]), ENT_NOQUOTES, 'UTF-8');
return ["<a href=\"$url\">$text</a>", strlen($matches[0])];
}
}
if (preg_match('~^</?(\w+\d?)( .*?)?>~', $text, $matches)) {
// HTML tags
return [$matches[0], strlen($matches[0])];
} elseif (preg_match('~^<!--.*?-->~', $text, $matches)) {
// HTML comments
return [$matches[0], strlen($matches[0])];
}
}
return ['<', 1];
}
/**
* Escapes `>` characters.
*/
protected function parseGt($text)
{
return ['>', 1];
}
/**
* Parses escaped special characters.
*/
protected function parseEscape($text)
{
if (isset($text[1]) && in_array($text[1], $this->escapeCharacters)) {
return [$text[1], 2];
}
return [$text[0], 1];
}
/**
* Parses a link indicated by `[`.
*/
protected function parseLink($markdown)
{
if (!in_array('parseLink', array_slice($this->context, 1)) && ($parts = $this->parseLinkOrImage($markdown)) !== false) {
list($text, $url, $title, $offset) = $parts;
$link = '<a href="' . htmlspecialchars($url, ENT_COMPAT | ENT_HTML401, 'UTF-8') . '"'
. (empty($title) ? '' : ' title="' . htmlspecialchars($title, ENT_COMPAT | ENT_HTML401, 'UTF-8') . '"')
. '>' . $this->parseInline($text) . '</a>';
return [$link, $offset];
} else {
// remove all starting [ markers to avoid next one to be parsed as link
$result = '[';
$i = 1;
while (isset($markdown[$i]) && $markdown[$i] == '[') {
$result .= '[';
$i++;
}
return [$result, $i];
}
}
/**
* Parses an image indicated by `![`.
*/
protected function parseImage($markdown)
{
if (($parts = $this->parseLinkOrImage(substr($markdown, 1))) !== false) {
list($text, $url, $title, $offset) = $parts;
$image = '<img src="' . htmlspecialchars($url, ENT_COMPAT | ENT_HTML401, 'UTF-8') . '"'
. ' alt="' . htmlspecialchars($text, ENT_COMPAT | ENT_HTML401, 'UTF-8') . '"'
. (empty($title) ? '' : ' title="' . htmlspecialchars($title, ENT_COMPAT | ENT_HTML401, 'UTF-8') . '"')
. ($this->html5 ? '>' : ' />');
return [$image, $offset + 1];
} else {
// remove all starting [ markers to avoid next one to be parsed as link
$result = '!';
$i = 1;
while (isset($markdown[$i]) && $markdown[$i] == '[') {
$result .= '[';
$i++;
}
return [$result, $i];
}
}
protected function parseLinkOrImage($markdown)
{
if (strpos($markdown, ']') !== false && preg_match('/\[((?>[^\]\[]+|(?R))*)\]/', $markdown, $textMatches)) { // TODO improve bracket regex
$text = $textMatches[1];
$offset = strlen($textMatches[0]);
$markdown = substr($markdown, $offset);
$pattern = <<<REGEXP
/(?(R) # in case of recursion match parentheses
\(((?>[^\s()]+)|(?R))*\)
| # else match a link with title
^\((((?>[^\s()]+)|(?R))*)(\s+"(.*?)")?\)
)/x
REGEXP;
if (preg_match($pattern, $markdown, $refMatches)) {
// inline link
return [
$text,
isset($refMatches[2]) ? $refMatches[2] : '', // url
empty($refMatches[5]) ? null: $refMatches[5], // title
$offset + strlen($refMatches[0]), // offset
null, // reference key
];
} elseif (preg_match('/^([ \n]?\[(.*?)\])?/s', $markdown, $refMatches)) {
// reference style link
if (empty($refMatches[2])) {
$key = strtolower($text);
} else {
$key = strtolower($refMatches[2]);
}
$normalizedKey = preg_replace('/\s+/', ' ', $key);
if (isset($this->references[$key]) || isset($this->references[$key = $normalizedKey])) {
return [
$text,
$this->references[$key]['url'], // url
empty($this->references[$key]['title']) ? null: $this->references[$key]['title'], // title
$offset + strlen($refMatches[0]), // offset
$key,
];
}
}
}
return false;
}
/**
* Parses an inline code span `` ` ``.
*/
protected function parseCode($text)
{
if (preg_match('/^(``+)\s(.+?)\s\1/s', $text, $matches)) { // code with enclosed backtick
return [
'<code>' . htmlspecialchars($matches[2], ENT_NOQUOTES, 'UTF-8') . '</code>',
strlen($matches[0])
];
} elseif (preg_match('/^`(.+?)`/s', $text, $matches)) {
return [
'<code>' . htmlspecialchars($matches[1], ENT_NOQUOTES, 'UTF-8') . '</code>',
strlen($matches[0])
];
}
return [$text[0], 1];
}
/**
* Parses empathized and strong elements.
*/
protected function parseEmphStrong($text)
{
$marker = $text[0];
if (!isset($text[1])) {
return [$text[0], 1];
}
if ($marker == $text[1]) { // strong
if ($marker == '*' && preg_match('/^[*]{2}((?:[^*]|[*][^*]*[*])+?)[*]{2}(?![*])/s', $text, $matches) ||
$marker == '_' && preg_match('/^__((?:[^_]|_[^_]*_)+?)__(?!_)/us', $text, $matches)) {
return ['<strong>' . $this->parseInline($matches[1]) . '</strong>', strlen($matches[0])];
}
} else { // emph
if ($marker == '*' && preg_match('/^[*]((?:[^*]|[*][*][^*]+?[*][*])+?)[*](?![*])/s', $text, $matches) ||
$marker == '_' && preg_match('/^_((?:[^_]|__[^_]*__)+?)_(?!_)\b/us', $text, $matches)) {
return ['<em>' . $this->parseInline($matches[1]) . '</em>', strlen($matches[0])];
}
}
return [$text[0], 1];
}
/**
* Return html attributes string from [attrName => attrValue] list
* @param array $attributes the attribute name-value pairs.
* @return string
*/
private function generateHtmlAttributes($attributes)
{
foreach ($attributes as $name => $value) {
$attributes[$name] = "$name=\"$value\"";
}
return implode(' ', $attributes);
}
}