mediawiki-extensions-Simple.../SimpleMathJaxHooks.php

42 行
1.7 KiB
PHP

<?php
2020-10-23 18:25:34 +08:00
class SimpleMathJaxHooks {
2017-06-12 01:11:02 +08:00
2020-10-23 18:25:34 +08:00
public static function onParserFirstCallInit( Parser $parser ) {
2020-10-19 04:10:34 +08:00
global $wgOut, $wgSmjUseCdn, $wgSmjUseChem, $wgSmjEnableMenu,
$wgSmjDisplayMath, $wgSmjExtraInlineMath,
$wgSmjScale, $wgSmjDisplayAlign;
2020-10-19 02:43:59 +08:00
$wgOut->addJsConfigVars( 'wgSmjUseCdn', $wgSmjUseCdn );
2017-12-28 21:07:42 +08:00
$wgOut->addJsConfigVars( 'wgSmjUseChem', $wgSmjUseChem );
2020-10-19 02:43:59 +08:00
$wgOut->addJsConfigVars( 'wgSmjDisplayMath', $wgSmjDisplayMath );
2020-10-19 03:35:17 +08:00
$wgOut->addJsConfigVars( 'wgSmjExtraInlineMath', $wgSmjExtraInlineMath );
$wgOut->addJsConfigVars( 'wgSmjScale', $wgSmjScale );
2020-10-23 18:25:34 +08:00
$wgOut->addJsConfigVars( 'wgSmjEnableMenu', $wgSmjEnableMenu );
2020-10-19 04:10:34 +08:00
$wgOut->addJsConfigVars( 'wgSmjDisplayAlign', $wgSmjDisplayAlign );
$wgOut->addModules( [ 'ext.SimpleMathJax' ] );
$wgOut->addModules( [ 'ext.SimpleMathJax.mobile' ] ); // For MobileFrontend
2017-06-12 01:11:02 +08:00
$parser->setHook( 'math', __CLASS__ . '::renderMath' );
2020-10-23 18:25:34 +08:00
if( $wgSmjUseChem ) $parser->setHook( 'chem', __CLASS__ . '::renderChem' ); }
2017-10-22 01:03:25 +08:00
public static function renderMath($tex, array $args, Parser $parser, PPFrame $frame ) {
2020-10-23 18:25:34 +08:00
global $wgSmjWrapDisplaystyle;
2017-06-12 01:11:02 +08:00
$tex = str_replace('\>', '\;', $tex);
$tex = str_replace('<', '\lt ', $tex);
$tex = str_replace('>', '\gt ', $tex);
if( $wgSmjWrapDisplaystyle ) $tex = "\displaystyle{ $tex }";
2017-10-22 01:03:25 +08:00
return self::renderTex($tex, $parser);
2017-06-12 01:11:02 +08:00
}
2017-10-22 01:03:25 +08:00
public static function renderChem($tex, array $args, Parser $parser, PPFrame $frame ) {
2020-10-23 18:25:34 +08:00
return self::renderTex("\ce{ $tex }", $parser);
2017-06-12 01:11:02 +08:00
}
2017-10-22 01:03:25 +08:00
private static function renderTex($tex, $parser) {
2020-11-11 11:19:14 +08:00
$attributes = [ "style" => "opacity:.5" ];
2020-11-16 08:05:04 +08:00
Hooks::run( "SimpleMathJaxAttributes", [ &$attributes, $tex ] );
2020-11-11 11:19:14 +08:00
$element = Html::Element( "span", $attributes, "[math]{$tex}[/math]" );
return [$element, 'markerType'=>'nowiki'];
2017-06-12 01:11:02 +08:00
}
}