2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * This filter provides automatic support for MathJax
20 * @package filter_mathjaxloader
21 * @copyright 2013 Damyon Wiese (damyon@moodle.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
30 class filter_mathjaxloader
extends moodle_text_filter
{
33 * Perform a mapping of the moodle language code to the equivalent for MathJax.
35 * @param string $moodlelangcode - The moodle language code - e.g. en_pirate
36 * @return string The MathJax language code.
38 public function map_language_code($moodlelangcode) {
39 $mathjaxlangcodes = array('br',
69 $exceptions = array('cz' => 'cs');
71 // First see if this is an exception.
72 if (isset($exceptions[$moodlelangcode])) {
73 $moodlelangcode = $exceptions[$moodlelangcode];
76 // Now look for an exact lang string match.
77 if (in_array($moodlelangcode, $mathjaxlangcodes)) {
78 return $moodlelangcode;
81 // Now try shortening the moodle lang string.
82 $moodlelangcode = preg_replace('/-.*/', '', $moodlelangcode);
83 // Look for a match on the shortened string.
84 if (in_array($moodlelangcode, $mathjaxlangcodes)) {
85 return $moodlelangcode;
87 // All failed - use english.
92 * Add the javascript to enable mathjax processing on this page.
94 * @param moodle_page $page The current page.
95 * @param context $context The current context.
97 public function setup($page, $context) {
98 // This only requires execution once per request.
99 static $jsinitialised = false;
101 if (empty($jsinitialised)) {
103 $url = get_config('filter_mathjaxloader', 'httpsurl');
105 $url = get_config('filter_mathjaxloader', 'httpurl');
107 $lang = $this->map_language_code(current_language());
108 $url = new moodle_url($url, array('delayStartupUntil' => 'configured'));
110 $moduleconfig = array(
115 $page->requires
->js_module($moduleconfig);
117 $config = get_config('filter_mathjaxloader', 'mathjaxconfig');
119 $params = array('mathjaxconfig' => $config, 'lang' => $lang);
121 $page->requires
->yui_module('moodle-filter_mathjaxloader-loader', 'M.filter_mathjaxloader.configure', array($params));
123 $jsinitialised = true;
128 * This function wraps the filtered text in a span, that mathjaxloader is configured to process.
130 * @param string $text The text to filter.
131 * @param array $options The filter options.
133 public function filter($text, array $options = array()) {
136 $legacy = get_config('filter_mathjaxloader', 'texfiltercompatibility');
137 $extradelimiters = explode(',', get_config('filter_mathjaxloader', 'additionaldelimiters'));
139 // This replaces any of the tex filter maths delimiters with the default for inline maths in MathJAX "\( blah \)".
140 // E.g. "<tex.*> blah </tex>".
141 $text = preg_replace('|<(/?) *tex( [^>]*)?>|u', '[\1tex]', $text);
142 // E.g. "[tex.*] blah [/tex]".
143 $text = str_replace('[tex]', '\\(', $text);
144 $text = str_replace('[/tex]', '\\)', $text);
145 // E.g. "$$ blah $$".
146 $text = preg_replace('|\$\$([\S\s]*?)\$\$|u', '\\(\1\\)', $text);
147 // E.g. "\[ blah \]".
148 $text = str_replace('\\[', '\\(', $text);
149 $text = str_replace('\\]', '\\)', $text);
152 $hasinline = strpos($text, '\\(') !== false && strpos($text, '\\)') !== false;
153 $hasdisplay = (strpos($text, '$$') !== false) ||
154 (strpos($text, '\\[') !== false && strpos($text, '\\]') !== false);
158 foreach ($extradelimiters as $extra) {
159 if ($extra && strpos($text, $extra) !== false) {
164 if ($hasinline ||
$hasdisplay ||
$hasextra) {
165 $PAGE->requires
->yui_module('moodle-filter_mathjaxloader-loader', 'M.filter_mathjaxloader.typeset');
166 return '<span class="nolink"><span class="filter_mathjaxloader_equation">' . $text . '</span></span>';