1 | <?php |
||
7 | class ParagraphConverter implements ConverterInterface |
||
8 | { |
||
9 | /** |
||
10 | * @param ElementInterface $element |
||
11 | * |
||
12 | * @return string |
||
13 | */ |
||
14 | 48 | public function convert(ElementInterface $element) |
|
15 | { |
||
16 | 48 | $value = $element->getValue(); |
|
17 | |||
18 | 48 | $markdown = ''; |
|
19 | |||
20 | 48 | $lines = preg_split('/\r\n|\r|\n/', $value); |
|
21 | 48 | foreach ($lines as $line) { |
|
22 | /* |
||
23 | * Some special characters need to be escaped based on the position that they appear |
||
24 | * The following function will deal with those special cases. |
||
25 | */ |
||
26 | 48 | $markdown .= $this->escapeSpecialCharacters($line); |
|
27 | 48 | $markdown .= "\n"; |
|
28 | } |
||
29 | |||
30 | 48 | return trim($markdown) !== '' ? rtrim($markdown) . "\n\n" : ''; |
|
31 | } |
||
32 | |||
33 | /** |
||
34 | * @return string[] |
||
35 | */ |
||
36 | 93 | public function getSupportedTags() |
|
40 | |||
41 | /** |
||
42 | * @param string $line |
||
43 | * |
||
44 | * @return string |
||
45 | */ |
||
46 | 48 | private function escapeSpecialCharacters($line) |
|
54 | |||
55 | /** |
||
56 | * @param string $line |
||
57 | * |
||
58 | * @return string |
||
59 | */ |
||
60 | 48 | private function escapeFirstCharacters($line) |
|
81 | |||
82 | /** |
||
83 | * @param string $line |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | 48 | private function escapeOtherCharacters($line) |
|
88 | { |
||
89 | $escapable = array( |
||
90 | 48 | '<!--' |
|
91 | ); |
||
92 | |||
93 | 48 | foreach ($escapable as $i) { |
|
94 | 48 | if (strpos($line, $i) !== false) { |
|
95 | // Found an escapable character, escaping it |
||
96 | $line = substr_replace($line, '\\', strpos($line, $i), 0); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | 48 | return $line; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param string $line |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | 48 | private function escapeOtherCharactersRegex($line) |
|
125 | } |
||
126 |