This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
forked from hbshim/mediawiki-mathjax
/
MathJax.php
207 lines (151 loc) · 6.05 KB
/
MathJax.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
use Nette\Utils\Html as NHtml;
use Nette\Utils\Strings as NStrings;
if (!defined('MEDIAWIKI')) {
die('This is a mediawiki extensions and can\'t be run from the command line.');
}
class MathJax_Parser
{
const MARKER_PREFIX = "\x7fMATH-";
const MARKER_SUFFIX = "-HTAM\x7f";
public static $defaultMathJaxPath = '/resources/lib/MathJax/MathJax.js';
public static $defaultMathJaxConfig = 'TeX-AMS-MML_HTMLorMML-full';
public static $defaultCustomConfig = '/resources/src/mathjax-config.js';
private static $markersCounter = 0;
private static $makers = [];
private static $jsInserted = false;
public static function parserInit(Parser $parser)
{
Hooks::register('ParserBeforeStrip', self::class . '::markBlockMathTags');
$parser->setHook('math', self::class . '::mathTag');
$parser->setHook('nomathjax', self::class . '::noMathJaxTag');
Hooks::register('ParserAfterTidy', self::class . '::afterTidy');
Hooks::register('BeforePageDisplay', self::class . '::Inject_JS');
return true;
}
public static function markBlockMathTags(Parser $parser, &$text)
{
$text = NStrings::replace($text, '~:\\<math([^\\>]*)\\>~', '<math display="block"$1>');
return true;
}
public static function mathTag($content, array $attrs, Parser $parser, PPFrame $frame)
{
$math = self::preProcessLinks($content, $parser, $frame);
$script = NHtml::el('script', ['type' => 'math/tex'])
->setHtml($math);
$wrapper = 'span';
if (!empty($attrs['display']) && $attrs['display'] === 'block') {
$script->attrs['type'] .= '; mode=display';
$wrapper = 'div';
}
if (self::containsXss($script)) {
return '<span style="color:red">Invalid latex syntax</span>';
}
if (!empty($attrs['id'])) {
$script = NHtml::el($wrapper, ['id' => 'equation-' . $attrs['id'], 'class' => 'math-equation'])
->add($script);
}
self::$makers[++self::$markersCounter] = (string) $script;
return [
Parser::MARKER_PREFIX . 'mathjax-' . self::$markersCounter . Parser::MARKER_PREFIX, // content
'markerType' => 'nowiki'
];
}
public static function noMathJaxTag($text, array $args, Parser $parser, PPFrame $frame)
{
$output = $parser->recursiveTagParse($text, $frame);
return '<span class="tex2jax_ignore">' . $output . '</span>';
}
public static function afterTidy(Parser $parser, &$text)
{
$text = NStrings::replace($text, '~' . preg_quote(Parser::MARKER_PREFIX) . 'mathjax\\-(?P<id>\\d+)' . preg_quote(Parser::MARKER_PREFIX) . '~', function (array $match) {
return self::$makers[$match['id']];
});
return true;
}
public static function Inject_JS(OutputPage $out)
{
global $wgMathJaxJS, $wgMathJaxProcConf, $wgMathJaxLocConf;
if (self::$jsInserted === true) {
return true;
}
$userConfig = ($wgMathJaxLocConf ?: self::$defaultCustomConfig);
$configContents = file_get_contents(__DIR__ . '/../../public' . $userConfig);
$out->addScript(\Html::rawElement(
'script',
['type' => 'text/x-mathjax-config'],
"\n" . $configContents . "\n"
));
$file = $wgMathJaxJS ?: self::$defaultMathJaxPath;
$config = ($wgMathJaxProcConf ?: self::$defaultMathJaxConfig);
$url = $file . '?' . http_build_query(['config' => $config], NULL, '&');
$out->addScript(Html::linkedScript($url));
self::$jsInserted = true;
return true;
}
/**
* @param string $content
* @param \Parser $parser
* @param \PPFrame $frame
* @return string
*/
public static function preProcessLinks($content, Parser $parser, PPFrame $frame)
{
return NStrings::replace($content, '~(?:(?<!\\\\)|^)\\\\wikiref\\{(?P<link>[^\\}]+)\\}~', function (array $m) use ($parser) {
$html = $parser->recursiveTagParseFully(sprintf('{{canonicalurl:%s}}', ltrim($m['link'], ':')));
if (!$doc = self::createDomFromHtml($html)) {
return $m[0];
}
$links = (new DOMXpath($doc))->query('//a[@href]/@href');
return $links->length !== 1
? $m[0]
: sprintf('\\href{%s}{\\text{#}}', $links->item(0)->value);
});
}
private static function containsXss(NHtml $script)
{
if (!NStrings::checkEncoding($script->getHtml())) {
return true;
}
if (!$dom = self::createDomFromHtml($script)) {
return true;
}
$scripts = $dom->getElementsByTagName('script');
if ($scripts->length > 1) {
return true;
}
$filteredContent = $scripts->item(0)->textContent;
if ($script->getHtml() !== $filteredContent) {
return true;
}
return false;
}
/**
* @param string $html
* @return \DOMDocument|null
*/
private static function createDomFromHtml($html)
{
$dom = new DOMDocument();
$dom->preserveWhiteSpace = true;
$dom->resolveExternals = false;
$dom->strictErrorChecking = false;
$dom->recover = true;
libxml_clear_errors();
$previousInternalErrors = libxml_use_internal_errors(true);
$previousEntityLoader = libxml_disable_entity_loader();
$result = $dom->loadHTML(self::prepareHtmlDocument($html));
libxml_disable_entity_loader($previousEntityLoader);
$htmlErrors = libxml_get_errors();
libxml_use_internal_errors($previousInternalErrors);
libxml_clear_errors();
return $result && !$htmlErrors ? $dom : null;
}
private static function prepareHtmlDocument($script)
{
return '<!doctype html><html>'
. '<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head>'
. '<body>' . $script . '</body>'
. '</html>';
}
}