Skip to content

Commit

Permalink
MDL-43856 MathJax: Improvements to the MathJax filter
Browse files Browse the repository at this point in the history
* Remove all traces of mathjax from the atto equation editor - it is filter agnostic
* Add a M.core.event module for core events that can be published/subscribed to by plugins (loose coupling)
* Add a FILTER_CONTENT_UPDATED event so plugins can tell the filter system when nodes are added to the page
* Implement a listener for this event in the MathJax filter
* Fire this event from the equation editor and from the glossary filter
* Add a nolink tag to the mathjax filter, so equations will not be processed by the other filters
* Add a smarter throttle function to the equation editor preview updates (Y.throttle is dumb)
  • Loading branch information
Damyon Wiese committed Apr 3, 2014
1 parent 289ed25 commit cc90ced
Show file tree
Hide file tree
Showing 26 changed files with 662 additions and 246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ Y.extend(AUTOLINKER, Y.Base, {
alertpanel = new M.core.alert({title:data.entries[key].concept,
message:definition, modal:false, yesLabel: M.util.get_string('ok', 'moodle')});
alertpanel.show();
Y.fire(M.core.event.FILTER_CONTENT_UPDATED, {nodes: (new Y.NodeList(alertpanel.get('boundingBox')))});

Y.Node.one('#id_yuialertconfirm-' + alertpanel.get('COUNT')).focus();
}

Expand Down Expand Up @@ -141,6 +143,7 @@ M.filter_glossary.init_filter_autolinking = function(config) {
"json-parse",
"event-delegate",
"overlay",
"moodle-core-event",
"moodle-core-notification-alert"
]
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ Y.extend(AUTOLINKER, Y.Base, {
alertpanel = new M.core.alert({title:data.entries[key].concept,
message:definition, modal:false, yesLabel: M.util.get_string('ok', 'moodle')});
alertpanel.show();
Y.fire(M.core.event.FILTER_CONTENT_UPDATED, {nodes: (new Y.NodeList(alertpanel.get('boundingBox')))});

Y.Node.one('#id_yuialertconfirm-' + alertpanel.get('COUNT')).focus();
}

Expand Down Expand Up @@ -141,6 +143,7 @@ M.filter_glossary.init_filter_autolinking = function(config) {
"json-parse",
"event-delegate",
"overlay",
"moodle-core-event",
"moodle-core-notification-alert"
]
});
2 changes: 2 additions & 0 deletions filter/glossary/yui/src/autolinker/js/autolinker.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Y.extend(AUTOLINKER, Y.Base, {
alertpanel = new M.core.alert({title:data.entries[key].concept,
message:definition, modal:false, yesLabel: M.util.get_string('ok', 'moodle')});
alertpanel.show();
Y.fire(M.core.event.FILTER_CONTENT_UPDATED, {nodes: (new Y.NodeList(alertpanel.get('boundingBox')))});

Y.Node.one('#id_yuialertconfirm-' + alertpanel.get('COUNT')).focus();
}

Expand Down
1 change: 1 addition & 0 deletions filter/glossary/yui/src/autolinker/meta/autolinker.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"json-parse",
"event-delegate",
"overlay",
"moodle-core-event",
"moodle-core-notification-alert"
]
}
Expand Down
25 changes: 20 additions & 5 deletions filter/mathjaxloader/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,29 @@ public function lazy_init() {
* @param array $options The filter options.
*/
public function filter($text, array $options = array()) {
// This replaces <tex> blah </tex> syntax with [tex] blah [/tex] syntax
// because MathJax cannot handle html tags as delimiters.
$legacy = get_config('filter_mathjaxloader', 'texfiltercompatibility');
if ($legacy) {
// This replaces any of the tex filter maths delimiters with the default for inline maths in MathJAX "\( blah \)".
// <tex.*> blah </tex>
$text = preg_replace('|<(/?) *tex( [^>]*)?>|u', '[\1tex]', $text);
// [tex.*] blah [/tex]
$text = str_replace('[tex]', '\\(', $text);
$text = str_replace('[/tex]', '\\)', $text);
// $$ blah $$
$text = preg_replace('|\$\$[\S\s]\$\$|u', '\\(\1\\)', $text);
// \[ blah \]
$text = str_replace('\\[', '\\(', $text);
$text = str_replace('\\]', '\\)', $text);
}

$hasinline = strpos($text, '\\(') !== false && strpos($text, '\\)') !== false;
$hasdisplay = (strpos($text, '$$') !== false) ||
(strpos($text, '\\[') !== false && strpos($text, '\\]') !== false);

$text = preg_replace('|<(/?) *tex( [^>]*)?>|u', '[\1tex]', $text);
if (strpos($text, '$$') !== false || strpos($text, '\\[') !== false || strpos($text, '[tex]') !== false) {
if ($hasinline || $hasdisplay) {
// Only call init if there is at least one equation on the page.
$this->lazy_init();
return '<span class="filter_mathjaxloader_equation">' . $text . '</span>';
return '<span class="nolink"><span class="filter_mathjaxloader_equation">' . $text . '</span></span>';
}
return $text;
}
Expand Down
14 changes: 11 additions & 3 deletions filter/mathjaxloader/lang/en/filter_mathjaxloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@
$string['filtername'] = 'MathJax';
$string['httpurl'] = 'HTTP MathJax URL';
$string['httpurl_help'] = 'Full URL to MathJax library. Used when the page is loaded via http.';
$string['httpsurl'] = 'HTTP MathJax URL';
$string['httpsurl'] = 'HTTPS MathJax URL';
$string['httpsurl_help'] = 'Full URL to MathJax library. Used when the page is loaded via https (secure). ';
$string['texfiltercompatibility'] = 'Tex filter compatibility';
$string['texfiltercompatibility_help'] = 'The MathJax filter can be used as a replacement for the Tex filter.
To support all the delimiters supported by the Tex filter MathJax will be configured to display all equations "inline" with the tex.';
$string['localinstall'] = 'Local MathJax installation';
$string['localinstall_help'] = 'The default MathJAX configuration uses the CDN version of MathJAX, but MathJAX can be installed locally if required. Some reasons this might be useful are to save on bandwidth - or because of local proxy restrictions. To use a local installation of MathJAX, first download the full MathJax library from http://www.mathjax.org/. Then install it on a web server. Finally update the MathJax filter settings httpurl and/or httpsurl to point to the local MathJax.js url.';
$string['localinstall_help'] = 'The default MathJAX configuration uses the CDN version of MathJAX, but MathJAX can be installed locally if required.
Some reasons this might be useful are to save on bandwidth - or because of local proxy restrictions.
To use a local installation of MathJAX, first download the full MathJax library from http://www.mathjax.org/. Then install it on a web server. Finally update the MathJax filter settings httpurl and/or httpsurl to point to the local MathJax.js url.';
$string['mathjaxsettings'] = 'MathJax configuration';
$string['mathjaxsettings_desc'] = 'The default MathJAX configuration should be appropriate for most users, but MathJax is highly configurable and advanced users may want to provide a different configuration. More information on MathJax configuration options is available here: http://docs.mathjax.org/en/latest/options/index.html';
$string['mathjaxsettings_desc'] = 'The default MathJAX configuration should be appropriate for most users, but MathJax is highly configurable and any of the standard MathJax configuration options can be added here.';

19 changes: 6 additions & 13 deletions filter/mathjaxloader/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
PARAM_RAW);
$settings->add($item);

$item = new admin_setting_configcheckbox('filter_mathjaxloader/texfiltercompatibility',
new lang_string('texfiltercompatibility', 'filter_mathjaxloader'),
new lang_string('texfiltercompatibility_help', 'filter_mathjaxloader'),
0);
$settings->add($item);

$default = '
MathJax.Hub.Config({
config: ["MMLorHTML.js", "Safe.js"],
Expand All @@ -58,19 +64,6 @@
mpMouse: true
},
errorSettings: { message: ["!"] },
tex2jax: {
skipTags: ["script","noscript","style","textarea"],
inlineMath: [
["\\\\(","\\\\)"],
["$$","$$"],
["\\\\[","\\\\]"]
],
displayMath: [
["[tex]","[/tex]"],
],
balanceBraces: true,
preview: "TeX"
},
skipStartupTypeset: true,
messageStyle: "none"
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,26 @@ M.filter_mathjaxloader = M.filter_mathjaxloader || {
MathJax.Hub.Queue(["Typeset",MathJax.Hub,node.getDOMNode()]);
});
MathJax.Hub.Configured();

// Listen for events triggered when new text is added to a page that needs
// processing by a filter.

Y.on(M.core.event.FILTER_CONTENT_UPDATED, this.contentUpdated, this);
},

/**
* Handle content updated events - typeset the new content.
* @method contentUpdated
* @param Y.Event - Custom event with "nodes" indicating the root of the updated nodes.
*/
contentUpdated: function(event) {
event.nodes.each(function (node) {
node.all('.filter_mathjaxloader_equation').each(function(node) {
MathJax.Hub.Queue(["Typeset",MathJax.Hub,node.getDOMNode()]);
});
});
}
};


}, '@VERSION@', {"requires": ["mathjax"]});
}, '@VERSION@', {"requires": ["moodle-core-event", "mathjax"]});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,26 @@ M.filter_mathjaxloader = M.filter_mathjaxloader || {
MathJax.Hub.Queue(["Typeset",MathJax.Hub,node.getDOMNode()]);
});
MathJax.Hub.Configured();

// Listen for events triggered when new text is added to a page that needs
// processing by a filter.

Y.on(M.core.event.FILTER_CONTENT_UPDATED, this.contentUpdated, this);
},

/**
* Handle content updated events - typeset the new content.
* @method contentUpdated
* @param Y.Event - Custom event with "nodes" indicating the root of the updated nodes.
*/
contentUpdated: function(event) {
event.nodes.each(function (node) {
node.all('.filter_mathjaxloader_equation').each(function(node) {
MathJax.Hub.Queue(["Typeset",MathJax.Hub,node.getDOMNode()]);
});
});
}
};


}, '@VERSION@', {"requires": ["mathjax"]});
}, '@VERSION@', {"requires": ["moodle-core-event", "mathjax"]});
18 changes: 18 additions & 0 deletions filter/mathjaxloader/yui/src/loader/js/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,23 @@ M.filter_mathjaxloader = M.filter_mathjaxloader || {
MathJax.Hub.Queue(["Typeset",MathJax.Hub,node.getDOMNode()]);
});
MathJax.Hub.Configured();

// Listen for events triggered when new text is added to a page that needs
// processing by a filter.

Y.on(M.core.event.FILTER_CONTENT_UPDATED, this.contentUpdated, this);
},

/**
* Handle content updated events - typeset the new content.
* @method contentUpdated
* @param Y.Event - Custom event with "nodes" indicating the root of the updated nodes.
*/
contentUpdated: function(event) {
event.nodes.each(function (node) {
node.all('.filter_mathjaxloader_equation').each(function(node) {
MathJax.Hub.Queue(["Typeset",MathJax.Hub,node.getDOMNode()]);
});
});
}
};
1 change: 1 addition & 0 deletions filter/mathjaxloader/yui/src/loader/meta/loader.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"moodle-filter_mathjaxloader-loader": {
"requires": [
"moodle-core-event",
"mathjax"
]
}
Expand Down
6 changes: 4 additions & 2 deletions lib/editor/atto/plugins/equation/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ function atto_equation_params_for_js($elementid, $options, $fpoptions) {
'elements' => get_config('atto_equation', 'librarygroup4'),
));

return array('texfilteractive' => $texfilteractive, 'contextid' => $context->id, 'library' => $library,
'texdocsurl' => get_docs_url('Using_TeX_Notation'));
return array('texfilteractive' => $texfilteractive,
'contextid' => $context->id,
'library' => $library,
'texdocsurl' => get_docs_url('Using_TeX_Notation'));
}
1 change: 1 addition & 0 deletions lib/editor/atto/plugins/equation/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,5 @@
$desc,
$default);
$settings->add($setting);

}
Loading

0 comments on commit cc90ced

Please sign in to comment.