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