PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / libraries / markdown / inline / EmphStrongTrait.php

EmphStrongTrait.php in DecaLog 4.4.0, at includes/libraries/markdown/inline/EmphStrongTrait.php

88 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @copyright Copyright (c) 2014 Carsten Brandt
4 * @license https://github.com/cebe/markdown/blob/master/LICENSE
5 * @link https://github.com/cebe/markdown#readme
6 */
7
8 namespace cebe\markdownparser\inline;
9
10 /**
11 * Adds inline emphasizes and strong elements
12 */
13 trait EmphStrongTrait
14 {
15 /**
16 * Parses emphasized and strong elements.
17 * @marker _
18 * @marker *
19 */
20 protected function parseEmphStrong($text)
21 {
22 $marker = $text[0];
23
24 if (!isset($text[1])) {
25 return [['text', $text[0]], 1];
26 }
27
28 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 if (strpos($text, $marker . $marker, 2) === false) {
34 return [['text', $text[0] . $text[1]], 2];
35 }
36
37 if ($marker === '*' && preg_match('/^[*]{2}((?>\\\\[*]|[^*]|[*][^*]*[*])+?)[*]{2}/s', $text, $matches) ||
38 $marker === '_' && preg_match('/^__((?>\\\\_|[^_]|_[^_]*_)+?)__/us', $text, $matches)) {
39
40 return [
41 [
42 'strong',
43 $this->parseInline($matches[1]),
44 ],
45 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 if (strpos($text, $marker, 1) === false) {
54 return [['text', $text[0]], 1];
55 }
56
57 if ($marker === '*' && preg_match('/^[*]((?>\\\\[*]|[^*]|[*][*][^*]+?[*][*])+?)[*](?![*][^*])/s', $text, $matches) ||
58 $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 if ($matches[1] === '' || $matches[1] === ' ') {
61 return [['text', $text[0]], 1];
62 }
63 return [
64 [
65 'emph',
66 $this->parseInline($matches[1]),
67 ],
68 strlen($matches[0])
69 ];
70 }
71 }
72 return [['text', $text[0]], 1];
73 }
74
75 protected function renderStrong($block)
76 {
77 return '<strong>' . $this->renderAbsy($block[1]) . '</strong>';
78 }
79
80 protected function renderEmph($block)
81 {
82 return '<em>' . $this->renderAbsy($block[1]) . '</em>';
83 }
84
85 abstract protected function parseInline($text);
86 abstract protected function renderAbsy($blocks);
87 }
88