Merge pull request #976 from annando/tags
[friendica-addons.git/.git] / mathjax / mathjax.php
1 <?php
2 /**
3  * Name: MathJax
4  * Description: Addon for Friendica to include MathJax (LaTeX math syntax)
5  * Version: 2.0
6  * Author: Tobias Diekershoff <https://social.diekershoff.de/profile/tobias>
7  * Author: Hypolite Petovan <https://friendica.mrpetovan.com/profile/hypolite>
8  * License: 3-clause BSD license
9  */
10
11 use Friendica\App;
12 use Friendica\Core\Hook;
13 use Friendica\Core\Renderer;
14 use Friendica\DI;
15
16 function mathjax_install()
17 {
18         Hook::register('footer'             , __FILE__, 'mathjax_footer');
19         Hook::register('addon_settings'     , __FILE__, 'mathjax_settings');
20         Hook::register('addon_settings_post', __FILE__, 'mathjax_settings_post');
21 }
22
23 function mathjax_uninstall()
24 {
25         Hook::unregister('footer'             , __FILE__, 'mathjax_footer');
26         Hook::unregister('addon_settings'     , __FILE__, 'mathjax_settings');
27         Hook::unregister('addon_settings_post', __FILE__, 'mathjax_settings_post');
28
29         // Legacy hooks
30         Hook::unregister('load_config'        , __FILE__, 'mathjax_load_config');
31         Hook::unregister('page_header'        , __FILE__, 'mathjax_page_header');
32         Hook::unregister('template_vars'      , __FILE__, 'mathjax_template_vars');
33 }
34
35 function mathjax_settings_post($a)
36 {
37         if (!local_user() || empty($_POST['mathjax-submit'])) {
38                 return;
39         }
40
41         DI::pConfig()->set(local_user(), 'mathjax', 'use', intval($_POST['mathjax_use']));
42 }
43
44 function mathjax_settings(App $a, &$s)
45 {
46         if (!local_user()) {
47                 return;
48         }
49
50         $use = DI::pConfig()->get(local_user(), 'mathjax', 'use', false);
51
52         $tpl = Renderer::getMarkupTemplate('settings.tpl', __DIR__);
53         $s .= Renderer::replaceMacros($tpl, [
54                 '$title'        => 'MathJax',
55                 '$description'  => DI::l10n()->t('The MathJax addon renders mathematical formulae written using the LaTeX syntax surrounded by the usual $$ or an eqnarray block in the postings of your wall,network tab and private mail.'),
56                 '$mathjax_use'  => ['mathjax_use', DI::l10n()->t('Use the MathJax renderer'), $use, ''],
57                 '$savesettings' => DI::l10n()->t('Save Settings'),
58         ]);
59 }
60
61 function mathjax_footer(App $a, &$b)
62 {
63         //  if the visitor of the page is not a local_user, use MathJax
64         //  otherwise check the users settings.
65         if (!local_user() || DI::pConfig()->get(local_user(), 'mathjax', 'use', false)) {
66                 DI::page()->registerFooterScript(__DIR__ . '/asset/MathJax.js?config=TeX-MML-AM_CHTML');
67                 DI::page()->registerFooterScript(__DIR__ . '/mathjax.js');
68         }
69 }