Merge "Support for chemical formulae"

This commit is contained in:
Mobrovac 2016-02-08 19:52:09 +00:00 committed by Gerrit Code Review
commit b144031847
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.