CS translation blackout THX Aditoo
[friendica-addons.git/.git] / mathjax / mathjax.php
1 <?php
2 /**
3  * Name: MathJax
4  * Description: Addon for Friendica to include MathJax (LaTeX math syntax)
5  * Version: 2.0
6  * Author: Tobias Diekershoff <https://social.diekershoff.de/profile/tobias>
7  * Author: Hypolite Petovan <https://friendica.mrpetovan.com/profile/hypolite>
8  * License: 3-clause BSD license
9  */
10
11 use Friendica\App;
12 use Friendica\Core\Hook;
13 use Friendica\Core\L10n;
14 use Friendica\Core\PConfig;
15 use Friendica\Core\Renderer;
16
17 function mathjax_install()
18 {
19         Hook::register('footer'             , __FILE__, 'mathjax_footer');
20         Hook::register('addon_settings'     , __FILE__, 'mathjax_settings');
21         Hook::register('addon_settings_post', __FILE__, 'mathjax_settings_post');
22 }
23
24 function mathjax_uninstall()
25 {
26         Hook::unregister('footer'             , __FILE__, 'mathjax_footer');
27         Hook::unregister('addon_settings'     , __FILE__, 'mathjax_settings');
28         Hook::unregister('addon_settings_post', __FILE__, 'mathjax_settings_post');
29
30         // Legacy hooks
31         Hook::unregister('load_config'        , __FILE__, 'mathjax_load_config');
32         Hook::unregister('page_header'        , __FILE__, 'mathjax_page_header');
33         Hook::unregister('template_vars'      , __FILE__, 'mathjax_template_vars');
34 }
35
36 function mathjax_settings_post($a)
37 {
38         if (!local_user() || empty($_POST['mathjax-submit'])) {
39                 return;
40         }
41
42         PConfig::set(local_user(), 'mathjax', 'use', intval($_POST['mathjax_use']));
43 }
44
45 function mathjax_settings(App $a, &$s)
46 {
47         if (!local_user()) {
48                 return;
49         }
50
51         $use = PConfig::get(local_user(), 'mathjax', 'use', false);
52
53         $tpl = Renderer::getMarkupTemplate('settings.tpl', __DIR__);
54         $s .= Renderer::replaceMacros($tpl, [
55                 '$title'        => 'MathJax',
56                 '$description'  => 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.'),
57                 '$mathjax_use'  => ['mathjax_use', L10n::t('Use the MathJax renderer'), $use, ''],
58                 '$savesettings' => L10n::t('Save Settings'),
59         ]);
60 }
61
62 function mathjax_footer(App $a, &$b)
63 {
64         //  if the visitor of the page is not a local_user, use MathJax
65         //  otherwise check the users settings.
66         if (!local_user() || PConfig::get(local_user(), 'mathjax', 'use', false)) {
67                 $a->registerFooterScript(__DIR__ . '/asset/MathJax.js?config=TeX-MML-AM_CHTML');
68                 $a->registerFooterScript(__DIR__ . '/mathjax.js');
69         }
70 }