]> git.mxchange.org Git - friendica-addons.git/blob - mathjax/mathjax.php
5118 Implement MathJax JS hook to update contents after live update
[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     Addon::registerHook('template_vars','addon/mathjax/mathjax.php', 'mathjax_template_vars');
20     logger('installed js_math addon');
21 }
22
23 function mathjax_template_vars($a, &$arr) {
24     $arr['vars']['addon_hooks'][] = "mathjax";
25 }
26
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');
32 }
33 function mathjax_settings_post ($a, $post) {
34     if (! local_user())
35         return;
36     if (!x($_POST,'mathjax-submit'))
37         return;
38     PConfig::set(local_user(),'mathjax','use',intval($_POST['mathjax_use']));
39 }
40 function mathjax_settings (&$a, &$s) {
41     if (! local_user())
42         return;
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>';
47     $s .= '</span>';
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>';
51     $s .= '</span>';
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>';
56
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>';
58     $s .= '</div>';
59 }
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.
62  */
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');
67                 if(! $url) {
68                         $url = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js?config=TeX-MML-AM_CHTML';
69                 }
70     if (! local_user()) {
71         $b .= '<script type="text/javascript" src="'.$url.'" async></script>';
72     } else {
73         $use = PConfig::get(local_user(),'mathjax','use');
74         if ($use) {
75             $b .= '<script type="text/javascript" src="'.$url.'" async></script>';
76         }
77     }
78 }
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);
83 }
84 function mathjax_addon_admin (App $a, &$o) {
85         $t = get_markup_template( "admin.tpl", "addon/mathjax/" );
86
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');
89         }
90
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']
94         ]);
95 }