Merge pull request #3598 from Carreau/statelessify-mjutil

Statelessify mathjaxutils

pass math as a variable, rather than instance attribute.

closes #3596
This commit is contained in:
Min RK 2013-07-18 09:52:25 -07:00
commit cf47bc329f
2 changed files with 57 additions and 41 deletions

View File

@ -9,9 +9,11 @@
// MathJax utility functions
//============================================================================
IPython.namespace('IPython.mathjaxutils');
IPython.mathjaxutils = (function (IPython) {
"use strict";
var init = function () {
if (window.MathJax) {
@ -29,7 +31,7 @@ IPython.mathjaxutils = (function (IPython) {
}
});
MathJax.Hub.Configured();
} else if (window.mathjax_url != "") {
} else if (window.mathjax_url !== "") {
// Don't have MathJax, but should. Show dialog.
var message = $('<div/>')
.append(
@ -69,7 +71,7 @@ IPython.mathjaxutils = (function (IPython) {
$("<p></p>").addClass('dialog').html(
"which will prevent this dialog from appearing."
)
)
);
IPython.dialog.modal({
title : "Failed to retrieve MathJax from '" + window.mathjax_url + "'",
body : message,
@ -77,9 +79,7 @@ IPython.mathjaxutils = (function (IPython) {
OK : {class: "btn-danger"}
}
});
} else {
// No MathJax, but none expected. No dialog.
};
}
};
// Some magic for deferring mathematical expressions to MathJax
@ -90,39 +90,38 @@ IPython.mathjaxutils = (function (IPython) {
// permission.
var inline = "$"; // the inline math delimiter
var blocks, start, end, last, braces; // used in searching for math
var math; // stores math until pagedown (Markdown parser) is done
// MATHSPLIT contains the pattern for math delimiters and special symbols
// needed for searching for math in the text input.
// needed for searching for math in the text input.
var MATHSPLIT = /(\$\$?|\\(?:begin|end)\{[a-z]*\*?\}|\\[\\{}$]|[{}]|(?:\n\s*)+|@@\d+@@)/i;
// The math is in blocks i through j, so
// The math is in blocks i through j, so
// collect it into one block and clear the others.
// Replace &, <, and > by named entities.
// For IE, put <br> at the ends of comments since IE removes \n.
// Clear the current math positions and store the index of the
// math, then push the math string onto the storage array.
// The preProcess function is called on all blocks if it has been passed in
var process_math = function (i, j, pre_process) {
var process_math = function (i, j, pre_process, math, blocks) {
var hub = MathJax.Hub;
var block = blocks.slice(i, j + 1).join("").replace(/&/g, "&amp;") // use HTML entity for &
.replace(/</g, "&lt;") // use HTML entity for <
.replace(/>/g, "&gt;") // use HTML entity for >
;
if (hub.Browser.isMSIE) {
block = block.replace(/(%[^\n]*)\n/g, "$1<br/>\n")
block = block.replace(/(%[^\n]*)\n/g, "$1<br/>\n");
}
while (j > i) {
blocks[j] = "";
j--;
}
blocks[i] = "@@" + math.length + "@@"; // replace the current block text with a unique tag to find later
if (pre_process)
if (pre_process){
block = pre_process(block);
}
math.push(block);
start = end = last = null;
}
return blocks;
};
// Break up the text into its component parts and search
// through them for math delimiters, braces, linebreaks, etc.
@ -135,9 +134,12 @@ IPython.mathjaxutils = (function (IPython) {
return text;
}
start = end = last = null; // for tracking math delimiters
math = []; // stores math strings for later
var math = []; // stores math strings for later
var start;
var end;
var last;
var braces;
// Except for extreme edge cases, this should catch precisely those pieces of the markdown
// source that will later be turned into code spans. While MathJax will not TeXify code spans,
// we still have to consider them at this point; the following issue has happened several times:
@ -150,12 +152,16 @@ IPython.mathjaxutils = (function (IPython) {
text = text.replace(/~/g, "~T").replace(/(^|[^\\])(`+)([^\n]*?[^`\n])\2(?!`)/gm, function (wholematch) {
return wholematch.replace(/\$/g, "~D");
});
de_tilde = function (text) { return text.replace(/~([TD])/g, function (wholematch, character) { return { T: "~", D: "$" }[character]; }) };
de_tilde = function (text) {
return text.replace(/~([TD])/g, function (wholematch, character) {
return { T: "~", D: "$" }[character];
});
};
} else {
de_tilde = function (text) { return text; };
}
blocks = IPython.utils.regex_split(text.replace(/\r\n?/g, "\n"),MATHSPLIT);
var blocks = IPython.utils.regex_split(text.replace(/\r\n?/g, "\n"),MATHSPLIT);
for (var i = 1, m = blocks.length; i < m; i += 2) {
var block = blocks[i];
@ -175,25 +181,30 @@ IPython.mathjaxutils = (function (IPython) {
//
if (block === end) {
if (braces) {
last = i
last = i;
}
else {
process_math(start, i, de_tilde)
blocks = process_math(start, i, de_tilde, math, blocks);
start = null;
end = null;
last = null;
}
}
else if (block.match(/\n.*\n/)) {
if (last) {
i = last;
process_math(start, i, de_tilde)
blocks = process_math(start, i, de_tilde, math, blocks);
}
start = end = last = null;
start = null;
end = null;
last = null;
braces = 0;
}
else if (block === "{") {
braces++
braces++;
}
else if (block === "}" && braces) {
braces--
braces--;
}
}
else {
@ -214,32 +225,32 @@ IPython.mathjaxutils = (function (IPython) {
}
}
if (last) {
process_math(start, last, de_tilde)
blocks = process_math(start, last, de_tilde, math, blocks);
start = null;
end = null;
last = null;
}
return de_tilde(blocks.join(""));
}
return [de_tilde(blocks.join("")), math];
};
//
// Put back the math strings that were saved,
// and clear the math array (no need to keep it around).
//
var replace_math = function (text) {
//
var replace_math = function (text, math) {
if (!window.MathJax) {
return text;
}
text = text.replace(/@@(\d+)@@/g, function (match, n) {
return math[n]
return math[n];
});
math = null;
return text;
}
};
return {
init : init,
process_math : process_math,
remove_math : remove_math,
replace_math : replace_math
};
}(IPython));
}(IPython));

View File

@ -17,6 +17,7 @@
@namespace IPython
*/
var IPython = (function (IPython) {
"use strict";
// TextCell base class
var key = IPython.utils.keycodes;
@ -315,9 +316,11 @@ var IPython = (function (IPython) {
if (this.rendered === false) {
var text = this.get_text();
if (text === "") { text = this.placeholder; }
text = IPython.mathjaxutils.remove_math(text);
var text_math = IPython.mathjaxutils.remove_math(text);
var text = text_math[0];
var math = text_math[1];
var html = marked.parser(marked.lexer(text));
html = $(IPython.mathjaxutils.replace_math(html));
html = $(IPython.mathjaxutils.replace_math(html, math));
// links in markdown cells should open in new tabs
html.find("a[href]").attr("target", "_blank");
try {
@ -519,9 +522,11 @@ var IPython = (function (IPython) {
text = text.replace(/\n/g, ' ');
if (text === "") { text = this.placeholder; }
text = Array(this.level + 1).join("#") + " " + text;
text = IPython.mathjaxutils.remove_math(text);
var text_and_math = IPython.mathjaxutils.remove_math(text);
var text = text_and_math[0];
var math = text_and_math[1];
var html = marked.parser(marked.lexer(text));
var h = $(IPython.mathjaxutils.replace_math(html));
var h = $(IPython.mathjaxutils.replace_math(html, math));
// add id and linkback anchor
var hash = h.text().replace(/ /g, '-');
h.attr('id', hash);