xref: /plugin/combo/syntax/math.php (revision 4cadd4f8c541149bdda95f080e38a6d4e3a640ca)
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     * Syntax Type
20     *
21     * Protected in order to say that we don't want it to be modified
22     * The mathjax javascript will take care of the rendering
23     *
24     * @return string
25     */
26    public function getType()
27    {
28        return 'substition';
29    }
30
31    /**
32     * @return array
33     * Allow which kind of plugin inside
34     *
35     * No one of array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs')
36     * because we manage self the content and we call self the parser
37     */
38    public function getAllowedTypes()
39    {
40        return array();
41    }
42
43    /**
44     * How Dokuwiki will add P element
45     *
46     *  * 'normal' - The plugin can be used inside paragraphs
47     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
48     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
49     *
50     * @see DokuWiki_Syntax_Plugin::getPType()
51     */
52    function getPType()
53    {
54        return 'normal';
55    }
56
57    /**
58     *
59     * @return int
60     */
61    public function getSort()
62    {
63        return 195;
64    }
65
66    /**
67     *
68     * @param string $mode
69     */
70    public function connectTo($mode)
71    {
72
73        // Add the entry patterns
74        foreach (self::getTags() as $element) {
75
76            $pattern = PluginUtility::getLeafContainerTagPattern($element);
77            $this->Lexer->addSpecialPattern($pattern, $mode, PluginUtility::getModeFromTag($this->getPluginComponent()));
78
79        }
80
81
82    }
83
84
85    /**
86     *
87     * @param string $match The text matched by the patterns
88     * @param int $state The lexer state for the match
89     * @param int $pos The character position of the matched text
90     * @param Doku_Handler $handler The Doku_Handler object
91     * @return  array Return an array with all data you want to use in render
92     */
93    public function handle($match, $state, $pos, Doku_Handler $handler)
94    {
95
96        return array($match);
97    }
98
99    /**
100     * Render the output
101     * @param string $format
102     * @param Doku_Renderer $renderer
103     * @param array $data - what the function handle() return'ed
104     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
105     * @see DokuWiki_Syntax_Plugin::render()
106     *
107     *
108     */
109    function render($format, Doku_Renderer $renderer, $data)
110    {
111
112        list($content) = $data;
113        switch ($format) {
114            case 'xhtml':
115            case 'odt':
116                /** @var Doku_Renderer_xhtml $renderer */
117                $renderer->doc .= $renderer->_xmlEntities($content) . DOKU_LF;
118
119                /**
120                 * CSS
121                 */
122                $snippetManager = PluginUtility::getSnippetManager();
123                $snippetManager->attachCssInternalStyleSheetForSlot(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                $snippetManager
144                    ->attachInternalJavascriptForSlot(
145                    self::TAG,
146                    $headHtmlElement
147                )
148                    ->addHtmlAttribute("type", "text/x-mathjax-config");
149                $snippetManager
150                    ->attachJavascriptLibraryForSlot(
151                        self::TAG,
152                        "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js"
153                    )
154                    ->setDoesManipulateTheDomOnRun(false);
155                break;
156
157            case 'latexport':
158                // Pass math expressions to latexport renderer
159                $renderer->mathjax_content($content);
160                break;
161
162            default:
163                $renderer->doc .= $renderer->$data;
164                break;
165
166        }
167
168        return true;
169
170    }
171
172    static public function getTags()
173    {
174        return PluginUtility::getTags(get_called_class());
175    }
176
177    public static function getComponentName()
178    {
179        return PluginUtility::getTagName(get_called_class());
180    }
181
182}
183
184