MDL-43856 MathJax: Coding style cleanups.

This commit is contained in:
Damyon Wiese 2014-04-03 14:26:56 +08:00
parent cc90cedc85
commit 441f94b263
15 changed files with 141 additions and 100 deletions

View File

@ -135,14 +135,14 @@ class filter_mathjaxloader extends moodle_text_filter {
$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>
// E.g. "<tex.*> blah </tex>".
$text = preg_replace('|<(/?) *tex( [^>]*)?>|u', '[\1tex]', $text);
// [tex.*] blah [/tex]
// E.g. "[tex.*] blah [/tex]".
$text = str_replace('[tex]', '\\(', $text);
$text = str_replace('[/tex]', '\\)', $text);
// $$ blah $$
// E.g. "$$ blah $$".
$text = preg_replace('|\$\$[\S\s]\$\$|u', '\\(\1\\)', $text);
// \[ blah \]
// E.g. "\[ blah \]".
$text = str_replace('\\[', '\\(', $text);
$text = str_replace('\\]', '\\)', $text);
}

View File

@ -32,15 +32,14 @@ $string['httpurl_help'] = 'Full URL to MathJax library. Used when the page is lo
$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.
$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.
$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.
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 any of the standard MathJax configuration options can be added here.';

View File

@ -26,8 +26,8 @@ defined('MOODLE_INTERNAL') || die;
if ($ADMIN->fulltree) {
$item = new admin_setting_heading('filter_mathjaxloader/localinstall',
new lang_string('localinstall', 'filter_mathjaxloader'),
new lang_string('localinstall_help', 'filter_mathjaxloader'));
new lang_string('localinstall', 'filter_mathjaxloader'),
new lang_string('localinstall_help', 'filter_mathjaxloader'));
$settings->add($item);
$item = new admin_setting_configtext('filter_mathjaxloader/httpurl',
@ -45,9 +45,9 @@ if ($ADMIN->fulltree) {
$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);
new lang_string('texfiltercompatibility', 'filter_mathjaxloader'),
new lang_string('texfiltercompatibility_help', 'filter_mathjaxloader'),
0);
$settings->add($item);
$default = '

View File

@ -35,13 +35,12 @@ M.filter_mathjaxloader = M.filter_mathjaxloader || {
MathJax.Localization.setLocale(params.lang);
Y.all('.filter_mathjaxloader_equation').each(function(node) {
MathJax.Hub.Queue(["Typeset",MathJax.Hub,node.getDOMNode()]);
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);
},
@ -53,7 +52,7 @@ M.filter_mathjaxloader = M.filter_mathjaxloader || {
contentUpdated: function(event) {
event.nodes.each(function (node) {
node.all('.filter_mathjaxloader_equation').each(function(node) {
MathJax.Hub.Queue(["Typeset",MathJax.Hub,node.getDOMNode()]);
MathJax.Hub.Queue(["Typeset", MathJax.Hub, node.getDOMNode()]);
});
});
}

View File

@ -35,13 +35,12 @@ M.filter_mathjaxloader = M.filter_mathjaxloader || {
MathJax.Localization.setLocale(params.lang);
Y.all('.filter_mathjaxloader_equation').each(function(node) {
MathJax.Hub.Queue(["Typeset",MathJax.Hub,node.getDOMNode()]);
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);
},
@ -53,7 +52,7 @@ M.filter_mathjaxloader = M.filter_mathjaxloader || {
contentUpdated: function(event) {
event.nodes.each(function (node) {
node.all('.filter_mathjaxloader_equation').each(function(node) {
MathJax.Hub.Queue(["Typeset",MathJax.Hub,node.getDOMNode()]);
MathJax.Hub.Queue(["Typeset", MathJax.Hub, node.getDOMNode()]);
});
});
}

View File

@ -33,13 +33,12 @@ M.filter_mathjaxloader = M.filter_mathjaxloader || {
MathJax.Localization.setLocale(params.lang);
Y.all('.filter_mathjaxloader_equation').each(function(node) {
MathJax.Hub.Queue(["Typeset",MathJax.Hub,node.getDOMNode()]);
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);
},
@ -51,7 +50,7 @@ M.filter_mathjaxloader = M.filter_mathjaxloader || {
contentUpdated: function(event) {
event.nodes.each(function (node) {
node.all('.filter_mathjaxloader_equation').each(function(node) {
MathJax.Hub.Queue(["Typeset",MathJax.Hub,node.getDOMNode()]);
MathJax.Hub.Queue(["Typeset", MathJax.Hub, node.getDOMNode()]);
});
});
}

View File

@ -71,9 +71,9 @@ if ($ADMIN->fulltree) {
\neq
';
$setting = new admin_setting_configtextarea('atto_equation/librarygroup1',
$name,
$desc,
$default);
$name,
$desc,
$default);
$settings->add($setting);
// Group 2
@ -96,9 +96,9 @@ if ($ADMIN->fulltree) {
\Leftrightarrow
';
$setting = new admin_setting_configtextarea('atto_equation/librarygroup2',
$name,
$desc,
$default);
$name,
$desc,
$default);
$settings->add($setting);
// Group 3
@ -141,9 +141,9 @@ if ($ADMIN->fulltree) {
\Omega
';
$setting = new admin_setting_configtextarea('atto_equation/librarygroup3',
$name,
$desc,
$default);
$name,
$desc,
$default);
$settings->add($setting);
// Group 4
@ -161,9 +161,9 @@ if ($ADMIN->fulltree) {
\left| \begin{matrix} a_1 & a_2 \\ a_3 & a_4 \end{matrix} \right|
';
$setting = new admin_setting_configtextarea('atto_equation/librarygroup4',
$name,
$desc,
$default);
$name,
$desc,
$default);
$settings->add($setting);
}

View File

@ -214,22 +214,21 @@ Y.namespace('M.atto_equation').Button = Y.Base.create('button', Y.M.editor_atto.
text = Y.one(selectedNode).get('text');
// We use space or not space because . does not match new lines.
// $$ blah $$
// $$ blah $$.
patterns.push(/\$\$([\S\s]*)\$\$/);
// \( blah \)
// E.g. "\( blah \)".
patterns.push(/\\\(([\S\s]*)\\\)/);
// \[ blah \]
// E.g. "\[ blah \]".
patterns.push(/\\\[([\S\s]*)\\\]/);
// [tex] blah [/tex]
// E.g. "[tex] blah [/tex]".
patterns.push(/\[tex\]([\S\s]*)\[\/tex\]/);
for (i = 0; i < patterns.length; i++) {
pattern = patterns[i];
equation = pattern.exec(text);
if (equation && equation.length) {
equation.shift();
// Remember the inner match so we can replace it later.
this.sourceEquation = equation = equation.shift();
this.sourceEquation = equation = equation[1];
return equation;
}
@ -286,10 +285,13 @@ Y.namespace('M.atto_equation').Button = Y.Base.create('button', Y.M.editor_atto.
/**
* Smart throttle, only call a function every delay milli seconds,
* and always run the last call.
* and always run the last call. Y.throttle does not work here,
* because it calls the function immediately, the first time, and then
* ignores repeated calls within X seconds. This does not guarantee
* that the last call will be executed (which is required here).
*
* @param {function} fn
* @param {Integer} delay - delay in milliseconds
* @param {Number} delay Delay in milliseconds
* @method _throttle
* @private
*/
@ -319,7 +321,8 @@ Y.namespace('M.atto_equation').Button = Y.Base.create('button', Y.M.editor_atto.
currentPos = textarea.get('selectionStart'),
prefix = '',
cursorLatex = '\\square ',
isChar;
isChar,
params;
if (e) {
e.preventDefault();
@ -352,8 +355,11 @@ Y.namespace('M.atto_equation').Button = Y.Base.create('button', Y.M.editor_atto.
text: equation
};
preview = Y.io(url, { sync: true,
data: params });
preview = Y.io(url, {
sync: true,
data: params
});
if (preview.status === 200) {
previewNode.setHTML(preview.responseText);
Y.fire(M.core.event.FILTER_CONTENT_UPDATED, {nodes: (new Y.NodeList(previewNode))});

View File

@ -214,22 +214,21 @@ Y.namespace('M.atto_equation').Button = Y.Base.create('button', Y.M.editor_atto.
text = Y.one(selectedNode).get('text');
// We use space or not space because . does not match new lines.
// $$ blah $$
// $$ blah $$.
patterns.push(/\$\$([\S\s]*)\$\$/);
// \( blah \)
// E.g. "\( blah \)".
patterns.push(/\\\(([\S\s]*)\\\)/);
// \[ blah \]
// E.g. "\[ blah \]".
patterns.push(/\\\[([\S\s]*)\\\]/);
// [tex] blah [/tex]
// E.g. "[tex] blah [/tex]".
patterns.push(/\[tex\]([\S\s]*)\[\/tex\]/);
for (i = 0; i < patterns.length; i++) {
pattern = patterns[i];
equation = pattern.exec(text);
if (equation && equation.length) {
equation.shift();
// Remember the inner match so we can replace it later.
this.sourceEquation = equation = equation.shift();
this.sourceEquation = equation = equation[1];
return equation;
}
@ -286,10 +285,13 @@ Y.namespace('M.atto_equation').Button = Y.Base.create('button', Y.M.editor_atto.
/**
* Smart throttle, only call a function every delay milli seconds,
* and always run the last call.
* and always run the last call. Y.throttle does not work here,
* because it calls the function immediately, the first time, and then
* ignores repeated calls within X seconds. This does not guarantee
* that the last call will be executed (which is required here).
*
* @param {function} fn
* @param {Integer} delay - delay in milliseconds
* @param {Number} delay Delay in milliseconds
* @method _throttle
* @private
*/
@ -319,7 +321,8 @@ Y.namespace('M.atto_equation').Button = Y.Base.create('button', Y.M.editor_atto.
currentPos = textarea.get('selectionStart'),
prefix = '',
cursorLatex = '\\square ',
isChar;
isChar,
params;
if (e) {
e.preventDefault();
@ -352,8 +355,11 @@ Y.namespace('M.atto_equation').Button = Y.Base.create('button', Y.M.editor_atto.
text: equation
};
preview = Y.io(url, { sync: true,
data: params });
preview = Y.io(url, {
sync: true,
data: params
});
if (preview.status === 200) {
previewNode.setHTML(preview.responseText);
Y.fire(M.core.event.FILTER_CONTENT_UPDATED, {nodes: (new Y.NodeList(previewNode))});

View File

@ -212,22 +212,21 @@ Y.namespace('M.atto_equation').Button = Y.Base.create('button', Y.M.editor_atto.
text = Y.one(selectedNode).get('text');
// We use space or not space because . does not match new lines.
// $$ blah $$
// $$ blah $$.
patterns.push(/\$\$([\S\s]*)\$\$/);
// \( blah \)
// E.g. "\( blah \)".
patterns.push(/\\\(([\S\s]*)\\\)/);
// \[ blah \]
// E.g. "\[ blah \]".
patterns.push(/\\\[([\S\s]*)\\\]/);
// [tex] blah [/tex]
// E.g. "[tex] blah [/tex]".
patterns.push(/\[tex\]([\S\s]*)\[\/tex\]/);
for (i = 0; i < patterns.length; i++) {
pattern = patterns[i];
equation = pattern.exec(text);
if (equation && equation.length) {
equation.shift();
// Remember the inner match so we can replace it later.
this.sourceEquation = equation = equation.shift();
this.sourceEquation = equation = equation[1];
return equation;
}
@ -284,10 +283,13 @@ Y.namespace('M.atto_equation').Button = Y.Base.create('button', Y.M.editor_atto.
/**
* Smart throttle, only call a function every delay milli seconds,
* and always run the last call.
* and always run the last call. Y.throttle does not work here,
* because it calls the function immediately, the first time, and then
* ignores repeated calls within X seconds. This does not guarantee
* that the last call will be executed (which is required here).
*
* @param {function} fn
* @param {Integer} delay - delay in milliseconds
* @param {Number} delay Delay in milliseconds
* @method _throttle
* @private
*/
@ -317,7 +319,8 @@ Y.namespace('M.atto_equation').Button = Y.Base.create('button', Y.M.editor_atto.
currentPos = textarea.get('selectionStart'),
prefix = '',
cursorLatex = '\\square ',
isChar;
isChar,
params;
if (e) {
e.preventDefault();
@ -350,8 +353,11 @@ Y.namespace('M.atto_equation').Button = Y.Base.create('button', Y.M.editor_atto.
text: equation
};
preview = Y.io(url, { sync: true,
data: params });
preview = Y.io(url, {
sync: true,
data: params
});
if (preview.status === 200) {
previewNode.setHTML(preview.responseText);
Y.fire(M.core.event.FILTER_CONTENT_UPDATED, {nodes: (new Y.NodeList(previewNode))});

View File

@ -25,17 +25,22 @@ var LOGNAME = 'moodle-core-event';
* List of published global JS events in Moodle. This is a collection
* of global events that can be subscribed to, or fired from any plugin.
*
* @namespace M.core.event
* @namespace M.core
* @class event
*/
M.core = M.core || {};
M.core.event = {
// This event is triggered when a page has added dynamic nodes to a page
// that should be processed by the filter system. An example is loading
// user text that could have equations in it. MathJax can typeset the equations
// but only if it is notified that there are new nodes in the page that need processing.
// This event must contain a property "nodes" listing the root of any new nodes in the DOM (as a NodeList).
// To trigger this event use M.core.Event.fire(M.core.Event.FILTER_CONTENT_UPDATED, {nodes: list});
/**
* This event is triggered when a page has added dynamic nodes to a page
* that should be processed by the filter system. An example is loading
* user text that could have equations in it. MathJax can typeset the equations
* but only if it is notified that there are new nodes in the page that need processing.
* To trigger this event use M.core.Event.fire(M.core.Event.FILTER_CONTENT_UPDATED, {nodes: list});
*
* @event "filter-content-updated"
* @param nodes {Y.NodeList} List of nodes added to the DOM.
*/
FILTER_CONTENT_UPDATED: "filter-content-updated"
};
@ -53,11 +58,15 @@ var eventDefaultConfig = {
}
};
for (var key in M.core.event) {
Y.publish(M.core.event[key], eventDefaultConfig);
// Publish all the events with a standard config.
var key;
for (key in M.core.event) {
if (M.core.event.hasOwnProperty(key)) {
Y.publish(M.core.event[key], eventDefaultConfig);
}
}
// Publish events with a custom config here.
}, '@VERSION@', {"requires": ["event-custom"]});

View File

@ -1 +1 @@
YUI.add("moodle-core-event",function(e,t){var n="moodle-core-event";M.core=M.core||{},M.core.event={FILTER_CONTENT_UPDATED:"filter-content-updated"};var r={emitFacade:!0,defaultFn:function(e){},preventedFn:function(e){},stoppedFn:function(e){}};for(var i in M.core.event)e.publish(M.core.event[i],r)},"@VERSION@",{requires:["event-custom"]});
YUI.add("moodle-core-event",function(e,t){var n="moodle-core-event";M.core=M.core||{},M.core.event={FILTER_CONTENT_UPDATED:"filter-content-updated"};var r={emitFacade:!0,defaultFn:function(e){},preventedFn:function(e){},stoppedFn:function(e){}},i;for(i in M.core.event)M.core.event.hasOwnProperty(i)&&e.publish(M.core.event[i],r)},"@VERSION@",{requires:["event-custom"]});

View File

@ -25,17 +25,22 @@ var LOGNAME = 'moodle-core-event';
* List of published global JS events in Moodle. This is a collection
* of global events that can be subscribed to, or fired from any plugin.
*
* @namespace M.core.event
* @namespace M.core
* @class event
*/
M.core = M.core || {};
M.core.event = {
// This event is triggered when a page has added dynamic nodes to a page
// that should be processed by the filter system. An example is loading
// user text that could have equations in it. MathJax can typeset the equations
// but only if it is notified that there are new nodes in the page that need processing.
// This event must contain a property "nodes" listing the root of any new nodes in the DOM (as a NodeList).
// To trigger this event use M.core.Event.fire(M.core.Event.FILTER_CONTENT_UPDATED, {nodes: list});
/**
* This event is triggered when a page has added dynamic nodes to a page
* that should be processed by the filter system. An example is loading
* user text that could have equations in it. MathJax can typeset the equations
* but only if it is notified that there are new nodes in the page that need processing.
* To trigger this event use M.core.Event.fire(M.core.Event.FILTER_CONTENT_UPDATED, {nodes: list});
*
* @event "filter-content-updated"
* @param nodes {Y.NodeList} List of nodes added to the DOM.
*/
FILTER_CONTENT_UPDATED: "filter-content-updated"
};
@ -50,11 +55,15 @@ var eventDefaultConfig = {
}
};
for (var key in M.core.event) {
Y.publish(M.core.event[key], eventDefaultConfig);
// Publish all the events with a standard config.
var key;
for (key in M.core.event) {
if (M.core.event.hasOwnProperty(key)) {
Y.publish(M.core.event[key], eventDefaultConfig);
}
}
// Publish events with a custom config here.
}, '@VERSION@', {"requires": ["event-custom"]});

View File

@ -23,17 +23,22 @@ var LOGNAME = 'moodle-core-event';
* List of published global JS events in Moodle. This is a collection
* of global events that can be subscribed to, or fired from any plugin.
*
* @namespace M.core.event
* @namespace M.core
* @class event
*/
M.core = M.core || {};
M.core.event = {
// This event is triggered when a page has added dynamic nodes to a page
// that should be processed by the filter system. An example is loading
// user text that could have equations in it. MathJax can typeset the equations
// but only if it is notified that there are new nodes in the page that need processing.
// This event must contain a property "nodes" listing the root of any new nodes in the DOM (as a NodeList).
// To trigger this event use M.core.Event.fire(M.core.Event.FILTER_CONTENT_UPDATED, {nodes: list});
/**
* This event is triggered when a page has added dynamic nodes to a page
* that should be processed by the filter system. An example is loading
* user text that could have equations in it. MathJax can typeset the equations
* but only if it is notified that there are new nodes in the page that need processing.
* To trigger this event use M.core.Event.fire(M.core.Event.FILTER_CONTENT_UPDATED, {nodes: list});
*
* @event "filter-content-updated"
* @param nodes {Y.NodeList} List of nodes added to the DOM.
*/
FILTER_CONTENT_UPDATED: "filter-content-updated"
};
@ -51,8 +56,12 @@ var eventDefaultConfig = {
}
};
for (var key in M.core.event) {
Y.publish(M.core.event[key], eventDefaultConfig);
// Publish all the events with a standard config.
var key;
for (key in M.core.event) {
if (M.core.event.hasOwnProperty(key)) {
Y.publish(M.core.event[key], eventDefaultConfig);
}
}
// Publish events with a custom config here.