2a7bc89c221281160a4c2ef667e3e9dc156c660b
[friendica-addons.git] / mathjax / mathjax.php
1 <?php
2
3 /**
4  * Name: MathJax
5  * Description: Addon for Friendika to include MathJax (LaTeX math syntax)
6  * Version: 1.0
7  * Author: Tobias Diekershoff <https://f.diekershoff.de/profile/tobias>
8  * License: 3-clause BSD license
9  */
10
11 use Friendica\Core\Config;
12 use Friendica\Core\PConfig;
13
14 function mathjax_install() {
15     register_hook('page_header', 'addon/mathjax/mathjax.php', 'mathjax_page_header');
16     register_hook('plugin_settings', 'addon/mathjax/mathjax.php', 'mathjax_settings');
17     register_hook('plugin_settings_post', 'addon/mathjax/mathjax.php', 'mathjax_settings_post');
18     logger('installed js_math plugin');
19 }
20 function mathjax_uninstall() {
21     unregister_hook('page_header', 'addon/mathjax/mathjax.php', 'mathjax_page_header');
22     unregister_hook('plugin_settings', 'addon/mathjax/mathjax.php', 'mathjax_settings');
23     unregister_hook('plugin_settings_post', 'addon/mathjax/mathjax.php', 'mathjax_settings_post');
24 }
25 function mathjax_settings_post ($a, $post) {
26     if (! local_user())
27         return;
28     // don't check statusnet settings if statusnet submit button is not clicked
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 '.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 '.t('Settings').'</h3>';
44     $s .= '</span>';
45     $s .= '<p>'.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">'.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="' . 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 = 'http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML';
62     if (! local_user()) {
63         $b .= '<script type="text/javascript" src="'.$url.'"></script>';
64     } else {
65         $use = PConfig::get(local_user(),'mathjax','use');
66         if ($use) {
67             $b .= '<script type="text/javascript" src="'.$url.'"></script>';
68         }
69     }
70 }
71 function mathjax_plugin_admin_post (&$a) {
72     $baseurl = ((x($_POST, 'baseurl')) ? trim($_POST['baseurl']) : 'http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML');
73     Config::set('mathjax','baseurl',$baseurl);
74     info( t('Settings updated.'). EOL);
75 }
76 function mathjax_plugin_admin (&$a, &$o) {
77         $t = get_markup_template( "admin.tpl", "addon/mathjax/" );
78         if (Config::get('mathjax','baseurl','') == '') {
79                 Config::set('mathjax','baseurl','http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML');
80         }
81
82         $o = replace_macros( $t, [
83                 '$submit' => t('Save Settings'),
84                 '$baseurl' => ['baseurl', t('MathJax Base URL'), Config::get('mathjax','baseurl' ), 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.')],
85         ]);
86 }