Merge pull request #944 from nupplaphil/task/cleanup_cache
[friendica-addons.git/.git] / markdown / markdown.php
1 <?php
2 /**
3  * Name: Markdown
4  * Description: Parse Markdown code when creating new items
5  * Version: 0.1
6  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
7  */
8 use Friendica\App;
9 use Friendica\Core\Hook;
10 use Friendica\Core\Logger;
11 use Friendica\Content\Text\Markdown;
12 use Friendica\Core\Renderer;
13 use Friendica\Core\PConfig;
14 use Friendica\Core\L10n;
15
16 function markdown_install() {
17         Hook::register('post_local_start',      __FILE__, 'markdown_post_local_start');
18         Hook::register('addon_settings',        __FILE__, 'markdown_addon_settings');
19         Hook::register('addon_settings_post',   __FILE__, 'markdown_addon_settings_post');
20 }
21
22 function markdown_addon_settings(App $a, &$s)
23 {
24         if (!local_user()) {
25                 return;
26         }
27
28         $enabled = intval(PConfig::get(local_user(), 'markdown', 'enabled'));
29
30         $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/markdown/');
31         $s .= Renderer::replaceMacros($t, [
32                 '$title'   => L10n::t('Markdown'),
33                 '$enabled' => ['enabled', L10n::t('Enable Markdown parsing'), $enabled, L10n::t('If enabled, self created items will additionally be parsed via Markdown.')],
34                 '$submit'  => L10n::t('Save Settings'),
35         ]);
36 }
37
38 function markdown_addon_settings_post(App $a, &$b)
39 {
40         if (!local_user() || empty($_POST['markdown-submit'])) {
41                 return;
42         }
43
44         PConfig::set(local_user(), 'markdown', 'enabled', intval($_POST['enabled']));
45 }
46
47 function markdown_post_local_start(App $a, &$request) {
48         if (empty($request['body']) || !PConfig::get(local_user(), 'markdown', 'enabled')) {
49                 return;
50         }
51
52         // Elements that shouldn't be parsed
53         $elements = ['code', 'noparse', 'nobb', 'pre', 'share', 'url', 'img', 'bookmark',
54                 'audio', 'video', 'youtube', 'vimeo', 'attachment', 'iframe', 'map', 'mail'];
55         foreach ($elements as $element) {
56                 $request['body'] = preg_replace_callback("/\[" . $element . "(.*?)\](.*?)\[\/" . $element . "\]/ism",
57                         function ($match) use ($element) {
58                                 return '[' . $element . '-b64' . base64_encode($match[1]) . ']' . base64_encode($match[2]) . '[/b64-' . $element . ']';
59                         },
60                         $request['body']
61                 );
62         }
63
64         $request['body'] = Markdown::toBBCode($request['body']);
65
66         foreach (array_reverse($elements) as $element) {
67                 $request['body'] = preg_replace_callback("/\[" . $element . "-b64(.*?)\](.*?)\[\/b64-" . $element . "\]/ism",
68                         function ($match) use ($element) {
69                                 return '[' . $element . base64_decode($match[1]) . ']' . base64_decode($match[2]) . '[/' . $element . ']';
70                         },
71                         $request['body']
72                 );
73         }
74 }