]> git.mxchange.org Git - friendica-addons.git/blob - mathjax/mathjax.php
9bff86a2a56bf04f76c021bdd30a587fe66f71ae
[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 function mathjax_install() {
12     register_hook('page_header', 'addon/mathjax/mathjax.php', 'mathjax_page_header');
13     register_hook('plugin_settings', 'addon/mathjax/mathjax.php', 'mathjax_settings'); 
14     register_hook('plugin_settings_post', 'addon/mathjax/mathjax.php', 'mathjax_settings_post');
15     logger('installed js_math plugin');
16 }
17 function mathjax_uninstall() {
18     unregister_hook('page_header', 'addon/mathjax/mathjax.php', 'mathjax_page_header');
19     unregister_hook('plugin_settings', 'addon/mathjax/mathjax.php', 'mathjax_settings'); 
20     unregister_hook('plugin_settings_post', 'addon/mathjax/mathjax.php', 'mathjax_settings_post');
21 }
22 function mathjax_settings_post ($a, $post) {
23     if (! local_user())
24         return;
25     // don't check statusnet settings if statusnet submit button is not clicked
26     if (!x($_POST,'mathjax-submit'))
27         return;
28     set_pconfig(local_user(),'mathjax','use',intval($_POST['mathjax_use']));
29 }
30 function mathjax_settings (&$a, &$s) {
31     if (! local_user())
32         return;
33     $use = get_pconfig(local_user(),'mathjax','use');
34     $usetext = (($use) ? ' checked="checked" ' : '');
35     $s .= '<span id="settings_mathjax_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_mathjax_expanded\'); openClose(\'settings_mathjax_inflated\');">';
36     $s .= '<h3>MathJax '.t('Settings').'</h3>';
37     $s .= '</span>';
38     $s .= '<div id="settings_mathjax_expanded" class="settings-block" style="display: none;">';
39     $s .= '<span class="fakelink" onclick="openClose(\'settings_mathjax_expanded\'); openClose(\'settings_mathjax_inflated\');">';
40     $s .= '<h3>MathJax '.t('Settings').'</h3>';
41     $s .= '</span>';
42     $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>';
43     $s .= '<label id="mathjax_label" for="mathjax_use">'.t('Use the MathJax renderer').'</label>';
44     $s .= '<input id="mathjax_use" type="checkbox" name="mathjax_use" value="1"'. $usetext .' />';
45     $s .= '<div class="clear"></div>';
46
47     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="mathjax-submit" name="mathjax-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div>';
48     $s .= '</div>';
49 }
50 /*  we need to add one JavaScript include command to the html output
51  *  note that you have to check the jsmath/easy/load.js too.
52  */
53 function mathjax_page_header($a, &$b) {
54     //  if the visitor of the page is not a local_user, use MathJax
55     //  otherwise check the users settings.
56     $url = get_config ('mathjax','baseurl');
57         if(! $url)
58                 $url = 'http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML';
59     if (! local_user()) {
60         $b .= '<script type="text/javascript" src="'.$url.'"></script>';
61     } else {
62         $use = get_pconfig(local_user(),'mathjax','use');
63         if ($use) { 
64             $b .= '<script type="text/javascript" src="'.$url.'"></script>';
65         }
66     }
67 }
68 function mathjax_plugin_admin_post (&$a) {
69     $baseurl = ((x($_POST, 'baseurl')) ? trim($_POST['baseurl']) : 'http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML');
70     set_config('mathjax','baseurl',$baseurl);
71     info( t('Settings updated.'). EOL);
72 }
73 function mathjax_plugin_admin (&$a, &$o) {
74         $t = get_markup_template( "admin.tpl", "addon/mathjax/" );
75         if (get_config('mathjax','baseurl','') == '') {
76                 set_config('mathjax','baseurl','http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML');
77         }
78
79         $o = replace_macros( $t, array(
80                 '$submit' => t('Save Settings'),
81                 '$baseurl' => array('baseurl', t('MathJax Base URL'), get_config('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.')),
82         ));
83 }