4 * Description: Addon for Friendika to include MathJax (LaTeX math syntax)
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
12 use Friendica\Core\Addon;
13 use Friendica\Core\L10n;
14 use Friendica\Core\PConfig;
16 function mathjax_install()
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');
23 function mathjax_uninstall()
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');
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');
35 function mathjax_settings_post($a)
41 if (empty($_POST['mathjax-submit'])) {
45 PConfig::set(local_user(), 'mathjax', 'use', intval($_POST['mathjax_use']));
48 function mathjax_settings(App $a, &$s)
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>';
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>';
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>';
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>';
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.
75 function mathjax_page_end(App $a, &$b)
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';
81 if (!local_user() || PConfig::get(local_user(), 'mathjax', 'use', false)) {
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]);