8564890eb3e6fc33957e0ddc56e5fe59642b722f
[friendica-addons.git/.git] / mathjax / mathjax.php
1 <?php
2 /**
3  * Name: MathJax
4  * Description: Addon for Friendika to include MathJax (LaTeX math syntax)
5  * Version: 2
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\Addon;
13 use Friendica\Core\L10n;
14 use Friendica\Core\PConfig;
15
16 function mathjax_install()
17 {
18         Addon::registerHook('page_end'           , __FILE__, 'mathjax_page_end');
19         Addon::registerHook('addon_settings'     , __FILE__, 'mathjax_settings');
20         Addon::registerHook('addon_settings_post', __FILE__, 'mathjax_settings_post');
21 }
22
23 function mathjax_uninstall()
24 {
25         Addon::unregisterHook('page_end'           , __FILE__, 'mathjax_page_end');
26         Addon::unregisterHook('addon_settings'     , __FILE__, 'mathjax_settings');
27         Addon::unregisterHook('addon_settings_post', __FILE__, 'mathjax_settings_post');
28
29         // Legacy hooks
30         Addon::unregisterHook('load_config'        , __FILE__, 'mathjax_load_config');
31         Addon::unregisterHook('page_header'        , __FILE__, 'mathjax_page_header');
32         Addon::unregisterHook('template_vars'      , __FILE__, 'mathjax_template_vars');
33 }
34
35 function mathjax_settings_post($a)
36 {
37         if (!local_user()) {
38                 return;
39         }
40
41         if (empty($_POST['mathjax-submit'])) {
42                 return;
43         }
44
45         PConfig::set(local_user(), 'mathjax', 'use', intval($_POST['mathjax_use']));
46 }
47
48 function mathjax_settings(App $a, &$s)
49 {
50         if (!local_user()) {
51                 return;
52         }
53
54         $use = PConfig::get(local_user(), 'mathjax', 'use', false);
55         $usetext = $use ? ' checked="checked" ' : '';
56         $s .= '<span id="settings_mathjax_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_mathjax_expanded\'); openClose(\'settings_mathjax_inflated\');">';
57         $s .= '<h3>MathJax ' . L10n::t('Settings') . '</h3>';
58         $s .= '</span>';
59         $s .= '<div id="settings_mathjax_expanded" class="settings-block" style="display: none;">';
60         $s .= '<span class="fakelink" onclick="openClose(\'settings_mathjax_expanded\'); openClose(\'settings_mathjax_inflated\');">';
61         $s .= '<h3>MathJax ' . L10n::t('Settings') . '</h3>';
62         $s .= '</span>';
63         $s .= '<p>' . 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.') . '</p>';
64         $s .= '<label id="mathjax_label" for="mathjax_use">' . L10n::t('Use the MathJax renderer') . '</label>';
65         $s .= '<input id="mathjax_use" type="checkbox" name="mathjax_use" value="1"' . $usetext . ' />';
66         $s .= '<div class="clear"></div>';
67
68         $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="mathjax-submit" name="mathjax-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
69         $s .= '</div>';
70 }
71
72 /*  we need to add one JavaScript include command to the html output
73  *  note that you have to check the jsmath/easy/load.js too.
74  */
75 function mathjax_page_end(App $a, &$b)
76 {
77         //  if the visitor of the page is not a local_user, use MathJax
78         //  otherwise check the users settings.
79         $url = $a->get_baseurl() . '/addon/mathjax/asset/MathJax.js?config=TeX-MML-AM_CHTML';
80
81         if (!local_user() || PConfig::get(local_user(), 'mathjax', 'use', false)) {
82                 $b .= <<<HTML
83 <script type="text/javascript" src="{$url}"></script>
84 <script type="text/javascript">
85         document.addEventListener('postprocess_liveupdate', function () {
86                 MathJax.Hub.Queue(['Typeset', MathJax.Hub]);
87         });
88 </script>
89 HTML;
90         }
91 }