| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Extension\Math\MathRenderer; |
| 22 |
use MediaWiki\Maintenance\Maintenance; |
| 23 |
|
| 24 |
require_once __DIR__ . '/../../../maintenance/Maintenance.php'; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
r |
| 36 |
|
| 37 |
class JsonToMathML extends Maintenance { |
| 38 |
|
| 39 |
private $inputPath = ""; |
| 40 |
|
| 41 |
|
| 42 |
private $outputPath = ""; |
| 43 |
|
| 44 |
private $inputFormat = 0; |
| 45 |
|
| 46 |
public function __construct() { |
| 47 |
parent::__construct(); |
| 48 |
$this->addDescription( 'From a JSON file containing (La)TeX math inputs, create a JSON file with LaTeX and ' . |
| 49 |
'corresponding MathML.' ); |
| 50 |
$this->addArg( 'input-path', "Path (with filename) of the json file read by this script.", |
| 51 |
true ); |
| 52 |
$this->addArg( 'output-path', "Path (with filename) of the output json file created by this script.", |
| 53 |
true ); |
| 54 |
$this->addOption( 'inputformat', 'Custom parsing how to format the input-json (see formatInput function)', |
| 55 |
false, true, 'i' ); |
| 56 |
$this->addOption( 'chem-fallback', 'If the json read does not define input-type (tex or chem), check ' . |
| 57 |
'expressions as Tex and then as chem', false, true, 'c' ); |
| 58 |
$this->requireExtension( 'Math' ); |
| 59 |
} |
| 60 |
|
| 61 |
public function execute() { |
| 62 |
$this->inputFormat = $this->getOption( "inputformat", 0 ); |
| 63 |
$this->inputPath = $this->getArg( 0 ); |
| 64 |
$this->outputPath = $this->getArg( 1 ); |
| 65 |
$this->readJsonAndGenerateMML(); |
| 66 |
} |
| 67 |
|
| 68 |
public function readJsonAndGenerateMML() { |
| 69 |
$inputTex = $this->getJSON( $this->inputPath ); |
| 70 |
if ( $inputTex == null ) { |
| 71 |
throw new InvalidArgumentException( "Provide a json file as input which has content" ); |
| 72 |
} |
| 73 |
$inputTexF = $this->formatInput( $inputTex ); |
| 74 |
$allEntries = []; |
| 75 |
|
| 76 |
foreach ( $inputTexF as $entry ) { |
| 77 |
try { |
| 78 |
$mmlMathoid = $this->fetchMathML( $entry['tex'], $entry['type'], 'mathml' ); |
| 79 |
if ( ( $this->getOption( "chem-fallback", 0 ) && !$mmlMathoid ) || $mmlMathoid == "" ) { |
| 80 |
$mmlMathoid = $this->fetchMathML( $entry['tex'], "chem", 'mathml' ); |
| 81 |
if ( $mmlMathoid && $mmlMathoid != "" ) { |
| 82 |
$entry['type'] = "chem"; |
| 83 |
} |
| 84 |
} |
| 85 |
$mmlLaTeXML = $this->fetchMathML( $entry['tex'], $entry['type'], 'latexml' ); |
| 86 |
$entryToAdd = [ |
| 87 |
"tex" => $entry['tex'], |
| 88 |
"type" => $entry['type'], |
| 89 |
"mmlMathoid" => $mmlMathoid, |
| 90 |
"mmlLaTeXML" => $mmlLaTeXML |
| 91 |
]; |
| 92 |
|
| 93 |
if ( array_key_exists( "texNew", $entry ) ) { |
| 94 |
$entryToAdd["texNew"] = $entry["texNew"]; |
| 95 |
} |
| 96 |
if ( array_key_exists( "typeC", $entry ) ) { |
| 97 |
$entryToAdd["typeC"] = $entry["typeC"]; |
| 98 |
} |
| 99 |
if ( array_key_exists( "description", $entry ) ) { |
| 100 |
$entryToAdd["description"] = $entry["description"]; |
| 101 |
} |
| 102 |
|
| 103 |
$allEntries[] = $entryToAdd; |
| 104 |
} catch ( Exception $e ) { |
| 105 |
$allEntries[] = [ |
| 106 |
"tex" => $entry['tex'], |
| 107 |
"type" => $entry['type'], |
| 108 |
"mmlMathoid" => "skipped (Exception)", |
| 109 |
"mmlLaTeXML" => "skipped (Exception)", |
| 110 |
"skipped" => true, |
| 111 |
]; |
| 112 |
$this->output( "Exception occurred during rendering:" . $entry['tex'] . " render:" . $e . "\n" ); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
$this->writeToFile( $this->outputPath, $allEntries ); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
private function formatInput( $fileData ) { |
| 125 |
$inputF = []; |
| 126 |
switch ( $this->inputFormat ) { |
| 127 |
case 0: |
| 128 |
|
| 129 |
foreach ( $fileData as $entry ) { |
| 130 |
$inputF[] = [ |
| 131 |
"tex" => $entry['input'], |
| 132 |
"type" => "tex", |
| 133 |
]; |
| 134 |
} |
| 135 |
break; |
| 136 |
case 1: |
| 137 |
|
| 138 |
foreach ( $fileData as $tex => $mml ) { |
| 139 |
$inputF[] = [ |
| 140 |
"tex" => $tex, |
| 141 |
"type" => "tex", |
| 142 |
]; |
| 143 |
} |
| 144 |
break; |
| 145 |
case 2: |
| 146 |
|
| 147 |
foreach ( $fileData as $tex => $type ) { |
| 148 |
$inputF[] = [ |
| 149 |
"tex" => $tex, |
| 150 |
"type" => $type, |
| 151 |
]; |
| 152 |
} |
| 153 |
break; |
| 154 |
case 3: |
| 155 |
|
| 156 |
foreach ( $fileData as $group => $cases ) { |
| 157 |
foreach ( $cases as $case ) { |
| 158 |
$inputF[] = [ |
| 159 |
"description" => $group, |
| 160 |
"tex" => $case["tex"], |
| 161 |
"texNew" => $case["texNew"], |
| 162 |
"type" => "chem", |
| 163 |
"typeC" => $case["type"] |
| 164 |
]; |
| 165 |
} |
| 166 |
} |
| 167 |
break; |
| 168 |
case 4: |
| 169 |
|
| 170 |
foreach ( $fileData as $entry ) { |
| 171 |
$inputF[] = [ |
| 172 |
"description" => $entry["description"], |
| 173 |
"tex" => $entry['tex'], |
| 174 |
"type" => $entry['type'], |
| 175 |
]; |
| 176 |
} |
| 177 |
break; |
| 178 |
} |
| 179 |
return $inputF; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
private function getJSON( string $filePath ) { |
| 188 |
$file = file_get_contents( $filePath ); |
| 189 |
$json = json_decode( $file, true ); |
| 190 |
return $json; |
| 191 |
} |
| 192 |
|
| 193 |
public function writeToFile( string $fullPath, array $allEntries ): void { |
| 194 |
$jsonData = json_encode( $allEntries, JSON_PRETTY_PRINT ); |
| 195 |
file_put_contents( $fullPath, $jsonData ); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public function fetchMathML( string $tex, string $type, string $renderingMode ): string { |
| 206 |
$params = [ "type" => $type ]; |
| 207 |
if ( $type == "chem" ) { |
| 208 |
$params["chem"] = true; |
| 209 |
$tex = "\\ce{ " . $tex . " }"; |
| 210 |
} |
| 211 |
|
| 212 |
$renderer = $this->getServiceContainer()->get( 'Math.RendererFactory' ) |
| 213 |
->getRenderer( $tex, $params, $renderingMode ); |
| 214 |
$renderer->render(); |
| 215 |
$mml = $renderer->getMathml(); |
| 216 |
return $mml; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
$maintClass = JsonToMathML::class; |
| 221 |
require_once RUN_MAINTENANCE_IF_MAIN; |