From 8851ae465847b5bda573e096eff52e8914032b96 Mon Sep 17 00:00:00 2001 From: 7u83 <7u83@mail.ru> Date: Fri, 1 Aug 2025 11:05:07 +0200 Subject: [PATCH] Uses katex now to convert latex to mathml. --- TextformatterLatexMathML.module | 67 ++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/TextformatterLatexMathML.module b/TextformatterLatexMathML.module index c16c75a..0ad4f49 100644 --- a/TextformatterLatexMathML.module +++ b/TextformatterLatexMathML.module @@ -30,9 +30,59 @@ class TextFormatterLatexMathML extends Textformatter { 'href' => 'https://git.planix.org/7u83/processwire-TextformatterLatexMathML', 'requires' => array('ProcessWire>=3.0.0'), ); - } + } - public function format(&$str) { + +function smartQuotesGermanHTML($html) { + $doc = new \DOMDocument(); + libxml_use_internal_errors(true); // Verhindert Warnungen bei schlechtem HTML + $doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8')); + + $xpath = new \DOMXPath($doc); + + foreach ($xpath->query('//text()') as $textNode) { + $text = $textNode->nodeValue; + $r = $this->replaceQuotesInText($text); + $textNode->nodeValue = $r; //this->replaceQuotesInText($text); + } + + // Nur extrahieren, weil DOMDocument etc. einfügt + $bodyNode = $doc->getElementsByTagName('body')->item(0); + $innerHTML = ''; + foreach ($bodyNode->childNodes as $child) { + $innerHTML .= $doc->saveHTML($child); + } + + return $innerHTML; +} + +function replaceQuotesInText($text) { + static $isOpen = true; + $result = ''; + $len = mb_strlen($text); + for ($i = 0; $i < $len; $i++) { + $char = mb_substr($text, $i, 1); + if ($char === '"') { + $result .= $isOpen ? '„' : '“'; + $isOpen = !$isOpen; + } else { + $result .= $char; + } + } + return $result; +} + + + + + + + + + +public function format(&$str) { + # $str = $this->smartQuotesGermanHTML($str); + # return; // Regular expression to match LaTeX formulas within $ symbols $latexPattern = '/(?loadHTML(''.$inputString.'', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); + $dom->loadHTML(''.$inputString.'', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); // Create a DOMXPath object to navigate the DOMDocument $xpath = new \DOMXPath($dom); @@ -92,7 +142,7 @@ class TextFormatterLatexMathML extends Textformatter { // Define a callback function to convert the matched formula to MathML function convertToMathML($matches) { $cache = wire('cache'); - # $cache->deleteAll(); + # $cache->deleteAll(); // $matches[1] contains the matched substring within '$' $formula = $matches[1]; @@ -112,8 +162,12 @@ class TextFormatterLatexMathML extends Textformatter { // Function to convert a formula to MathML function convertFormulaToMathML($formula) { - $command = 'latexmlmath --quiet -'; - $inputString = '$'.$formula.'$'; + # $command = 'latexmlmath --quiet -'; + # + + $command = '/usr/local/bin/katex --format mathml'; +# $inputString = '$'.$formula.'$'; + $inputString = $formula; // Construct the full command with the input string @@ -122,6 +176,7 @@ class TextFormatterLatexMathML extends Textformatter { // Execute the command and capture the output $output = shell_exec($fullCommand); + $output = str_replace(' xmlns="http://www.w3.org/1998/Math/MathML"', '', $output); return $output; }