From d3c636883a07266e52abb9941d86c4885df5de30 Mon Sep 17 00:00:00 2001 From: Stegmujo Date: Thu, 5 Oct 2023 09:35:36 +0000 Subject: [PATCH] Render MathML for some letters used in mhchem * \Alpha \Beta \Chi \Epsilon \Eta \Iota \Kappa \Mu \Nu \Omicron \Rho \Tau \Zeta * \ca Bug: T340024 Change-Id: I0d9d8314644927ccae0794ae8459a3f3893fc24a --- src/TexVC/MMLmappings/BaseMappings.php | 14 ++++++++ src/TexVC/MMLmappings/BaseParsing.php | 12 +++++++ .../unit/TexVC/Mhchem/MhchemBasicMMLTest.php | 33 +++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/src/TexVC/MMLmappings/BaseMappings.php b/src/TexVC/MMLmappings/BaseMappings.php index 04c1be2d8..847e10606 100644 --- a/src/TexVC/MMLmappings/BaseMappings.php +++ b/src/TexVC/MMLmappings/BaseMappings.php @@ -774,6 +774,20 @@ class BaseMappings { "varointclockwise" => [ 'oint', '\u2232', [ "texClass" => TexClass::OP ] ], "\\P" => [ 'oint', '\u00B6', [ "texClass" => TexClass::OP ] ], 'textvisiblespace' => [ 'Insert', '\u2423' ], // From TextCompMappings.js (only makro it seems) + "Alpha" => [ 'customLetters', "A" ], + "Beta" => [ 'customLetters', "B" ], + "Chi" => [ 'customLetters', "X" ], + "Epsilon" => [ 'customLetters', "E" ], + "Eta" => [ 'customLetters', "H" ], + "Iota" => [ 'customLetters', "I" ], + "Kappa" => [ 'customLetters', "K" ], + "Mu" => [ 'customLetters', "M" ], + "Nu" => [ 'customLetters', "N" ], + "Omicron" => [ 'customLetters', "O" ], + "Rho" => [ 'customLetters', "P" ], + "Tau" => [ 'customLetters', "T" ], + "Zeta" => [ 'customLetters', "Z" ], + "ca" => [ "customLetters", "∼", true ] ]; private const ALL = [ diff --git a/src/TexVC/MMLmappings/BaseParsing.php b/src/TexVC/MMLmappings/BaseParsing.php index 5ed256d95..ff74878cf 100644 --- a/src/TexVC/MMLmappings/BaseParsing.php +++ b/src/TexVC/MMLmappings/BaseParsing.php @@ -211,6 +211,18 @@ class BaseParsing { return "tbd chemCustom"; } + public static function customLetters( $node, $passedArgs, $operatorContent, $name, $char, $isOperator = false ) { + $mrow = new MMLmrow(); + + if ( $isOperator ) { + $mo = new MMLmo(); + return $mrow->encapsulateRaw( $mo->encapsulateRaw( $char ) ); + } + + $mi = new MMLmi( "", [ "mathvariant" => "normal" ] ); + return $mrow->encapsulateRaw( $mi->encapsulateRaw( $char ) ); + } + public static function cFrac( $node, $passedArgs, $operatorContent, $name ) { $mrow = new MMLmrow(); $mfrac = new MMLmfrac(); diff --git a/tests/phpunit/unit/TexVC/Mhchem/MhchemBasicMMLTest.php b/tests/phpunit/unit/TexVC/Mhchem/MhchemBasicMMLTest.php index 7dadc7f9f..6f250e660 100644 --- a/tests/phpunit/unit/TexVC/Mhchem/MhchemBasicMMLTest.php +++ b/tests/phpunit/unit/TexVC/Mhchem/MhchemBasicMMLTest.php @@ -15,6 +15,39 @@ use MediaWikiUnitTestCase; */ final class MhchemBasicMMLTest extends MediaWikiUnitTestCase { + public static function provideTestCasesLetters() { + return [ + [ "Alpha" , "A" ], + [ "Beta" , "B" ] , + [ "Chi" , "X" ], + [ "Epsilon" , "E" ], + [ "Eta" , "H" ], + [ "Iota" , "I" ], + [ "Kappa" , "K" ], + [ "Mu" , "M" ], + [ "Nu" , "N" ], + [ "Omicron" , "O" ], + [ "Rho" , "P" ], + [ "Tau" , "T" ], + [ "Zeta" , "Z" ] + ]; + } + + /** + * @dataProvider provideTestCasesLetters + */ + public function testmhchemLetters( $case, $result ) { + $input = "\ce{\\" . $case . " \ca }"; + $texVC = new TexVC(); + $options = [ "usemhchem" => true, "usemhchemtexified" => true ]; + $warnings = []; + $res = $texVC->check( $input, $options, $warnings, true ); + $mml = $res['input']->renderMML(); + $this->assertStringContainsString( 'assertStringContainsString( $result . '', $mml ); + $this->assertStringContainsString( '', $mml ); + } + public function testHarpoonsLeftRight() { $input = "A \\longLeftrightharpoons L"; $texVC = new TexVC();