Compare commits

..

3 Commits

Author SHA1 Message Date
8851ae4658 Uses katex now to convert latex to mathml. 2025-08-01 11:05:07 +02:00
bb37fc62bd Encodes & to & 2023-12-12 12:35:58 +01:00
89cf770472 Fixed module definition syntax error 2023-12-12 12:23:19 +01:00

View File

@ -24,7 +24,7 @@ class TextFormatterLatexMathML extends Textformatter {
'title' => 'LatexMathML Text Formatter',
'version' => '0.9.1',
'summary' => 'Replaces $latexformula$ with inline MathML',
'author' = '7u83',
'author' => '7u83',
'autoload' => true,
'singular' => true,
'href' => 'https://git.planix.org/7u83/processwire-TextformatterLatexMathML',
@ -32,7 +32,57 @@ class TextFormatterLatexMathML extends Textformatter {
);
}
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 <body> extrahieren, weil DOMDocument <html><body> 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 = '/(?<!\\\\)\$([^$\\\\]*(\\\\.[^$\\\\]*)*)\$/';
@ -43,7 +93,7 @@ class TextFormatterLatexMathML extends Textformatter {
$dom = new \DOMDocument('1.0', 'UTF-8');
// Load the HTML string into the DOMDocument
$dom->loadHTML('<?xml encoding="utf-8" ?><html>'.$inputString.'</html>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$dom->loadHTML('<?xml encoding="utf-8" ?'.'><html>'.$inputString.'</html>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
// Create a DOMXPath object to navigate the DOMDocument
$xpath = new \DOMXPath($dom);
@ -65,6 +115,8 @@ class TextFormatterLatexMathML extends Textformatter {
// unescape esaped $-signs
$modifiedValue = str_replace('\$', '$',$modifiedValue);
$modifiedValue = str_replace('&','&amp;',$modifiedValue);
// Save the node and its modified content for later replacement
$nodesForReplacement[] = array(
'node' => $node,
@ -90,6 +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();
// $matches[1] contains the matched substring within '$'
$formula = $matches[1];
@ -109,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
@ -119,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;
}