]>
Commit | Line | Data |
---|---|---|
1be4afac A |
1 | <?php |
2 | ||
3 | /** | |
4 | * Plugin Markdown. | |
5 | * | |
6 | * Shaare's descriptions are parsed with Markdown. | |
7 | */ | |
8 | ||
3ce20d9e A |
9 | /* |
10 | * If this tag is used on a shaare, the description won't be processed by Parsedown. | |
3ce20d9e | 11 | */ |
8c4e6018 | 12 | define('NO_MD_TAG', 'nomarkdown'); |
3ce20d9e | 13 | |
1be4afac A |
14 | /** |
15 | * Parse linklist descriptions. | |
16 | * | |
17 | * @param array $data linklist data. | |
18 | * | |
19 | * @return mixed linklist data parsed in markdown (and converted to HTML). | |
20 | */ | |
21 | function hook_markdown_render_linklist($data) | |
22 | { | |
23 | foreach ($data['links'] as &$value) { | |
3ce20d9e | 24 | if (!empty($value['tags']) && noMarkdownTag($value['tags'])) { |
266e3fe5 | 25 | $value = stripNoMarkdownTag($value); |
3ce20d9e A |
26 | continue; |
27 | } | |
1be4afac A |
28 | $value['description'] = process_markdown($value['description']); |
29 | } | |
1be4afac A |
30 | return $data; |
31 | } | |
32 | ||
635d38c2 A |
33 | /** |
34 | * Parse feed linklist descriptions. | |
35 | * | |
36 | * @param array $data linklist data. | |
37 | * | |
38 | * @return mixed linklist data parsed in markdown (and converted to HTML). | |
39 | */ | |
40 | function hook_markdown_render_feed($data) | |
41 | { | |
42 | foreach ($data['links'] as &$value) { | |
43 | if (!empty($value['tags']) && noMarkdownTag($value['tags'])) { | |
266e3fe5 | 44 | $value = stripNoMarkdownTag($value); |
635d38c2 A |
45 | continue; |
46 | } | |
47 | $value['description'] = process_markdown($value['description']); | |
48 | } | |
49 | ||
50 | return $data; | |
51 | } | |
52 | ||
1be4afac A |
53 | /** |
54 | * Parse daily descriptions. | |
55 | * | |
56 | * @param array $data daily data. | |
57 | * | |
58 | * @return mixed daily data parsed in markdown (and converted to HTML). | |
59 | */ | |
60 | function hook_markdown_render_daily($data) | |
61 | { | |
62 | // Manipulate columns data | |
63 | foreach ($data['cols'] as &$value) { | |
64 | foreach ($value as &$value2) { | |
3ce20d9e | 65 | if (!empty($value2['tags']) && noMarkdownTag($value2['tags'])) { |
266e3fe5 | 66 | $value2 = stripNoMarkdownTag($value2); |
3ce20d9e A |
67 | continue; |
68 | } | |
1be4afac A |
69 | $value2['formatedDescription'] = process_markdown($value2['formatedDescription']); |
70 | } | |
71 | } | |
72 | ||
73 | return $data; | |
74 | } | |
75 | ||
3ce20d9e A |
76 | /** |
77 | * Check if noMarkdown is set in tags. | |
78 | * | |
79 | * @param string $tags tag list | |
80 | * | |
81 | * @return bool true if markdown should be disabled on this link. | |
82 | */ | |
83 | function noMarkdownTag($tags) | |
84 | { | |
266e3fe5 | 85 | return preg_match('/(^|\s)'. NO_MD_TAG .'(\s|$)/', $tags); |
3ce20d9e A |
86 | } |
87 | ||
8c4e6018 A |
88 | /** |
89 | * Remove the no-markdown meta tag so it won't be displayed. | |
90 | * | |
266e3fe5 | 91 | * @param array $link Link data. |
8c4e6018 | 92 | * |
266e3fe5 | 93 | * @return array Updated link without no markdown tag. |
8c4e6018 | 94 | */ |
266e3fe5 | 95 | function stripNoMarkdownTag($link) |
8c4e6018 | 96 | { |
266e3fe5 A |
97 | if (! empty($link['taglist'])) { |
98 | $offset = array_search(NO_MD_TAG, $link['taglist']); | |
99 | if ($offset !== false) { | |
100 | unset($link['taglist'][$offset]); | |
101 | } | |
102 | } | |
103 | ||
104 | if (!empty($link['tags'])) { | |
105 | str_replace(NO_MD_TAG, '', $link['tags']); | |
106 | } | |
107 | ||
108 | return $link; | |
8c4e6018 A |
109 | } |
110 | ||
1be4afac A |
111 | /** |
112 | * When link list is displayed, include markdown CSS. | |
113 | * | |
114 | * @param array $data includes data. | |
115 | * | |
116 | * @return mixed - includes data with markdown CSS file added. | |
117 | */ | |
118 | function hook_markdown_render_includes($data) | |
119 | { | |
120 | if ($data['_PAGE_'] == Router::$PAGE_LINKLIST | |
121 | || $data['_PAGE_'] == Router::$PAGE_DAILY | |
122 | || $data['_PAGE_'] == Router::$PAGE_EDITLINK | |
123 | ) { | |
124 | ||
125 | $data['css_files'][] = PluginManager::$PLUGINS_PATH . '/markdown/markdown.css'; | |
126 | } | |
127 | ||
128 | return $data; | |
129 | } | |
130 | ||
131 | /** | |
132 | * Hook render_editlink. | |
133 | * Adds an help link to markdown syntax. | |
134 | * | |
135 | * @param array $data data passed to plugin | |
136 | * | |
137 | * @return array altered $data. | |
138 | */ | |
139 | function hook_markdown_render_editlink($data) | |
140 | { | |
141 | // Load help HTML into a string | |
142 | $data['edit_link_plugin'][] = file_get_contents(PluginManager::$PLUGINS_PATH .'/markdown/help.html'); | |
3ce20d9e A |
143 | |
144 | // Add no markdown 'meta-tag' in tag list if it was never used, for autocompletion. | |
145 | if (! in_array(NO_MD_TAG, $data['tags'])) { | |
146 | $data['tags'][NO_MD_TAG] = 0; | |
147 | } | |
148 | ||
1be4afac A |
149 | return $data; |
150 | } | |
151 | ||
152 | ||
153 | /** | |
154 | * Remove HTML links auto generated by Shaarli core system. | |
155 | * Keeps HREF attributes. | |
156 | * | |
157 | * @param string $description input description text. | |
158 | * | |
159 | * @return string $description without HTML links. | |
160 | */ | |
161 | function reverse_text2clickable($description) | |
162 | { | |
9ccca401 A |
163 | $descriptionLines = explode(PHP_EOL, $description); |
164 | $descriptionOut = ''; | |
165 | $codeBlockOn = false; | |
166 | $lineCount = 0; | |
167 | ||
168 | foreach ($descriptionLines as $descriptionLine) { | |
c5941f31 A |
169 | // Detect line of code: starting with 4 spaces, |
170 | // except lists which can start with +/*/- or `2.` after spaces. | |
171 | $codeLineOn = preg_match('/^ +(?=[^\+\*\-])(?=(?!\d\.).)/', $descriptionLine) > 0; | |
9ccca401 A |
172 | // Detect and toggle block of code |
173 | if (!$codeBlockOn) { | |
174 | $codeBlockOn = preg_match('/^```/', $descriptionLine) > 0; | |
175 | } | |
176 | elseif (preg_match('/^```/', $descriptionLine) > 0) { | |
177 | $codeBlockOn = false; | |
178 | } | |
179 | ||
180 | $hashtagTitle = ' title="Hashtag [^"]+"'; | |
181 | // Reverse `inline code` hashtags. | |
182 | $descriptionLine = preg_replace( | |
183 | '!(`[^`\n]*)<a href="[^ ]*"'. $hashtagTitle .'>([^<]+)</a>([^`\n]*`)!m', | |
184 | '$1$2$3', | |
185 | $descriptionLine | |
186 | ); | |
187 | ||
c5941f31 A |
188 | // Reverse all links in code blocks, only non hashtag elsewhere. |
189 | $hashtagFilter = (!$codeBlockOn && !$codeLineOn) ? '(?!'. $hashtagTitle .')': '(?:'. $hashtagTitle .')?'; | |
9ccca401 | 190 | $descriptionLine = preg_replace( |
c5941f31 | 191 | '#<a href="[^ ]*"'. $hashtagFilter .'>([^<]+)</a>#m', |
9ccca401 A |
192 | '$1', |
193 | $descriptionLine | |
194 | ); | |
195 | ||
196 | $descriptionOut .= $descriptionLine; | |
197 | if ($lineCount++ < count($descriptionLines) - 1) { | |
198 | $descriptionOut .= PHP_EOL; | |
199 | } | |
200 | } | |
201 | return $descriptionOut; | |
1be4afac A |
202 | } |
203 | ||
204 | /** | |
205 | * Remove <br> tag to let markdown handle it. | |
206 | * | |
207 | * @param string $description input description text. | |
208 | * | |
209 | * @return string $description without <br> tags. | |
210 | */ | |
211 | function reverse_nl2br($description) | |
212 | { | |
213 | return preg_replace('!<br */?>!im', '', $description); | |
214 | } | |
215 | ||
216 | /** | |
217 | * Remove HTML spaces ' ' auto generated by Shaarli core system. | |
218 | * | |
219 | * @param string $description input description text. | |
220 | * | |
221 | * @return string $description without HTML links. | |
222 | */ | |
223 | function reverse_space2nbsp($description) | |
224 | { | |
225 | return preg_replace('/(^| ) /m', '$1 ', $description); | |
226 | } | |
227 | ||
228 | /** | |
2925687e A |
229 | * Remove dangerous HTML tags (tags, iframe, etc.). |
230 | * Doesn't affect <code> content (already escaped by Parsedown). | |
1be4afac A |
231 | * |
232 | * @param string $description input description text. | |
233 | * | |
2925687e | 234 | * @return string given string escaped. |
1be4afac | 235 | */ |
2925687e | 236 | function sanitize_html($description) |
1be4afac | 237 | { |
2925687e A |
238 | $escapeTags = array( |
239 | 'script', | |
240 | 'style', | |
241 | 'link', | |
242 | 'iframe', | |
243 | 'frameset', | |
244 | 'frame', | |
245 | ); | |
246 | foreach ($escapeTags as $tag) { | |
247 | $description = preg_replace_callback( | |
248 | '#<\s*'. $tag .'[^>]*>(.*</\s*'. $tag .'[^>]*>)?#is', | |
249 | function ($match) { return escape($match[0]); }, | |
250 | $description); | |
251 | } | |
252 | $description = preg_replace( | |
253 | '#(<[^>]+)on[a-z]*="[^"]*"#is', | |
254 | '$1', | |
255 | $description); | |
256 | return $description; | |
1be4afac A |
257 | } |
258 | ||
259 | /** | |
260 | * Render shaare contents through Markdown parser. | |
261 | * 1. Remove HTML generated by Shaarli core. | |
2925687e A |
262 | * 2. Reverse the escape function. |
263 | * 3. Generate markdown descriptions. | |
264 | * 4. Sanitize sensible HTML tags for security. | |
265 | * 5. Wrap description in 'markdown' CSS class. | |
1be4afac A |
266 | * |
267 | * @param string $description input description text. | |
268 | * | |
269 | * @return string HTML processed $description. | |
270 | */ | |
271 | function process_markdown($description) | |
272 | { | |
273 | $parsedown = new Parsedown(); | |
274 | ||
275 | $processedDescription = $description; | |
1be4afac A |
276 | $processedDescription = reverse_nl2br($processedDescription); |
277 | $processedDescription = reverse_space2nbsp($processedDescription); | |
9ccca401 | 278 | $processedDescription = reverse_text2clickable($processedDescription); |
2925687e | 279 | $processedDescription = unescape($processedDescription); |
1be4afac A |
280 | $processedDescription = $parsedown |
281 | ->setMarkupEscaped(false) | |
282 | ->setBreaksEnabled(true) | |
283 | ->text($processedDescription); | |
2925687e | 284 | $processedDescription = sanitize_html($processedDescription); |
841df2dd | 285 | |
286 | if(!empty($processedDescription)){ | |
287 | $processedDescription = '<div class="markdown">'. $processedDescription . '</div>'; | |
288 | } | |
1be4afac A |
289 | |
290 | return $processedDescription; | |
291 | } |