define([
'base/js/namespace',
'jquery',
'base/js/utils',
'base/js/dialog',
], function(IPython, $, utils, dialog) {
"use strict";
var init = function () {
if (window.MathJax) {
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
processEscapes: true,
processEnvironments: true
},
displayAlign: 'center',
"HTML-CSS": {
styles: {'.MathJax_Display': {"margin": 0}},
linebreaks: { automatic: true }
}
});
MathJax.Hub.Configured();
} else if (window.mathjax_url !== "") {
var message = $('<div/>')
.append(
$("<p/></p>").addClass('dialog').text(
"Math/LaTeX rendering will be disabled."
)
).append(
$("<p></p>").addClass('dialog').text(
"If you have administrative access to the notebook server and" +
" a working internet connection, you can install a local copy" +
" of MathJax for offline use with the following command on the server" +
" at a Python or IPython prompt:"
)
).append(
$("<pre></pre>").addClass('dialog').text(
">>> from IPython.external import mathjax; mathjax.install_mathjax()"
)
).append(
$("<p></p>").addClass('dialog').text(
"This will try to install MathJax into the IPython source directory."
)
).append(
$("<p></p>").addClass('dialog').text(
"If IPython is installed to a location that requires" +
" administrative privileges to write, you will need to make this call as" +
" an administrator, via 'sudo'."
)
).append(
$("<p></p>").addClass('dialog').text(
"When you start the notebook server, you can instruct it to disable MathJax support altogether:"
)
).append(
$("<pre></pre>").addClass('dialog').text(
"$ ipython notebook --no-mathjax"
)
).append(
$("<p></p>").addClass('dialog').text(
"which will prevent this dialog from appearing."
)
);
dialog.modal({
title : "Failed to retrieve MathJax from '" + window.mathjax_url + "'",
body : message,
buttons : {
OK : {class: "btn-danger"}
}
});
}
};
var inline = "$";
var MATHSPLIT = /(\$\$?|\\(?:begin|end)\{[a-z]*\*?\}|\\[\\{}$]|[{}]|(?:\n\s*)+|@@\d+@@)/i;
var process_math = function (i, j, pre_process, math, blocks) {
var block = blocks.slice(i, j + 1).join("").replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
;
if (utils.browser === 'msie') {
block = block.replace(/(%[^\n]*)\n/g, "$1<br/>\n");
}
while (j > i) {
blocks[j] = "";
j--;
}
blocks[i] = "@@" + math.length + "@@";
if (pre_process){
block = pre_process(block);
}
math.push(block);
return blocks;
};
var remove_math = function (text) {
var math = [];
var start;
var end;
var last;
var braces;
var hasCodeSpans = /`/.test(text),
de_tilde;
if (hasCodeSpans) {
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];
});
};
} else {
de_tilde = function (text) { return text; };
}
var blocks = 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];
if (block.charAt(0) === "@") {
blocks[i] = "@@" + math.length + "@@";
math.push(block);
}
else if (start) {
if (block === end) {
if (braces) {
last = i;
}
else {
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;
blocks = process_math(start, i, de_tilde, math, blocks);
}
start = null;
end = null;
last = null;
braces = 0;
}
else if (block === "{") {
braces++;
}
else if (block === "}" && braces) {
braces--;
}
}
else {
if (block === inline || block === "$$") {
start = i;
end = block;
braces = 0;
}
else if (block.substr(1, 5) === "begin") {
start = i;
end = "\\end" + block.substr(6);
braces = 0;
}
}
}
if (last) {
blocks = process_math(start, last, de_tilde, math, blocks);
start = null;
end = null;
last = null;
}
return [de_tilde(blocks.join("")), math];
};
var replace_math = function (text, math) {
text = text.replace(/@@(\d+)@@/g, function (match, n) {
return math[n];
});
return text;
};
var mathjaxutils = {
init : init,
remove_math : remove_math,
replace_math : replace_math
};
IPython.mathjaxutils = mathjaxutils;
return mathjaxutils;
});