Support for chemical formulae

* Add support for LaTeX package mhchem.
* Add hook <ce>
* Add format <math chem>

See bug description for the details.

Bug: T98306
Change-Id: I2ab80834edc5d071618bc52f133cfb2b7ece5016
This commit is contained in:
physikerwelt 2016-01-29 14:05:54 +01:00 committed by Physikerwelt
parent 30cb42eab6
commit 47f530b355
4 changed files with 31 additions and 1 deletions

View File

@ -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 <ce> 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 );
}
}

View File

@ -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( '<math>' . $tex . '</math>' );
} elseif ( $params['type'] == 'ascii' ) {
$this->inputType = 'ascii';
} elseif ( $params['type'] == 'chem' ){
$this->inputType = 'chem';
}
}
if ( !isset( $params['display'] ) && $this->getMathStyle() == 'inlineDisplaystyle' ) {

View File

@ -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 );

View File

@ -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( '<msubsup>', $rbi->getMathML() );
$this->assertContains( '<mtext>H</mtext>', $rbi->getMathML() );
}
/**
* @expectedException MWException
* @expectedExceptionMessage TeX input is invalid.