-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathSimpleMathJaxHooks.php
More file actions
121 lines (107 loc) · 4.68 KB
/
Copy pathSimpleMathJaxHooks.php
File metadata and controls
121 lines (107 loc) · 4.68 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?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;
$config = [
"wgSmjUseCdn" => $wgSmjUseCdn,
"wgSmjUseChem" => $wgSmjUseChem,
"wgSmjDirectMathJax" => $wgSmjDirectMathJax,
"wgSmjDisplayMath" => $wgSmjDisplayMath,
"wgSmjExtraInlineMath" => $wgSmjExtraInlineMath,
"wgSmjIgnoreHtmlClass" => $wgSmjIgnoreHtmlClass,
"wgSmjScale" => $wgSmjScale,
"wgSmjEnableMenu" => $wgSmjEnableMenu,
"wgSmjDisplayAlign" => $wgSmjDisplayAlign,
"wgSmjWrapDisplaystyle" => $wgSmjWrapDisplaystyle,
"wgSmjEnableHtmlAttributes" => $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( array_keys( $config ) as $varname ) {
if( array_key_exists($varname, $confset) ) $config[$varname] = $confset[$varname];
}
}
$clientConfigVars = [ "wgSmjUseCdn", "wgSmjDirectMathJax",
"wgSmjDisplayMath", "wgSmjExtraInlineMath", "wgSmjIgnoreHtmlClass",
"wgSmjScale", "wgSmjEnableMenu", "wgSmjDisplayAlign" ];
foreach( $clientConfigVars as $varname ) {
$wgOut->addJsConfigVars( $varname, $config[$varname] );
}
self::$useChem = $config["wgSmjUseChem"];
self::$wrapDisplaystyle = $config["wgSmjWrapDisplaystyle"];
self::$enableHtmlAttributes = $config["wgSmjEnableHtmlAttributes"];
if ( $config["wgSmjDirectMathJax"] !== 'none' ) {
$wgOut->addModules( [ 'ext.SimpleMathJax' ] );
}
$parser->setHook( 'math', __CLASS__ . '::renderMath' );
if( self::$useChem ) $parser->setHook( 'chem', __CLASS__ . '::renderChem' );
}
public static function renderMath($tex, array $args, Parser $parser, PPFrame $frame ) {
$parserOutput = $parser->getOutput();
$parserOutput->addModules( [ 'ext.SimpleMathJax' ] );
if( !self::$enableHtmlAttributes ) $args = [];
if( isset($args["chem"]) ) {
$parserOutput->setJsConfigVar( "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 ) {
$parserOutput = $parser->getOutput();
$parserOutput->addModules( [ 'ext.SimpleMathJax' ] );
$parserOutput->setJsConfigVar( "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'];
}
}