##// END OF EJS Templates
Merge pull request #3973 from minrk/26nosuper...
Merge pull request #3973 from minrk/26nosuper logging.Formatter is not new-style in 2.6 add workaround and regression test

File last commit:

r11843:4c4c8f5d
r12108:b5edafe4 merge
Show More
mathjaxutils.js
256 lines | 9.9 KiB | application/javascript | JavascriptLexer
Brian Granger
Major refactoring of notebook....
r6193 //----------------------------------------------------------------------------
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 // Copyright (C) 2008-2012 The IPython Development Team
Brian Granger
Major refactoring of notebook....
r6193 //
// Distributed under the terms of the BSD License. The full license is in
// the file COPYING, distributed as part of this software.
//----------------------------------------------------------------------------
//============================================================================
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 // MathJax utility functions
Brian Granger
Major refactoring of notebook....
r6193 //============================================================================
Matthias BUSSONNIER
statelessify matjaxutils...
r11524
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 IPython.namespace('IPython.mathjaxutils');
Brian Granger
Major refactoring of notebook....
r6193
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 IPython.mathjaxutils = (function (IPython) {
Matthias BUSSONNIER
linting and remove unused var
r11525 "use strict";
Aron Ahmadia
partial refactor, can't quite get environments working
r8565
var init = function () {
Aron Ahmadia
Remove Equation References/Numbering, Fix Bugs...
r8660 if (window.MathJax) {
Brian Granger
Major refactoring of notebook....
r6193 // MathJax loaded
Aron Ahmadia
Finalizing fixes to MathJax enhancements...
r8662 MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
MinRK
process escapes in mathjax...
r10941 processEscapes: true,
Aron Ahmadia
Finalizing fixes to MathJax enhancements...
r8662 processEnvironments: true
},
displayAlign: 'left', // Change this to 'center' to center equations.
"HTML-CSS": {
styles: {'.MathJax_Display': {"margin": 0}}
}
});
MathJax.Hub.Configured();
Matthias BUSSONNIER
linting and remove unused var
r11525 } else if (window.mathjax_url !== "") {
Brian Granger
Major refactoring of notebook....
r6193 // Don't have MathJax, but should. Show dialog.
MinRK
bootstrap dialogs
r10895 var message = $('<div/>')
Brian Granger
Major refactoring of notebook....
r6193 .append(
MinRK
bootstrap dialogs
r10895 $("<p/></p>").addClass('dialog').html(
Brian Granger
Major refactoring of notebook....
r6193 "Math/LaTeX rendering will be disabled."
)
).append(
$("<p></p>").addClass('dialog').html(
"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').html(
">>> from IPython.external import mathjax; mathjax.install_mathjax()"
)
).append(
$("<p></p>").addClass('dialog').html(
"This will try to install MathJax into the IPython source directory."
)
).append(
$("<p></p>").addClass('dialog').html(
"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').html(
"When you start the notebook server, you can instruct it to disable MathJax support altogether:"
)
).append(
$("<pre></pre>").addClass('dialog').html(
"$ ipython notebook --no-mathjax"
)
).append(
$("<p></p>").addClass('dialog').html(
"which will prevent this dialog from appearing."
)
Matthias BUSSONNIER
linting and remove unused var
r11525 );
MinRK
bootstrap dialogs
r10895 IPython.dialog.modal({
title : "Failed to retrieve MathJax from '" + window.mathjax_url + "'",
body : message,
buttons : {
OK : {class: "btn-danger"}
}
});
Matthias BUSSONNIER
linting and remove unused var
r11525 }
Brian Granger
Major refactoring of notebook....
r6193 };
Aron Ahmadia
Refactored to JS standards. Fixed Attribution....
r8568 // Some magic for deferring mathematical expressions to MathJax
// by hiding them from the Markdown parser.
// Some of the code here is adapted with permission from Davide Cervone
// under the terms of the Apache2 license governing the MathJax project.
// Other minor modifications are also due to StackExchange and are used with
// permission.
Aron Ahmadia
partial refactor, can't quite get environments working
r8565
var inline = "$"; // the inline math delimiter
// MATHSPLIT contains the pattern for math delimiters and special symbols
Matthias BUSSONNIER
linting and remove unused var
r11525 // needed for searching for math in the text input.
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 var MATHSPLIT = /(\$\$?|\\(?:begin|end)\{[a-z]*\*?\}|\\[\\{}$]|[{}]|(?:\n\s*)+|@@\d+@@)/i;
Matthias BUSSONNIER
linting and remove unused var
r11525 // The math is in blocks i through j, so
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 // 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
Matthias BUSSONNIER
linting and remove unused var
r11525 var process_math = function (i, j, pre_process, math, blocks) {
Aron Ahmadia
Finalizing fixes to MathJax enhancements...
r8662 var hub = MathJax.Hub;
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 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 >
;
Aron Ahmadia
Finalizing fixes to MathJax enhancements...
r8662 if (hub.Browser.isMSIE) {
Matthias BUSSONNIER
linting and remove unused var
r11525 block = block.replace(/(%[^\n]*)\n/g, "$1<br/>\n");
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 }
while (j > i) {
blocks[j] = "";
j--;
}
blocks[i] = "@@" + math.length + "@@"; // replace the current block text with a unique tag to find later
Matthias BUSSONNIER
linting and remove unused var
r11525 if (pre_process){
Aron Ahmadia
Refactored to JS standards. Fixed Attribution....
r8568 block = pre_process(block);
Matthias BUSSONNIER
linting and remove unused var
r11525 }
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 math.push(block);
Matthias BUSSONNIER
linting and remove unused var
r11525 return blocks;
};
Aron Ahmadia
partial refactor, can't quite get environments working
r8565
// Break up the text into its component parts and search
// through them for math delimiters, braces, linebreaks, etc.
// Math delimiters must match and braces must balance.
// Don't allow math to pass through a double linebreak
// (which will be a paragraph).
//
Aron Ahmadia
Refactored to JS standards. Fixed Attribution....
r8568 var remove_math = function (text) {
Aron Ahmadia
Remove Equation References/Numbering, Fix Bugs...
r8660 if (!window.MathJax) {
Jessica B. Hamrick
Parse markdown correctly when mathjax is disabled
r11843 return [text, null];
Aron Ahmadia
Remove Equation References/Numbering, Fix Bugs...
r8660 }
Matthias BUSSONNIER
statelessify matjaxutils...
r11524 var math = []; // stores math strings for later
var start;
var end;
var last;
Matthias BUSSONNIER
linting and remove unused var
r11525 var braces;
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 // 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:
//
// `$foo` and `$bar` are varibales. --> <code>$foo ` and `$bar</code> are variables.
var hasCodeSpans = /`/.test(text),
Aron Ahmadia
Refactored to JS standards. Fixed Attribution....
r8568 de_tilde;
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 if (hasCodeSpans) {
text = text.replace(/~/g, "~T").replace(/(^|[^\\])(`+)([^\n]*?[^`\n])\2(?!`)/gm, function (wholematch) {
return wholematch.replace(/\$/g, "~D");
});
Matthias BUSSONNIER
linting and remove unused var
r11525 de_tilde = function (text) {
return text.replace(/~([TD])/g, function (wholematch, character) {
return { T: "~", D: "$" }[character];
});
};
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 } else {
Aron Ahmadia
Refactored to JS standards. Fixed Attribution....
r8568 de_tilde = function (text) { return text; };
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 }
Matthias BUSSONNIER
linting and remove unused var
r11525
Matthias BUSSONNIER
statelessify matjaxutils...
r11524 var blocks = IPython.utils.regex_split(text.replace(/\r\n?/g, "\n"),MATHSPLIT);
Aron Ahmadia
partial refactor, can't quite get environments working
r8565
for (var i = 1, m = blocks.length; i < m; i += 2) {
var block = blocks[i];
if (block.charAt(0) === "@") {
//
// Things that look like our math markers will get
// stored and then retrieved along with the math.
//
blocks[i] = "@@" + math.length + "@@";
math.push(block);
}
else if (start) {
//
// If we are in math, look for the end delimiter,
// but don't go past double line breaks, and
// and balance braces within the math.
//
if (block === end) {
if (braces) {
Matthias BUSSONNIER
linting and remove unused var
r11525 last = i;
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 }
else {
Matthias BUSSONNIER
linting and remove unused var
r11525 blocks = process_math(start, i, de_tilde, math, blocks);
start = null;
end = null;
last = null;
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 }
}
else if (block.match(/\n.*\n/)) {
if (last) {
i = last;
Matthias BUSSONNIER
linting and remove unused var
r11525 blocks = process_math(start, i, de_tilde, math, blocks);
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 }
Matthias BUSSONNIER
statelessify matjaxutils...
r11524 start = null;
end = null;
last = null;
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 braces = 0;
}
else if (block === "{") {
Matthias BUSSONNIER
linting and remove unused var
r11525 braces++;
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 }
else if (block === "}" && braces) {
Matthias BUSSONNIER
linting and remove unused var
r11525 braces--;
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 }
}
else {
//
// Look for math start delimiters and when
// found, set up the end delimiter.
//
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) {
Matthias BUSSONNIER
linting and remove unused var
r11525 blocks = process_math(start, last, de_tilde, math, blocks);
start = null;
end = null;
last = null;
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 }
Matthias BUSSONNIER
statelessify matjaxutils...
r11524 return [de_tilde(blocks.join("")), math];
Matthias BUSSONNIER
linting and remove unused var
r11525 };
Brian Granger
Major refactoring of notebook....
r6193
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 //
// Put back the math strings that were saved,
// and clear the math array (no need to keep it around).
Matthias BUSSONNIER
linting and remove unused var
r11525 //
Matthias BUSSONNIER
statelessify matjaxutils...
r11524 var replace_math = function (text, math) {
Aron Ahmadia
Remove Equation References/Numbering, Fix Bugs...
r8660 if (!window.MathJax) {
return text;
}
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 text = text.replace(/@@(\d+)@@/g, function (match, n) {
Matthias BUSSONNIER
linting and remove unused var
r11525 return math[n];
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 });
return text;
Matthias BUSSONNIER
linting and remove unused var
r11525 };
Brian Granger
Major refactoring of notebook....
r6193
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 return {
init : init,
Aron Ahmadia
Refactored to JS standards. Fixed Attribution....
r8568 remove_math : remove_math,
Aron Ahmadia
Remove Equation References/Numbering, Fix Bugs...
r8660 replace_math : replace_math
Aron Ahmadia
partial refactor, can't quite get environments working
r8565 };
Brian Granger
Major refactoring of notebook....
r6193
Matthias BUSSONNIER
statelessify matjaxutils...
r11524 }(IPython));