Merge pull request 'Pnut: add connector for pnut.io' (#1466) from spacenerdmo/friendi...
[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\Renderer;
14 use Friendica\DI;
15
16 function mathjax_install()
17 {
18         Hook::register('footer'             , __FILE__, 'mathjax_footer');
19         Hook::register('addon_settings'     , __FILE__, 'mathjax_settings');
20         Hook::register('addon_settings_post', __FILE__, 'mathjax_settings_post');
21 }
22
23 function mathjax_settings_post()
24 {
25         if (!DI::userSession()->getLocalUserId() || empty($_POST['mathjax-submit'])) {
26                 return;
27         }
28
29         DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'mathjax', 'use', intval($_POST['mathjax_use']));
30 }
31
32 function mathjax_settings(array &$data)
33 {
34         if (!DI::userSession()->getLocalUserId()) {
35                 return;
36         }
37
38         $use = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'mathjax', 'use', false);
39
40         $tpl = Renderer::getMarkupTemplate('settings.tpl', 'addon/mathjax');
41         $html = Renderer::replaceMacros($tpl, [
42                 '$description'  => DI::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.'),
43                 '$mathjax_use'  => ['mathjax_use', DI::l10n()->t('Use the MathJax renderer'), $use, ''],
44         ]);
45
46         $data = [
47                 'addon' => 'mathjax',
48                 'title' => 'MathJax',
49                 'html'  => $html,
50         ];
51 }
52
53 function mathjax_footer(string &$body)
54 {
55         //  if the visitor of the page is not a local_user, use MathJax
56         //  otherwise check the users settings.
57         if (!DI::userSession()->getLocalUserId() || DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'mathjax', 'use', false)) {
58                 DI::page()->registerFooterScript(__DIR__ . '/asset/MathJax.js?config=TeX-MML-AM_CHTML');
59                 DI::page()->registerFooterScript(__DIR__ . '/mathjax.js');
60         }
61 }