From 4a94909cc5ed57264d7279049facc25936521f03 Mon Sep 17 00:00:00 2001 From: Xinyu Jiang <125269853+xjiang-at-wiris@users.noreply.github.com> Date: Thu, 31 Oct 2024 10:43:12 +0100 Subject: [PATCH] fix: Render latex on php mode (#162) * feature: render latex on php mode * chore: lint * chore: check with linter * chore: pass linter again * chore: update comment Co-authored-by: Ubaldo Santos <164866893+usantos-at-wiris@users.noreply.github.com> * chore: update comment Co-authored-by: Ubaldo Santos <164866893+usantos-at-wiris@users.noreply.github.com> * chore: update comment --------- Co-authored-by: Ubaldo Santos <164866893+usantos-at-wiris@users.noreply.github.com> --- classes/pluginwrapper.php | 12 +++++++++ .../plugin/impl/TextServiceImpl.class.php | 4 +-- subfilters/php.php | 27 ++++++++++++++----- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/classes/pluginwrapper.php b/classes/pluginwrapper.php index 13c0a73a..661dcb2c 100644 --- a/classes/pluginwrapper.php +++ b/classes/pluginwrapper.php @@ -208,6 +208,18 @@ public function was_chem_editor_enabled() { return $this->moodleconfig->waschemeditorenabled; } + /** + * Checks whether the LaTeX parsing feature is enabled. + * + * This method retrieves the configuration setting for LaTeX. + * + * @return bool True if LaTeX parsing is enabled, false otherwise. + */ + public function wiris_editor_parse_latex() { + $value = $this->get_instance()->getConfiguration()->getProperty('wiriseditorparselatex', null); + return ($value == "true") ? true : false; + } + /** * Returns MathType plugin data from the plugin installed in the default Moodle * HTML editor (or the first available), or false if none found. diff --git a/integration/lib/com/wiris/plugin/impl/TextServiceImpl.class.php b/integration/lib/com/wiris/plugin/impl/TextServiceImpl.class.php index f99cebfc..42be3b19 100644 --- a/integration/lib/com/wiris/plugin/impl/TextServiceImpl.class.php +++ b/integration/lib/com/wiris/plugin/impl/TextServiceImpl.class.php @@ -49,14 +49,14 @@ public function getMathML($digest, $latex) { } } public function latex2mathml($latex) { - $param = array();; + $param = array(); $param["latex"] = $latex; $provider = $this->plugin->newGenericParamsProvider($param); $mathml = $this->service("latex2mathml", $provider); return ((_hx_index_of($mathml, "Http Error #500", null) === -1) ? $mathml : "Error converting from latex to mathml"); } public function mathml2latex($mml) { - $param = array();; + $param = array(); $param["mml"] = $mml; $provider = $this->plugin->newGenericParamsProvider($param); return $this->service("mathml2latex", $provider); diff --git a/subfilters/php.php b/subfilters/php.php index e123d8f3..cbce0abe 100644 --- a/subfilters/php.php +++ b/subfilters/php.php @@ -56,7 +56,12 @@ public function filter($text, array $options = []) { $n1 = stripos($text, 'mathjax_have_preference()) { - return $text; - } - $wirispluginwrapper = new filter_wiris_pluginwrapper(); $wirispluginwrapper->begin(); @@ -89,6 +88,20 @@ public function filter($text, array $options = []) { $query .= 'category=' . $COURSE->category; } + // If MathJax doesn't have preference and wiriseditorparselatex = true, parse LateX into MathML. + if (!$this->mathjax_have_preference() && $wirispluginwrapper->wiris_editor_parse_latex()) { + foreach ($matches[0] as $latex) { + $response = $textservice->getMathML(null, $latex); + + $decodedresponse = json_decode($response, true); + if (isset($decodedresponse['status']) && $decodedresponse['status'] === "ok") { + $mathml = $decodedresponse['result']['text']; + + $text = str_replace($latex, $mathml, $text); + } + } + } + $prop['refererquery'] = $query; $prop['lang'] = current_language(); $prop['savemode'] = 'safeXml'; // ...safeXml filtering.