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