From 47f530b355ce7c1a5199d5d7ea0afe8d3192c176 Mon Sep 17 00:00:00 2001 From: physikerwelt Date: Fri, 29 Jan 2016 14:05:54 +0100 Subject: [PATCH] Support for chemical formulae * Add support for LaTeX package mhchem. * Add hook * Add format See bug description for the details. Bug: T98306 Change-Id: I2ab80834edc5d071618bc52f133cfb2b7ece5016 --- Math.hooks.php | 15 +++++++++++++++ MathMathML.php | 4 +++- MathRenderer.php | 4 ++++ tests/MathRestBaseInterfaceTest.php | 9 +++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Math.hooks.php b/Math.hooks.php index 895e785..bd2d07b 100644 --- a/Math.hooks.php +++ b/Math.hooks.php @@ -165,6 +165,7 @@ class MathHooks { */ static function onParserFirstCallInit( $parser ) { $parser->setHook( 'math', array( 'MathHooks', 'mathTagHook' ) ); + $parser->setHook( 'ce', array( 'MathHooks', 'ceTagHook' ) ); return true; } @@ -371,4 +372,18 @@ class MathHooks { $wgMathDisableTexFilter = MathRenderer::getDisableTexFilter(); $wgDefaultUserOptions['math'] = self::mathModeToString( $wgDefaultUserOptions['math'] ); } + + /** + * Callback function for the parser hook. + * + * @param $content (the LaTeX input) + * @param $attributes + * @param Parser $parser + * @return array + */ + static function ceTagHook( $content, $attributes, $parser ) { + $attributes['chem'] = true; + return MathHooks::mathTagHook( '\ce{' . $content . '}', $attributes, $parser ); + } + } diff --git a/MathMathML.php b/MathMathML.php index 515925e..d3fcec6 100644 --- a/MathMathML.php +++ b/MathMathML.php @@ -14,7 +14,7 @@ use MediaWiki\Logger\LoggerFactory; class MathMathML extends MathRenderer { protected $defaultAllowedRootElements = array( 'math' ); - protected $restbaseInputTypes = array( 'tex', 'inline-tex' ); + protected $restbaseInputTypes = array( 'tex', 'inline-tex', 'chem' ); protected $allowedRootElements = ''; protected $hosts; @@ -33,6 +33,8 @@ class MathMathML extends MathRenderer { $this->setMathml( '' . $tex . '' ); } elseif ( $params['type'] == 'ascii' ) { $this->inputType = 'ascii'; + } elseif ( $params['type'] == 'chem' ){ + $this->inputType = 'chem'; } } if ( !isset( $params['display'] ) && $this->getMathStyle() == 'inlineDisplaystyle' ) { diff --git a/MathRenderer.php b/MathRenderer.php index e61a5b3..4da750d 100644 --- a/MathRenderer.php +++ b/MathRenderer.php @@ -157,6 +157,10 @@ abstract class MathRenderer { unset( $params['type'] ); } } + if ( isset( $params['chem'] ) ) { + $mode = 'mathml'; + $params['type'] = 'chem'; + } switch ( $mode ) { case 'source': $renderer = new MathSource( $tex, $params ); diff --git a/tests/MathRestBaseInterfaceTest.php b/tests/MathRestBaseInterfaceTest.php index 9bab719..e78e6e3 100644 --- a/tests/MathRestBaseInterfaceTest.php +++ b/tests/MathRestBaseInterfaceTest.php @@ -54,6 +54,15 @@ class MathRestbaseInterfaceTest extends MediaWikiTestCase { $this->assertEquals( 'Illegal TeX function', $rbi->getError()->error->message ); } + public function testChem() { + $input = '\ce{H2O}'; + $rbi = new MathRestbaseInterface( $input, 'chem' ); + $this->assertTrue( $rbi->checkTeX(), "Assuming that $input is valid input." ); + $this->assertTrue( $rbi->getSuccess(), "Assuming that $input is valid input." ); + $this->assertEquals( '{\ce {H2O}}', $rbi->getCheckedTex() ); + $this->assertContains( '', $rbi->getMathML() ); + $this->assertContains( 'H', $rbi->getMathML() ); + } /** * @expectedException MWException * @expectedExceptionMessage TeX input is invalid.