Add MathML interface to LocalChecker

In preparation for caching we store the MathML fragment returned by
local checker. The rational is that the string serialization of the
parse tree is better chachable as the parsetree.

Change-Id: Ice2ef1f4f7b83ed187507d4d067f446603d0d6a5
This commit is contained in:
Moritz Schubotz (physikerwelt) 2023-06-19 17:39:49 +02:00
parent a5eea48d1a
commit fc425b977c
No known key found for this signature in database
GPG key ID: F803DB146DDF36C3
3 changed files with 20 additions and 1 deletions

View file

@ -13,6 +13,8 @@ class LocalChecker extends BaseChecker {
private ?Message $error = null;
private ?TexArray $parseTree = null;
private ?string $mathMl = null;
/**
* @param string $tex the TeX input string to be checked
* @param string $type the input type
@ -55,4 +57,9 @@ class LocalChecker extends BaseChecker {
public function getParseTree(): ?TexArray {
return $this->parseTree;
}
public function getPresentationMathMLFragment(): string {
$this->mathMl ??= $this->parseTree->renderMML();
return $this->mathMl;
}
}

View file

@ -28,7 +28,7 @@ class MathNativeMML extends MathMathML {
}
protected function doRender(): StatusValue {
$presentation = $this->getChecker()->getParseTree()->renderMML();
$presentation = $this->getChecker()->getPresentationMathMLFragment();
$config = MediaWikiServices::getInstance()->getMainConfig();
$attributes = [ 'class' => 'mwe-math-element' ];
if ( $this->getID() !== '' ) {

View file

@ -94,4 +94,16 @@ class LocalCheckerTest extends MediaWikiIntegrationTestCase {
$this->assertInstanceOf( TexArray::class, $parseTree );
$this->assertSame( 0, $parseTree->getLength() );
}
public function testGetMML() {
$checker = new LocalChecker( 'e^{i \pi} + 1 = 0' );
$mml = $checker->getPresentationMathMLFragment();
$this->assertStringContainsString( '<mn>0</mn>', $mml );
}
public function testGetMMLEmpty() {
$checker = new LocalChecker( '' );
$mml = $checker->getPresentationMathMLFragment();
$this->assertSame( '', $mml );
}
}