Skip to content

Commit

Permalink
fix: Render latex on php mode (#162)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
xjiang-at-wiris and usantos-at-wiris authored Oct 31, 2024
1 parent cffacdf commit 4a94909
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
12 changes: 12 additions & 0 deletions classes/pluginwrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
27 changes: 20 additions & 7 deletions subfilters/php.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ public function filter($text, array $options = []) {
$n1 = stripos($text, '<math');
$n2 = mb_stripos($text, '«applet');

if ($n0 === false && $n1 === false && $n2 === false) {
// Find LateX.
$matches = [];
$latexpattern = '/\$\$(.*?)\$\$/';
preg_match_all($latexpattern, $text, $matches);

if ($n0 === false && $n1 === false && $n2 === false && count($matches[0]) == 0) {
// Nothing to do.
return $text;
}
Expand All @@ -66,12 +71,6 @@ public function filter($text, array $options = []) {
// Automatic class loading not avaliable for Moodle 2.4 and 2.5.
wrs_loadclasses();

// MathJax and MathML
// Not filter if MathJax filter order < MathType filter order.
if ($n1 !== false && $this->mathjax_have_preference()) {
return $text;
}

$wirispluginwrapper = new filter_wiris_pluginwrapper();

$wirispluginwrapper->begin();
Expand All @@ -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.
Expand Down

0 comments on commit 4a94909

Please sign in to comment.