xref: /plugin/combo/syntax/math.php (revision 85e82846b0a214bc35e62864fa49d9cad0723d0e)
1<?php
2
3use ComboStrap\SnippetManager;
4use ComboStrap\LogUtility;
5use ComboStrap\PluginUtility;
6
7if (!defined('DOKU_INC')) die();
8require_once(__DIR__ . '/../class/PluginUtility.php');
9
10
11/**
12 * Class syntax_plugin_combo_math
13 */
14class syntax_plugin_combo_math extends DokuWiki_Syntax_Plugin
15{
16
17    const TAG = "math";
18
19
20    /**
21     * syntax_plugin_combo_math constructor.
22     */
23    public function __construct()
24    {
25        LogUtility::msg("The math syntax object was instantiated", LogUtility::LVL_MSG_DEBUG);
26    }
27
28
29    /**
30     * Syntax Type
31     *
32     * Protected in order to say that we don't want it to be modified
33     * The mathjax javascript will take care of the rendering
34     *
35     * @return string
36     */
37    public function getType()
38    {
39        return 'substition';
40    }
41
42    /**
43     * @return array
44     * Allow which kind of plugin inside
45     *
46     * No one of array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs')
47     * because we manage self the content and we call self the parser
48     */
49    public function getAllowedTypes()
50    {
51        return array();
52    }
53
54    /**
55     * How Dokuwiki will add P element
56     *
57     *  * 'normal' - The plugin can be used inside paragraphs
58     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
59     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
60     *
61     * @see DokuWiki_Syntax_Plugin::getPType()
62     */
63    function getPType()
64    {
65        return 'normal';
66    }
67
68    /**
69     *
70     * @return int
71     */
72    public function getSort()
73    {
74        return 195;
75    }
76
77    /**
78     *
79     * @param string $mode
80     */
81    public function connectTo($mode)
82    {
83
84        // Add the entry patterns
85        foreach (self::getTags() as $element) {
86
87            $pattern = PluginUtility::getLeafContainerTagPattern($element);
88            $this->Lexer->addSpecialPattern($pattern, $mode, PluginUtility::getModeFromTag($this->getPluginComponent()));
89
90        }
91
92
93    }
94
95
96    /**
97     *
98     * @param string $match The text matched by the patterns
99     * @param int $state The lexer state for the match
100     * @param int $pos The character position of the matched text
101     * @param Doku_Handler $handler The Doku_Handler object
102     * @return  array Return an array with all data you want to use in render
103     */
104    public function handle($match, $state, $pos, Doku_Handler $handler)
105    {
106
107        return array($match);
108    }
109
110    /**
111     * Render the output
112     * @param string $format
113     * @param Doku_Renderer $renderer
114     * @param array $data - what the function handle() return'ed
115     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
116     * @see DokuWiki_Syntax_Plugin::render()
117     *
118     *
119     */
120    function render($format, Doku_Renderer $renderer, $data)
121    {
122
123        list($content) = $data;
124        switch ($format) {
125            case 'xhtml':
126            case 'odt':
127                /** @var Doku_Renderer_xhtml $renderer */
128                $renderer->doc .= $renderer->_xmlEntities($content) . DOKU_LF;
129
130                /**
131                 * CSS
132                 */
133                PluginUtility::getSnippetManager()->upsertCssSnippetForBar(self::TAG);
134
135                /**
136                 * Javascript config
137                 */
138                $headHtmlElement = <<<EOD
139MathJax.Hub.Config({
140    showProcessingMessages: true,
141    extensions: ["tex2jax.js","TeX/AMSmath.js","TeX/AMSsymbols.js"],
142    jax: ["input/TeX", "output/HTML-CSS"],
143    tex2jax: {
144        inlineMath: [ ["<math>","</math>"]],
145        displayMath: [ ["<MATH>","</MATH>"] ],
146        processEscapes: true,
147        scale:120
148    },
149    "HTML-CSS": { fonts: ["TeX"] }
150});
151EOD;
152
153                PluginUtility::getSnippetManager()->upsertTagsForBar(self::TAG,
154                    array("script" => [
155                        array(
156                            "type" => "text/x-mathjax-config",
157                            "_data" => $headHtmlElement
158                        ),
159                        array(
160                            "type" => "text/javascript",
161                            "src" => "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js",
162                            "async" => true
163                        )
164                    ])
165                );
166
167                break;
168
169            case 'latexport':
170                // Pass math expressions to latexport renderer
171                $renderer->mathjax_content($content);
172                break;
173
174            default:
175                $renderer->doc .= $renderer->$data;
176                break;
177
178        }
179
180        return true;
181
182    }
183
184    static public function getTags()
185    {
186        return PluginUtility::getTags(get_called_class());
187    }
188
189    public static function getComponentName()
190    {
191        return PluginUtility::getTagName(get_called_class());
192    }
193
194}
195
196