-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathSimpleMathJaxHooks.php
More file actions
106 lines (94 loc) · 4.29 KB
/
SimpleMathJaxHooks.php
File metadata and controls
106 lines (94 loc) · 4.29 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
use MediaWiki\Html\Html;
use MediaWiki\Parser\Sanitizer;
class SimpleMathJaxHooks {
private static $useChem;
private static $wrapDisplaystyle;
private static $enableHtmlAttributes;
public static function onParserFirstCallInit( Parser $parser ) {
global $wgOut, $wgSmjUseCdn, $wgSmjUseChem, $wgSmjDirectMathJax, $wgSmjEnableMenu,
$wgSmjDisplayMath, $wgSmjExtraInlineMath, $wgSmjIgnoreHtmlClass,
$wgSmjScale, $wgSmjDisplayAlign, $wgSmjWrapDisplaystyle,
$wgSmjEnableHtmlAttributes, $wgSmjConfigByRevision;
$globalvars = [ "wgSmjUseCdn", "wgSmjDirectMathJax",
"wgSmjDisplayMath", "wgSmjExtraInlineMath", "wgSmjIgnoreHtmlClass",
"wgSmjScale", "wgSmjEnableMenu", "wgSmjDisplayAlign" ];
foreach( $globalvars as $varname ) {
$wgOut->addJsConfigVars( $varname, $$varname );
}
self::$useChem = $wgSmjUseChem;
self::$wrapDisplaystyle = $wgSmjWrapDisplaystyle;
self::$enableHtmlAttributes = $wgSmjEnableHtmlAttributes;
$articlerev = (int)$wgOut->getRevisionId();
foreach ($wgSmjConfigByRevision as $confset) {
if ($articlerev == 0) break;
if (!isset($confset["upto"]) && !isset($confset["since"])) continue;
if (isset($confset["upto"]) && $confset["upto"] < $articlerev) continue;
if (isset($confset["since"]) && $confset["since"] > $articlerev) continue;
foreach( $globalvars as $varname ) {
if( isset($confset[$varname]) ) $wgOut->addJsConfigVars( $varname, $confset[$varname] );
}
if (isset($confset["wgSmjUseChem"]) ) self::$useChem = $confset["wgSmjUseChem"];
if (isset($confset["wgSmjWrapDisplaystyle"]) ) self::$wrapDisplaystyle = $confset["wgSmjWrapDisplaystyle"];
if (isset($confset["wgSmjEnableHtmlAttributes"]) ) self::$enableHtmlAttributes = $confset["wgSmjEnableHtmlAttributes"];
}
$wgOut->addModules( [ 'ext.SimpleMathJax' ] );
$wgOut->addModules( [ 'ext.SimpleMathJax.mobile' ] ); // For MobileFrontend
$parser->setHook( 'math', __CLASS__ . '::renderMath' );
if( self::$useChem ) $parser->setHook( 'chem', __CLASS__ . '::renderChem' );
}
public static function renderMath($tex, array $args, Parser $parser, PPFrame $frame ) {
global $wgOut;
if( !self::$enableHtmlAttributes ) $args = [];
if( isset($args["chem"]) ) {
$wgOut->addJsConfigVars( "wgSmjPreloadChem", true );
}
if( isset($args["inline-block"]) ) {
if( isset($args["display"]) ) {
return self::renderError('SimpleMathJax: Do not use the inline-block attribute and the display attribute together on the same element.');
}
$tex = "\\displaystyle{ $tex }";
} else if( !isset($args["display"]) ) {
if( self::$wrapDisplaystyle ) $tex = "\\displaystyle{ $tex }";
} else switch ($args["display"]) {
case "":
break;
case "inline":
$tex = "\\textstyle{ $tex }";
break;
case "block":
break;
default:
return self::renderError('SimpleMathJax: Invalid attribute value: display="' . $args["display"] . '"');
}
return self::renderTex($tex, $parser, $args);
}
public static function renderChem($tex, array $args, Parser $parser, PPFrame $frame ) {
global $wgOut;
$wgOut->addJsConfigVars( "wgSmjPreloadChem", true );
if( !self::$enableHtmlAttributes ) $args = [];
return self::renderTex("\\ce{ $tex }", $parser, $args);
}
private static function renderTex($tex, $parser, $args) {
$hookContainer = MediaWiki\MediaWikiServices::getInstance()->getHookContainer();
$attributes = [ "style" => "opacity:.5", "class" => "" ];
$inherit_tags = [ "class", "id", "title", "lang", "dir" ];
$validatedAttribs = Sanitizer::validateAttributes( $args, array_fill_keys( $inherit_tags, true ) );
$attributes = array_merge( $attributes, $validatedAttribs );
$hookContainer->run( "SimpleMathJaxAttributes", [ &$attributes, $tex, $args ] );
if( !isset($attributes["smj-debug"]) && !isset($args["smj-debug"]) ) {
$attributes["class"] .= " smj-container";
}
if( isset($args["display"]) && $args["display"] == "block" ) {
$element = Html::Element( "span", $attributes, "\\begin{displaymjx}{$tex}\\end{displaymjx}" );
} else {
$element = Html::Element( "span", $attributes, "[math]{$tex}[/math]" );
}
return [$element, 'markerType'=>'nowiki'];
}
private static function renderError($str) {
$attributes = [ "class" => "error texerror" ];
$element = Html::Element( "strong", $attributes, $str );
return [$element, 'markerType'=>'nowiki'];
}
}