Merge pull request #2517 from ahmadia/mathjax_fix2

Addresses the javascript errors resulting in a blank notebook pointed out in #2349 as well as significant performance degradations that were introduced by that merge.

It also disables equation numbering/references, fixes broken offline access and re-typesets individual Notebook Cells instead of the entire document on edit.

Closes #2289.
This commit is contained in:
Fernando Perez 2012-11-01 21:51:56 -07:00
commit 3254572a90
5 changed files with 18 additions and 114 deletions

View File

@ -50,15 +50,13 @@ var IPython = (function (IPython) {
});
};
// typeset with MathJax if MathJax is available
Cell.prototype.typeset = function () {
if (window.MathJax){
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
var cell_math = this.element.get(0);
MathJax.Hub.Queue(["Typeset",MathJax.Hub,cell_math]);
}
};
Cell.prototype.select = function () {
this.element.addClass('ui-widget-content ui-corner-all');
this.selected = true;

View File

@ -14,10 +14,9 @@ IPython.namespace('IPython.mathjaxutils');
IPython.mathjaxutils = (function (IPython) {
var init = function () {
if (window.MathJax) {
if (window.MathJax) {
// MathJax loaded
MathJax.Hub.Config({
TeX: { equationNumbers: { autoNumber: "AMS", useLabelIds: true } },
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
@ -28,6 +27,7 @@ IPython.mathjaxutils = (function (IPython) {
styles: {'.MathJax_Display': {"margin": 0}}
}
});
MathJax.Hub.Configured();
} else if (window.mathjax_url != "") {
// Don't have MathJax, but should. Show dialog.
var dialog = $('<div></div>')
@ -88,7 +88,6 @@ IPython.mathjaxutils = (function (IPython) {
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
var HUB = MathJax.Hub;
// MATHSPLIT contains the pattern for math delimiters and special symbols
// needed for searching for math in the text input.
@ -102,11 +101,12 @@ IPython.mathjaxutils = (function (IPython) {
// 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 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) {
if (hub.Browser.isMSIE) {
block = block.replace(/(%[^\n]*)\n/g, "$1<br/>\n")
}
while (j > i) {
@ -127,6 +127,10 @@ IPython.mathjaxutils = (function (IPython) {
// (which will be a paragraph).
//
var remove_math = function (text) {
if (!window.MathJax) {
return text;
}
start = end = last = null; // for tracking math delimiters
math = []; // stores math strings for later
@ -216,6 +220,10 @@ IPython.mathjaxutils = (function (IPython) {
// and clear the math array (no need to keep it around).
//
var replace_math = function (text) {
if (!window.MathJax) {
return text;
}
text = text.replace(/@@(\d+)@@/g, function (match, n) {
return math[n]
});
@ -223,21 +231,11 @@ IPython.mathjaxutils = (function (IPython) {
return text;
}
var queue_render = function () {
// see https://groups.google.com/forum/?fromgroups=#!topic/mathjax-users/cpwy5eCH1ZQ
MathJax.Hub.Queue(
["resetEquationNumbers",MathJax.InputJax.TeX],
["PreProcess",MathJax.Hub],
["Reprocess",MathJax.Hub]
);
}
return {
init : init,
process_math : process_math,
remove_math : remove_math,
replace_math : replace_math,
queue_render : queue_render
replace_math : replace_math
};
}(IPython));

View File

@ -221,11 +221,9 @@ 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 html = IPython.markdown_converter.makeHtml(text);
html = IPython.mathjaxutils.replace_math(html)
try {
this.set_rendered(html);
} catch (e) {
@ -235,7 +233,6 @@ var IPython = (function (IPython) {
"Error rendering Markdown!<br/>" + e.toString())
);
}
this.typeset()
this.element.find('div.text_cell_input').hide();
this.element.find("div.text_cell_render").show();
var code_snippets = this.element.find("pre > code");
@ -250,8 +247,7 @@ var IPython = (function (IPython) {
return '<code class="prettyprint">' + code + '</code>';
});
IPython.mathjaxutils.queue_render()
this.typeset()
this.rendered = true;
}
};

View File

@ -2,7 +2,7 @@
{% block stylesheet %}
{% if mathjax_url %}
<script type="text/javascript" src="{{mathjax_url}}?config=TeX-AMS_HTML" charset="utf-8"></script>
<script type="text/javascript" src="{{mathjax_url}}?config=TeX-AMS_HTML-full&delayStartupUntil=configured" charset="utf-8"></script>
{% end %}
<script type="text/javascript">
// MathJax disabled, set as null to distingish from *missing* MathJax,

View File

@ -162,87 +162,7 @@
"\n",
"---\n",
"\n",
"These equation reference examples are adapted from an [example page in the MathJax documentation](http://cdn.mathjax.org/mathjax/latest/test/sample-eqrefs.html). Note that it's okay to reference equations across cells. Click inside this cell to see the source.\n",
"\n",
"## Labeled equations and references\n",
"\n",
"Here is a labeled equation:\n",
"\\begin{equation}\n",
"x+1\\over\\sqrt{1-x^2}\\label{ref1}\n",
"\\end{equation}\n",
"\n",
"with a reference to ref1: \\ref{ref1},\n",
"and another numbered one with no label:\n",
"\\begin{equation}\n",
"x+1\\over\\sqrt{1-x^2}\n",
"\\end{equation}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## \\nonumber and equation*\n",
"\n",
"This one uses \\nonumber:\n",
"\\begin{equation}\n",
"x+1\\over\\sqrt{1-x^2}\\nonumber\n",
"\\end{equation}\n",
"\n",
"Here's one with the equation* environment:\n",
"\\begin{equation*}\n",
"x+1\\over\\sqrt{1-x^2}\n",
"\\end{equation*}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Forward references\n",
"\n",
"This is a forward reference [\\ref{ref2}] and another \\eqref{ref2} for the \n",
"following equation:\n",
"\n",
"\\begin{equation}\n",
"x+1\\over\\sqrt{1-x^2}\\label{ref2}\n",
"\\end{equation}\n",
"\n",
"More math:\n",
"\\begin{equation}\n",
"x+1\\over\\sqrt{1-x^2}\n",
"\\end{equation}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### References inline and in environments\n",
"\n",
"Here is a ref inside math: $\\ref{ref2}+1$ and text after it.\n",
"\n",
"\\begin{align} \n",
"x& = y_1-y_2+y_3-y_5+y_8-\\dots \n",
"&& \\text{by \\eqref{ref1}}\\\\ \n",
"& = y'\\circ y^* && \\text{(by \\eqref{ref3})}\\\\ \n",
"& = y(0) y' && \\text {by Axiom 1.} \n",
"\\end{align} \n",
"\n",
"### Missing references\n",
"Here's a bad ref [\\ref{ref4}] to a nonexistent label.\n",
"\n",
"### Numbering align environments\n",
"An alignment:\n",
"\\begin{align}\n",
"a&=b\\label{ref3}\\cr\n",
"&=c+d\n",
"\\end{align}\n",
"and a starred one:\n",
"\\begin{align*}\n",
"a&=b\\cr\n",
"&=c+d\n",
"\\end{align*}"
"Equation numbering and referencing will be available in a future version of IPython."
]
},
{
@ -331,14 +251,6 @@
"x=4\n",
"$$"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}