]>
Commit | Line | Data |
---|---|---|
1be4afac A |
1 | <?php |
2 | ||
3 | /** | |
4 | * Plugin Markdown. | |
5 | * | |
6 | * Shaare's descriptions are parsed with Markdown. | |
7 | */ | |
8 | ||
9 | require_once 'Parsedown.php'; | |
10 | ||
3ce20d9e A |
11 | /* |
12 | * If this tag is used on a shaare, the description won't be processed by Parsedown. | |
13 | * Using a private tag so it won't appear for visitors. | |
14 | */ | |
15 | define('NO_MD_TAG', '.nomarkdown'); | |
16 | ||
1be4afac A |
17 | /** |
18 | * Parse linklist descriptions. | |
19 | * | |
20 | * @param array $data linklist data. | |
21 | * | |
22 | * @return mixed linklist data parsed in markdown (and converted to HTML). | |
23 | */ | |
24 | function hook_markdown_render_linklist($data) | |
25 | { | |
26 | foreach ($data['links'] as &$value) { | |
3ce20d9e A |
27 | if (!empty($value['tags']) && noMarkdownTag($value['tags'])) { |
28 | continue; | |
29 | } | |
1be4afac A |
30 | $value['description'] = process_markdown($value['description']); |
31 | } | |
32 | ||
33 | return $data; | |
34 | } | |
35 | ||
635d38c2 A |
36 | /** |
37 | * Parse feed linklist descriptions. | |
38 | * | |
39 | * @param array $data linklist data. | |
40 | * | |
41 | * @return mixed linklist data parsed in markdown (and converted to HTML). | |
42 | */ | |
43 | function hook_markdown_render_feed($data) | |
44 | { | |
45 | foreach ($data['links'] as &$value) { | |
46 | if (!empty($value['tags']) && noMarkdownTag($value['tags'])) { | |
47 | continue; | |
48 | } | |
49 | $value['description'] = process_markdown($value['description']); | |
50 | } | |
51 | ||
52 | return $data; | |
53 | } | |
54 | ||
1be4afac A |
55 | /** |
56 | * Parse daily descriptions. | |
57 | * | |
58 | * @param array $data daily data. | |
59 | * | |
60 | * @return mixed daily data parsed in markdown (and converted to HTML). | |
61 | */ | |
62 | function hook_markdown_render_daily($data) | |
63 | { | |
64 | // Manipulate columns data | |
65 | foreach ($data['cols'] as &$value) { | |
66 | foreach ($value as &$value2) { | |
3ce20d9e A |
67 | if (!empty($value2['tags']) && noMarkdownTag($value2['tags'])) { |
68 | continue; | |
69 | } | |
1be4afac A |
70 | $value2['formatedDescription'] = process_markdown($value2['formatedDescription']); |
71 | } | |
72 | } | |
73 | ||
74 | return $data; | |
75 | } | |
76 | ||
3ce20d9e A |
77 | /** |
78 | * Check if noMarkdown is set in tags. | |
79 | * | |
80 | * @param string $tags tag list | |
81 | * | |
82 | * @return bool true if markdown should be disabled on this link. | |
83 | */ | |
84 | function noMarkdownTag($tags) | |
85 | { | |
86 | return strpos($tags, NO_MD_TAG) !== false; | |
87 | } | |
88 | ||
1be4afac A |
89 | /** |
90 | * When link list is displayed, include markdown CSS. | |
91 | * | |
92 | * @param array $data includes data. | |
93 | * | |
94 | * @return mixed - includes data with markdown CSS file added. | |
95 | */ | |
96 | function hook_markdown_render_includes($data) | |
97 | { | |
98 | if ($data['_PAGE_'] == Router::$PAGE_LINKLIST | |
99 | || $data['_PAGE_'] == Router::$PAGE_DAILY | |
100 | || $data['_PAGE_'] == Router::$PAGE_EDITLINK | |
101 | ) { | |
102 | ||
103 | $data['css_files'][] = PluginManager::$PLUGINS_PATH . '/markdown/markdown.css'; | |
104 | } | |
105 | ||
106 | return $data; | |
107 | } | |
108 | ||
109 | /** | |
110 | * Hook render_editlink. | |
111 | * Adds an help link to markdown syntax. | |
112 | * | |
113 | * @param array $data data passed to plugin | |
114 | * | |
115 | * @return array altered $data. | |
116 | */ | |
117 | function hook_markdown_render_editlink($data) | |
118 | { | |
119 | // Load help HTML into a string | |
120 | $data['edit_link_plugin'][] = file_get_contents(PluginManager::$PLUGINS_PATH .'/markdown/help.html'); | |
3ce20d9e A |
121 | |
122 | // Add no markdown 'meta-tag' in tag list if it was never used, for autocompletion. | |
123 | if (! in_array(NO_MD_TAG, $data['tags'])) { | |
124 | $data['tags'][NO_MD_TAG] = 0; | |
125 | } | |
126 | ||
1be4afac A |
127 | return $data; |
128 | } | |
129 | ||
130 | ||
131 | /** | |
132 | * Remove HTML links auto generated by Shaarli core system. | |
133 | * Keeps HREF attributes. | |
134 | * | |
135 | * @param string $description input description text. | |
136 | * | |
137 | * @return string $description without HTML links. | |
138 | */ | |
139 | function reverse_text2clickable($description) | |
140 | { | |
141 | return preg_replace('!<a +href="([^ ]*)">[^ ]+</a>!m', '$1', $description); | |
142 | } | |
143 | ||
144 | /** | |
145 | * Remove <br> tag to let markdown handle it. | |
146 | * | |
147 | * @param string $description input description text. | |
148 | * | |
149 | * @return string $description without <br> tags. | |
150 | */ | |
151 | function reverse_nl2br($description) | |
152 | { | |
153 | return preg_replace('!<br */?>!im', '', $description); | |
154 | } | |
155 | ||
156 | /** | |
157 | * Remove HTML spaces ' ' auto generated by Shaarli core system. | |
158 | * | |
159 | * @param string $description input description text. | |
160 | * | |
161 | * @return string $description without HTML links. | |
162 | */ | |
163 | function reverse_space2nbsp($description) | |
164 | { | |
165 | return preg_replace('/(^| ) /m', '$1 ', $description); | |
166 | } | |
167 | ||
168 | /** | |
2925687e A |
169 | * Remove dangerous HTML tags (tags, iframe, etc.). |
170 | * Doesn't affect <code> content (already escaped by Parsedown). | |
1be4afac A |
171 | * |
172 | * @param string $description input description text. | |
173 | * | |
2925687e | 174 | * @return string given string escaped. |
1be4afac | 175 | */ |
2925687e | 176 | function sanitize_html($description) |
1be4afac | 177 | { |
2925687e A |
178 | $escapeTags = array( |
179 | 'script', | |
180 | 'style', | |
181 | 'link', | |
182 | 'iframe', | |
183 | 'frameset', | |
184 | 'frame', | |
185 | ); | |
186 | foreach ($escapeTags as $tag) { | |
187 | $description = preg_replace_callback( | |
188 | '#<\s*'. $tag .'[^>]*>(.*</\s*'. $tag .'[^>]*>)?#is', | |
189 | function ($match) { return escape($match[0]); }, | |
190 | $description); | |
191 | } | |
192 | $description = preg_replace( | |
193 | '#(<[^>]+)on[a-z]*="[^"]*"#is', | |
194 | '$1', | |
195 | $description); | |
196 | return $description; | |
1be4afac A |
197 | } |
198 | ||
199 | /** | |
200 | * Render shaare contents through Markdown parser. | |
201 | * 1. Remove HTML generated by Shaarli core. | |
2925687e A |
202 | * 2. Reverse the escape function. |
203 | * 3. Generate markdown descriptions. | |
204 | * 4. Sanitize sensible HTML tags for security. | |
205 | * 5. Wrap description in 'markdown' CSS class. | |
1be4afac A |
206 | * |
207 | * @param string $description input description text. | |
208 | * | |
209 | * @return string HTML processed $description. | |
210 | */ | |
211 | function process_markdown($description) | |
212 | { | |
213 | $parsedown = new Parsedown(); | |
214 | ||
215 | $processedDescription = $description; | |
216 | $processedDescription = reverse_text2clickable($processedDescription); | |
217 | $processedDescription = reverse_nl2br($processedDescription); | |
218 | $processedDescription = reverse_space2nbsp($processedDescription); | |
2925687e | 219 | $processedDescription = unescape($processedDescription); |
1be4afac A |
220 | $processedDescription = $parsedown |
221 | ->setMarkupEscaped(false) | |
222 | ->setBreaksEnabled(true) | |
223 | ->text($processedDescription); | |
2925687e | 224 | $processedDescription = sanitize_html($processedDescription); |
841df2dd | 225 | |
226 | if(!empty($processedDescription)){ | |
227 | $processedDescription = '<div class="markdown">'. $processedDescription . '</div>'; | |
228 | } | |
1be4afac A |
229 | |
230 | return $processedDescription; | |
231 | } |