-
Notifications
You must be signed in to change notification settings - Fork 24
/
SimpleMathJaxHooks.php
42 lines (36 loc) · 1.83 KB
/
SimpleMathJaxHooks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
class SimpleMathJaxHooks {
public static function onParserFirstCallInit( Parser $parser ) {
global $wgOut, $wgSmjUseCdn, $wgSmjUseChem, $wgSmjEnableMenu,
$wgSmjDisplayMath, $wgSmjExtraInlineMath,
$wgSmjScale, $wgSmjDisplayAlign;
$wgOut->addJsConfigVars( 'wgSmjUseCdn', $wgSmjUseCdn );
$wgOut->addJsConfigVars( 'wgSmjUseChem', $wgSmjUseChem );
$wgOut->addJsConfigVars( 'wgSmjDisplayMath', $wgSmjDisplayMath );
$wgOut->addJsConfigVars( 'wgSmjExtraInlineMath', $wgSmjExtraInlineMath );
$wgOut->addJsConfigVars( 'wgSmjScale', $wgSmjScale );
$wgOut->addJsConfigVars( 'wgSmjEnableMenu', $wgSmjEnableMenu );
$wgOut->addJsConfigVars( 'wgSmjDisplayAlign', $wgSmjDisplayAlign );
$wgOut->addModules( [ 'ext.SimpleMathJax' ] );
$wgOut->addModules( [ 'ext.SimpleMathJax.mobile' ] ); // For MobileFrontend
$parser->setHook( 'math', __CLASS__ . '::renderMath' );
if( $wgSmjUseChem ) $parser->setHook( 'chem', __CLASS__ . '::renderChem' ); }
public static function renderMath($tex, array $args, Parser $parser, PPFrame $frame ) {
global $wgSmjWrapDisplaystyle;
$tex = str_replace('\>', '\;', $tex);
$tex = str_replace('<', '\lt ', $tex);
$tex = str_replace('>', '\gt ', $tex);
if( $wgSmjWrapDisplaystyle ) $tex = "\displaystyle{ $tex }";
return self::renderTex($tex, $parser);
}
public static function renderChem($tex, array $args, Parser $parser, PPFrame $frame ) {
return self::renderTex("\ce{ $tex }", $parser);
}
private static function renderTex($tex, $parser) {
$hookContainer = MediaWiki\MediaWikiServices::getInstance()->getHookContainer();
$attributes = [ "style" => "opacity:.5", "class" => "smj-container" ];
$hookContainer->run( "SimpleMathJaxAttributes", [ &$attributes, $tex ] );
$element = Html::Element( "span", $attributes, "[math]{$tex}[/math]" );
return [$element, 'markerType'=>'nowiki'];
}
}