Set version to 0.9.1

This commit is contained in:
7u83 2023-11-25 16:27:47 +01:00
parent 50a033b7e3
commit 1dcebd4fc5
1 changed files with 126 additions and 126 deletions

View File

@ -1,126 +1,126 @@
<?php <?php
/* /*
Copyright (C) 2023 by 7u83 7u83@mail.ru Copyright (C) 2023 by 7u83 7u83@mail.ru
Permission to use, copy, modify, and/or distribute this software Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted. for any purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
*/ */
namespace ProcessWire; namespace ProcessWire;
class TextFormatterLatexMathML extends Textformatter { class TextFormatterLatexMathML extends Textformatter {
public static function getModuleInfo() { public static function getModuleInfo() {
return array( return array(
'title' => 'LatexMathML Text Formatter', 'title' => 'LatexMathML Text Formatter',
'version' => '1.0.0', 'version' => '0.9.1',
'summary' => 'Replaces $latexformula$ with inline MathML', 'summary' => 'Replaces $latexformula$ with inline MathML',
'author' = '7u83', 'author' = '7u83',
'autoload' => true, 'autoload' => true,
'singular' => true, 'singular' => true,
'href' => 'https://git.planix.org/7u83/processwire-TextformatterLatexMathML', 'href' => 'https://git.planix.org/7u83/processwire-TextformatterLatexMathML',
'requires' => array('ProcessWire>=3.0.0'), 'requires' => array('ProcessWire>=3.0.0'),
); );
} }
public function format(&$str) { public function format(&$str) {
// Regular expression to match LaTeX formulas within $ symbols // Regular expression to match LaTeX formulas within $ symbols
$latexPattern = '/(?<!\\\\)\$([^$\\\\]*(\\\\.[^$\\\\]*)*)\$/'; $latexPattern = '/(?<!\\\\)\$([^$\\\\]*(\\\\.[^$\\\\]*)*)\$/';
$inputString = $str; $inputString = $str;
// Create a DOMDocument object // Create a DOMDocument object
$dom = new \DOMDocument('1.0', 'UTF-8'); $dom = new \DOMDocument('1.0', 'UTF-8');
// Load the HTML string into the DOMDocument // 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 // Create a DOMXPath object to navigate the DOMDocument
$xpath = new \DOMXPath($dom); $xpath = new \DOMXPath($dom);
// Find all <p>, <div>, <span> ... elements // Find all <p>, <div>, <span> ... elements
$elements = $xpath->query('//p | //div | //span | //strong | //b | //i | //em'); $elements = $xpath->query('//p | //div | //span | //strong | //b | //i | //em');
$nodesForReplacement = array(); $nodesForReplacement = array();
// Iterate through the elements and create and arraz of nodes to modify // Iterate through the elements and create and arraz of nodes to modify
foreach ($elements as $element) { foreach ($elements as $element) {
foreach ($element->childNodes as $node) { foreach ($element->childNodes as $node) {
if ($node->nodeType === XML_TEXT_NODE) { if ($node->nodeType === XML_TEXT_NODE) {
// convert $formula$ to mathml // convert $formula$ to mathml
$modifiedValue = preg_replace_callback($latexPattern, array($this,'convertToMathML'), $node->nodeValue); $modifiedValue = preg_replace_callback($latexPattern, array($this,'convertToMathML'), $node->nodeValue);
// unescape esaped $-signs // unescape esaped $-signs
$modifiedValue = str_replace('\$', '$',$modifiedValue); $modifiedValue = str_replace('\$', '$',$modifiedValue);
// Save the node and its modified content for later replacement // Save the node and its modified content for later replacement
$nodesForReplacement[] = array( $nodesForReplacement[] = array(
'node' => $node, 'node' => $node,
'modifiedValue' => $modifiedValue 'modifiedValue' => $modifiedValue
); );
} }
} }
} }
// Replace the nodes // Replace the nodes
foreach ($nodesForReplacement as $replacement) { foreach ($nodesForReplacement as $replacement) {
$fragment = $dom->createDocumentFragment(); $fragment = $dom->createDocumentFragment();
$fragment->appendXML($replacement['modifiedValue']); $fragment->appendXML($replacement['modifiedValue']);
$replacement['node']->parentNode->replaceChild($fragment, $replacement['node']); $replacement['node']->parentNode->replaceChild($fragment, $replacement['node']);
} }
// Output the modified HTML // Output the modified HTML
$str = str_replace(array('<html>','</html>') , '' , $dom->saveHTML($dom->documentElement)); $str = str_replace(array('<html>','</html>') , '' , $dom->saveHTML($dom->documentElement));
} }
// Define a callback function to convert the matched formula to MathML // Define a callback function to convert the matched formula to MathML
function convertToMathML($matches) { function convertToMathML($matches) {
$cache = wire('cache'); $cache = wire('cache');
// $matches[1] contains the matched substring within '$' // $matches[1] contains the matched substring within '$'
$formula = $matches[1]; $formula = $matches[1];
$cn = "TFLatexMathML$formula"; $cn = "TFLatexMathML$formula";
$mathml = base64_decode($cache->get($cn)); $mathml = base64_decode($cache->get($cn));
if ($mathml) if ($mathml)
return $mathml; return $mathml;
$mathml = $this->convertFormulaToMathML($formula); $mathml = $this->convertFormulaToMathML($formula);
$cache->save($cn,base64_encode($mathml)); $cache->save($cn,base64_encode($mathml));
// Return the processed result as the replacement // Return the processed result as the replacement
return $mathml; return $mathml;
} }
// Function to convert a formula to MathML // Function to convert a formula to MathML
function convertFormulaToMathML($formula) { function convertFormulaToMathML($formula) {
$command = 'latexmlmath --quiet -'; $command = 'latexmlmath --quiet -';
$inputString = '$'.$formula.'$'; $inputString = '$'.$formula.'$';
// Construct the full command with the input string // Construct the full command with the input string
$fullCommand = sprintf('echo %s | %s', escapeshellarg($inputString), $command); $fullCommand = sprintf('echo %s | %s', escapeshellarg($inputString), $command);
// Execute the command and capture the output // Execute the command and capture the output
$output = shell_exec($fullCommand); $output = shell_exec($fullCommand);
return $output; return $output;
} }
} }