4 * Description: Addon for Friendika to include MathJax (LaTeX math syntax)
6 * Author: Tobias Diekershoff <https://social.diekershoff.de/profile/tobias>
7 * License: 3-clause BSD license
10 use Friendica\Core\Addon;
11 use Friendica\Core\Config;
12 use Friendica\Core\L10n;
13 use Friendica\Core\PConfig;
15 function mathjax_install() {
16 Addon::registerHook('page_header', 'addon/mathjax/mathjax.php', 'mathjax_page_header');
17 Addon::registerHook('addon_settings', 'addon/mathjax/mathjax.php', 'mathjax_settings');
18 Addon::registerHook('addon_settings_post', 'addon/mathjax/mathjax.php', 'mathjax_settings_post');
19 Addon::registerHook('template_vars','addon/mathjax/mathjax.php', 'mathjax_template_vars');
20 logger('installed js_math addon');
23 function mathjax_template_vars($a, &$arr) {
24 $arr['vars']['addon_hooks'][] = "mathjax";
27 function mathjax_uninstall() {
28 Addon::unregisterHook('page_header', 'addon/mathjax/mathjax.php', 'mathjax_page_header');
29 Addon::unregisterHook('addon_settings', 'addon/mathjax/mathjax.php', 'mathjax_settings');
30 Addon::unregisterHook('addon_settings_post', 'addon/mathjax/mathjax.php', 'mathjax_settings_post');
31 Addon::unregisterHook('template_vars','addon/mathjax/mathjax.php', 'mathjax_template_vars');
33 function mathjax_settings_post ($a, $post) {
36 if (!x($_POST,'mathjax-submit'))
38 PConfig::set(local_user(),'mathjax','use',intval($_POST['mathjax_use']));
40 function mathjax_settings (&$a, &$s) {
43 $use = PConfig::get(local_user(),'mathjax','use');
44 $usetext = (($use) ? ' checked="checked" ' : '');
45 $s .= '<span id="settings_mathjax_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_mathjax_expanded\'); openClose(\'settings_mathjax_inflated\');">';
46 $s .= '<h3>MathJax '.L10n::t('Settings').'</h3>';
48 $s .= '<div id="settings_mathjax_expanded" class="settings-block" style="display: none;">';
49 $s .= '<span class="fakelink" onclick="openClose(\'settings_mathjax_expanded\'); openClose(\'settings_mathjax_inflated\');">';
50 $s .= '<h3>MathJax '.L10n::t('Settings').'</h3>';
52 $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>';
53 $s .= '<label id="mathjax_label" for="mathjax_use">'.L10n::t('Use the MathJax renderer').'</label>';
54 $s .= '<input id="mathjax_use" type="checkbox" name="mathjax_use" value="1"'. $usetext .' />';
55 $s .= '<div class="clear"></div>';
57 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="mathjax-submit" name="mathjax-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
60 /* we need to add one JavaScript include command to the html output
61 * note that you have to check the jsmath/easy/load.js too.
63 function mathjax_page_header($a, &$b) {
64 // if the visitor of the page is not a local_user, use MathJax
65 // otherwise check the users settings.
66 $url = Config::get ('mathjax','baseurl');
68 $url = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-MML-AM_CHTML';
71 $b .= '<script type="text/javascript" src="'.$url.'" async></script>';
73 $use = PConfig::get(local_user(),'mathjax','use');
75 $b .= '<script type="text/javascript" src="'.$url.'" async></script>';
79 function mathjax_addon_admin_post (&$a) {
80 $baseurl = ((x($_POST, 'mjbaseurl')) ? trim($_POST['mjbaseurl']) : 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-MML-AM_CHTML');
81 Config::set('mathjax','baseurl',$baseurl);
82 info(L10n::t('Settings updated.'). EOL);
84 function mathjax_addon_admin (App $a, &$o) {
85 $t = get_markup_template( "admin.tpl", "addon/mathjax/" );
87 if (Config::get('mathjax','baseurl','') == '') {
88 Config::set('mathjax','baseurl','https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-MML-AM_CHTML');
91 $o = replace_macros( $t, [
92 '$submit' => L10n::t('Save Settings'),
93 '$mjbaseurl' => ['mjbaseurl', L10n::t('MathJax Base URL'), Config::get('mathjax','baseurl' ), L10n::t('The URL for the javascript file that should be included to use MathJax. Can be either the MathJax CDN or another installation of MathJax.'), 'required']