]> git.mxchange.org Git - friendica-addons.git/blob - mathjax/mathjax.php
7d1a81340694412bcd4c89ff235e1be5e6f41c9d
[friendica-addons.git] / mathjax / mathjax.php
1 <?php
2 /**
3  * Name: MathJax
4  * Description: Addon for Friendika to include MathJax (LaTeX math syntax)
5  * Version: 1.1
6  * Author: Tobias Diekershoff <https://social.diekershoff.de/profile/tobias>
7  * License: 3-clause BSD license
8  */
9 use Friendica\App;
10 use Friendica\Core\Addon;
11 use Friendica\Core\Config;
12 use Friendica\Core\L10n;
13 use Friendica\Core\PConfig;
14
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     logger('installed js_math addon');
20 }
21 function mathjax_uninstall() {
22     Addon::unregisterHook('page_header', 'addon/mathjax/mathjax.php', 'mathjax_page_header');
23     Addon::unregisterHook('addon_settings', 'addon/mathjax/mathjax.php', 'mathjax_settings');
24     Addon::unregisterHook('addon_settings_post', 'addon/mathjax/mathjax.php', 'mathjax_settings_post');
25 }
26 function mathjax_settings_post ($a, $post) {
27     if (! local_user())
28         return;
29     if (!x($_POST,'mathjax-submit'))
30         return;
31     PConfig::set(local_user(),'mathjax','use',intval($_POST['mathjax_use']));
32 }
33 function mathjax_settings (&$a, &$s) {
34     if (! local_user())
35         return;
36     $use = PConfig::get(local_user(),'mathjax','use');
37     $usetext = (($use) ? ' checked="checked" ' : '');
38     $s .= '<span id="settings_mathjax_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_mathjax_expanded\'); openClose(\'settings_mathjax_inflated\');">';
39     $s .= '<h3>MathJax '.L10n::t('Settings').'</h3>';
40     $s .= '</span>';
41     $s .= '<div id="settings_mathjax_expanded" class="settings-block" style="display: none;">';
42     $s .= '<span class="fakelink" onclick="openClose(\'settings_mathjax_expanded\'); openClose(\'settings_mathjax_inflated\');">';
43     $s .= '<h3>MathJax '.L10n::t('Settings').'</h3>';
44     $s .= '</span>';
45     $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>';
46     $s .= '<label id="mathjax_label" for="mathjax_use">'.L10n::t('Use the MathJax renderer').'</label>';
47     $s .= '<input id="mathjax_use" type="checkbox" name="mathjax_use" value="1"'. $usetext .' />';
48     $s .= '<div class="clear"></div>';
49
50     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="mathjax-submit" name="mathjax-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
51     $s .= '</div>';
52 }
53 /*  we need to add one JavaScript include command to the html output
54  *  note that you have to check the jsmath/easy/load.js too.
55  */
56 function mathjax_page_header($a, &$b) {
57     //  if the visitor of the page is not a local_user, use MathJax
58     //  otherwise check the users settings.
59     $url = Config::get ('mathjax','baseurl');
60                 if(! $url) {
61                         $url = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-MML-AM_CHTML';
62                 }
63     if (! local_user()) {
64         $b .= '<script type="text/javascript" src="'.$url.'" async></script>';
65     } else {
66         $use = PConfig::get(local_user(),'mathjax','use');
67         if ($use) {
68             $b .= '<script type="text/javascript" src="'.$url.'" async></script>';
69         }
70     }
71 }
72 function mathjax_addon_admin_post (&$a) {
73     $baseurl = ((x($_POST, 'mjbaseurl')) ? trim($_POST['mjbaseurl']) : 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-MML-AM_CHTML');
74     Config::set('mathjax','baseurl',$baseurl);
75     info(L10n::t('Settings updated.'). EOL);
76 }
77 function mathjax_addon_admin (App $a, &$o) {
78         $t = get_markup_template( "admin.tpl", "addon/mathjax/" );
79
80         if (Config::get('mathjax','baseurl','') == '') {
81                 Config::set('mathjax','baseurl','https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-MML-AM_CHTML');
82         }
83
84         $o = replace_macros( $t, [
85                 '$submit' => L10n::t('Save Settings'),
86                 '$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']
87         ]);
88 }