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