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:
parent
30cb42eab6
commit
47f530b355
@ -165,6 +165,7 @@ class MathHooks {
|
|||||||
*/
|
*/
|
||||||
static function onParserFirstCallInit( $parser ) {
|
static function onParserFirstCallInit( $parser ) {
|
||||||
$parser->setHook( 'math', array( 'MathHooks', 'mathTagHook' ) );
|
$parser->setHook( 'math', array( 'MathHooks', 'mathTagHook' ) );
|
||||||
|
$parser->setHook( 'ce', array( 'MathHooks', 'ceTagHook' ) );
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -371,4 +372,18 @@ class MathHooks {
|
|||||||
$wgMathDisableTexFilter = MathRenderer::getDisableTexFilter();
|
$wgMathDisableTexFilter = MathRenderer::getDisableTexFilter();
|
||||||
$wgDefaultUserOptions['math'] = self::mathModeToString( $wgDefaultUserOptions['math'] );
|
$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 );
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ use MediaWiki\Logger\LoggerFactory;
|
|||||||
class MathMathML extends MathRenderer {
|
class MathMathML extends MathRenderer {
|
||||||
|
|
||||||
protected $defaultAllowedRootElements = array( 'math' );
|
protected $defaultAllowedRootElements = array( 'math' );
|
||||||
protected $restbaseInputTypes = array( 'tex', 'inline-tex' );
|
protected $restbaseInputTypes = array( 'tex', 'inline-tex', 'chem' );
|
||||||
protected $allowedRootElements = '';
|
protected $allowedRootElements = '';
|
||||||
protected $hosts;
|
protected $hosts;
|
||||||
|
|
||||||
@ -33,6 +33,8 @@ class MathMathML extends MathRenderer {
|
|||||||
$this->setMathml( '<math>' . $tex . '</math>' );
|
$this->setMathml( '<math>' . $tex . '</math>' );
|
||||||
} elseif ( $params['type'] == 'ascii' ) {
|
} elseif ( $params['type'] == 'ascii' ) {
|
||||||
$this->inputType = 'ascii';
|
$this->inputType = 'ascii';
|
||||||
|
} elseif ( $params['type'] == 'chem' ){
|
||||||
|
$this->inputType = 'chem';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ( !isset( $params['display'] ) && $this->getMathStyle() == 'inlineDisplaystyle' ) {
|
if ( !isset( $params['display'] ) && $this->getMathStyle() == 'inlineDisplaystyle' ) {
|
||||||
|
@ -157,6 +157,10 @@ abstract class MathRenderer {
|
|||||||
unset( $params['type'] );
|
unset( $params['type'] );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if ( isset( $params['chem'] ) ) {
|
||||||
|
$mode = 'mathml';
|
||||||
|
$params['type'] = 'chem';
|
||||||
|
}
|
||||||
switch ( $mode ) {
|
switch ( $mode ) {
|
||||||
case 'source':
|
case 'source':
|
||||||
$renderer = new MathSource( $tex, $params );
|
$renderer = new MathSource( $tex, $params );
|
||||||
|
@ -54,6 +54,15 @@ class MathRestbaseInterfaceTest extends MediaWikiTestCase {
|
|||||||
$this->assertEquals( 'Illegal TeX function', $rbi->getError()->error->message );
|
$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
|
* @expectedException MWException
|
||||||
* @expectedExceptionMessage TeX input is invalid.
|
* @expectedExceptionMessage TeX input is invalid.
|
||||||
|
Loading…
Reference in New Issue
Block a user