1 | <?php |
||
13 | trait EmphStrongTrait |
||
14 | { |
||
15 | /** |
||
16 | * Parses emphasized and strong elements. |
||
17 | * @marker _ |
||
18 | * @marker * |
||
19 | */ |
||
20 | 34 | protected function parseEmphStrong($text) |
|
21 | { |
||
22 | 34 | $marker = $text[0]; |
|
23 | |||
24 | 34 | if (!isset($text[1])) { |
|
25 | 3 | return [['text', $text[0]], 1]; |
|
26 | } |
||
27 | |||
28 | 34 | if ($marker == $text[1]) { // strong |
|
29 | // work around a PHP bug that crashes with a segfault on too much regex backtrack |
||
30 | // check whether the end marker exists in the text |
||
31 | // https://github.com/erusev/parsedown/issues/443 |
||
32 | // https://bugs.php.net/bug.php?id=45735 |
||
33 | 27 | if (strpos($text, $marker . $marker, 2) === false) { |
|
34 | 9 | return [['text', $text[0] . $text[1]], 2]; |
|
35 | } |
||
36 | |||
37 | 21 | if ($marker === '*' && preg_match('/^[*]{2}((?>\\\\[*]|[^*]|[*][^*]*[*])+?)[*]{2}/s', $text, $matches) || |
|
38 | 21 | $marker === '_' && preg_match('/^__((?>\\\\_|[^_]|_[^_]*_)+?)__/us', $text, $matches)) { |
|
39 | |||
40 | return [ |
||
41 | [ |
||
42 | 21 | 'strong', |
|
43 | 21 | $this->parseInline($matches[1]), |
|
44 | ], |
||
45 | 21 | strlen($matches[0]) |
|
46 | ]; |
||
47 | } |
||
48 | } else { // emph |
||
49 | // work around a PHP bug that crashes with a segfault on too much regex backtrack |
||
50 | // check whether the end marker exists in the text |
||
51 | // https://github.com/erusev/parsedown/issues/443 |
||
52 | // https://bugs.php.net/bug.php?id=45735 |
||
53 | 22 | if (strpos($text, $marker, 1) === false) { |
|
54 | 11 | return [['text', $text[0]], 1]; |
|
55 | } |
||
56 | |||
57 | 17 | if ($marker === '*' && preg_match('/^[*]((?>\\\\[*]|[^*]|[*][*][^*]+?[*][*])+?)[*](?![*][^*])/s', $text, $matches) || |
|
58 | 17 | $marker === '_' && preg_match('/^_((?>\\\\_|[^_]|__[^_]*__)+?)_(?!_[^_])\b/us', $text, $matches)) { |
|
59 | // if only a single whitespace or nothing is contained in an emphasis, do not consider it valid |
||
60 | 16 | if ($matches[1] === '' || $matches[1] === ' ') { |
|
61 | 4 | return [['text', $text[0]], 1]; |
|
62 | } |
||
63 | return [ |
||
64 | [ |
||
65 | 15 | 'emph', |
|
66 | 15 | $this->parseInline($matches[1]), |
|
67 | ], |
||
68 | 15 | strlen($matches[0]) |
|
69 | ]; |
||
70 | } |
||
71 | } |
||
72 | 5 | return [['text', $text[0]], 1]; |
|
73 | } |
||
74 | |||
75 | 21 | protected function renderStrong($block) |
|
76 | { |
||
77 | 21 | return '<strong>' . $this->renderAbsy($block[1]) . '</strong>'; |
|
78 | } |
||
79 | |||
80 | 15 | protected function renderEmph($block) |
|
81 | { |
||
82 | 15 | return '<em>' . $this->renderAbsy($block[1]) . '</em>'; |
|
83 | } |
||
84 | |||
85 | abstract protected function parseInline($text); |
||
87 | } |
||
88 |