Squash literals in operatornames

Chains of single letter mi elements are rendered with no
space but one mi element with multiple letters gets
space. For example, the operator log will be translated
to <mi>log</mi> rather than <mi>l</mi><mi>o</mi><mi>g</mi>
as done by wikitexvc.

* Squash literals into single element, iff they don't
contain commands

Following the implementation of the literal rendering
it seems that all literals with special behaviour have
a command.

Bug: T375861
Change-Id: I97adf5f5132180503cbeb59d32fdb8d45b08aaa7
This commit is contained in:
physikerwelt 2024-10-13 23:57:28 +02:00 committed by Physikerwelt
parent 9a27d92c09
commit 87632766e0
3 changed files with 33 additions and 1 deletions

View file

@ -370,7 +370,7 @@ class BaseParsing {
$mmlNot = MMLParsingUtil::createNot();
}
$passedArgs = array_merge( $passedArgs, [ Tag::CLASSTAG => TexClass::OP, "mathvariant" => Variants::NORMAL ] );
return $mmlNot . $node->getArg()->renderMML( $passedArgs ) . $applyFct;
return $mmlNot . $node->getArg()->renderMML( $passedArgs, [ 'squashLiterals' => true ] ) . $applyFct;
}
public static function lap( $node, $passedArgs, $operatorContent, $name ) {

View file

@ -193,12 +193,32 @@ class TexArray extends TexNode implements \ArrayAccess, \IteratorAggregate {
return [ $hasNamedFct, $hasValidParameters ];
}
private function squashLiterals() {
$tmp = '';
foreach ( $this->args as $arg ) {
if ( !( $arg instanceof Literal ) ) {
return;
}
// Don't squash if there is a macro in the literal
if ( preg_match( "/[\\\\]/", $arg->getArg() ) ) {
return;
}
$tmp .= $arg->getArg();
}
$this->args = [ new Literal( $tmp ) ];
$this->curly = false;
}
public function renderMML( $arguments = [], $state = [] ) {
// Everything here is for parsing displaystyle, probably refactored to WikiTexVC grammar later
$fullRenderedArray = "";
$mmlStyles = [];
$currentColor = null;
if ( array_key_exists( 'squashLiterals', $state ) ) {
$this->squashLiterals();
}
for ( $i = 0, $count = count( $this->args ); $i < $count; $i++ ) {
$current = $this->args[$i];
if ( isset( $this->args[$i + 1] ) ) {

View file

@ -120,4 +120,16 @@ class TexArrayTest extends MediaWikiUnitTestCase {
$ta = new TexArray();
$ta[0] = 'a';
}
public function testSquashLiterals() {
$ta = new TexArray( new Literal( 'a' ), new Literal( 'b' ) );
$res = $ta->renderMML( [], [ 'squashLiterals' => true ] );
$this->assertEquals( '<mi>ab</mi>', $res );
}
public function testSquashLiteralsMacro() {
$ta = new TexArray( new Literal( 'a' ), new Literal( '\gamma' ) );
$res = $ta->renderMML( [], [ 'squashLiterals' => true ] );
$this->assertEquals( '<mi>a</mi><mi>&#x03B3;</mi>', $res );
}
}