-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.php
More file actions
55 lines (47 loc) · 2.32 KB
/
Copy pathfilter.php
File metadata and controls
55 lines (47 loc) · 2.32 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
<?php
defined('MOODLE_INTERNAL') || die();
class filter_mathjax extends moodle_text_filter {
public function __construct($context, array $localconfig) {
global $PAGE, $CFG;
if (isset($PAGE)) {
if (file_exists($CFG->dirroot . '/filter/mathjax/js/MathJax.js') &&
file_exists($CFG->dirroot . '/filter/mathjax/js/config') &&
file_exists($CFG->dirroot . '/filter/mathjax/js/jax') &&
file_exists($CFG->dirroot . '/filter/mathjax/js/fonts') &&
file_exists($CFG->dirroot . '/filter/mathjax/js/images') &&
file_exists($CFG->dirroot . '/filter/mathjax/js/extensions')) {
$mathjaxpath = '/filter/mathjax/js';
} else if (substr($PAGE->url, 0, 6) === 'https:') {
if (!empty($CFG->filter_mathjax_distroothttps)) {
$mathjaxpath = $CFG->filter_mathjax_distroothttps;
} else {
$mathjaxpath = 'https://c328740.ssl.cf1.rackcdn.com/mathjax/latest';
}
} else {
if (!empty($CFG->filter_mathjax_distroothttp)) {
$mathjaxpath = $CFG->filter_mathjax_distroothttp;
} else {
$mathjaxpath = 'http://cdn.mathjax.org/mathjax/latest';
}
}
$url = new moodle_url($mathjaxpath . '/MathJax.js',
array('config' => 'TeX-AMS-MML_HTMLorMML',
'delayStartupUntil' => 'configured'));
$PAGE->requires->js($url);
$PAGE->requires->js_init_call('M.filter_mathjax.init',
array('mathjaxroot' => (string)(new moodle_url($mathjaxpath))));
}
parent::__construct($context, $localconfig);
}
public function filter($text, array $options = array()) {
// The presence of these indicates MathJax ought to process the block:
// inline: \( ... \)
// block: $$ ... $$, \[ ... \]
// tex environments: \begin{...} ... \end{...}
// mathml: <math> ... </math>
if (preg_match('/\$\$.+?\$\$|\\\\\\[.+?\\\\\\]|\\\\\\(.+?\\\\\\)|\\\\begin\\{|<math/s', $text)) {
return '<div class="filter-mathjax">'.$text.'</div>';
}
return $text;
}
}