Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: added StackExchange-style MathJax filtering #2349

Merged
merged 4 commits into from Oct 20, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
83 changes: 0 additions & 83 deletions IPython/frontend/html/notebook/static/js/initmathjax.js

This file was deleted.

243 changes: 243 additions & 0 deletions IPython/frontend/html/notebook/static/js/mathjaxutils.js
@@ -0,0 +1,243 @@
//----------------------------------------------------------------------------
// Copyright (C) 2008-2012 The IPython Development Team
//
// Distributed under the terms of the BSD License. The full license is in
// the file COPYING, distributed as part of this software.
//----------------------------------------------------------------------------

//============================================================================
// MathJax utility functions
//============================================================================

IPython.namespace('IPython.mathjaxutils');

IPython.mathjaxutils = (function (IPython) {

var init = function () {
if (window.MathJax) {
// MathJax loaded
MathJax.Hub.Config({
TeX: { equationNumbers: { autoNumber: "AMS", useLabelIds: true } },
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
processEnvironments: true
},
displayAlign: 'left', // Change this to 'center' to center equations.
"HTML-CSS": {
styles: {'.MathJax_Display': {"margin": 0}}
}
});
} else if (window.mathjax_url != "") {
// Don't have MathJax, but should. Show dialog.
var dialog = $('<div></div>')
.append(
$("<p></p>").addClass('dialog').html(
"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."
)
).dialog({
title: "Failed to retrieve MathJax from '" + window.mathjax_url + "'",
width: "70%",
modal: true,
})
} else {
// No MathJax, but none expected. No dialog.
};
};

// 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.

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.
var MATHSPLIT = /(\$\$?|\\(?:begin|end)\{[a-z]*\*?\}|\\[\\{}$]|[{}]|(?:\n\s*)+|@@\d+@@)/i;

// 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 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")
}
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)
block = pre_process(block);
math.push(block);
start = end = last = null;
}

// 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).
//
var remove_math = function (text) {
start = end = last = null; // for tracking math delimiters
math = []; // stores math strings for later

// 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),
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; };
}

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];
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) {
last = i
}
else {
process_math(start, i, de_tilde)
}
}
else if (block.match(/\n.*\n/)) {
if (last) {
i = last;
process_math(start, i, de_tilde)
}
start = end = last = null;
braces = 0;
}
else if (block === "{") {
braces++
}
else if (block === "}" && braces) {
braces--
}
}
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) {
process_math(start, last, de_tilde)
}
return de_tilde(blocks.join(""));
}

//
// Put back the math strings that were saved,
// and clear the math array (no need to keep it around).
//
var replace_math = function (text) {
text = text.replace(/@@(\d+)@@/g, function (match, n) {
return math[n]
});
math = null;
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
};

}(IPython));
2 changes: 1 addition & 1 deletion IPython/frontend/html/notebook/static/js/notebookmain.js
Expand Up @@ -31,7 +31,7 @@ $(document).ready(function () {
}
// end monkey patching CodeMirror

IPython.init_mathjax();
IPython.mathjaxutils.init();

IPython.read_only = $('body').data('readOnly') === 'True';
$('div#main_app').addClass('border-box-sizing ui-widget');
Expand Down
Expand Up @@ -12,7 +12,7 @@

$(document).ready(function () {

IPython.init_mathjax();
IPython.mathjaxutils.init();

IPython.read_only = $('body').data('readOnly') === 'True';
$('div#main_app').addClass('border-box-sizing ui-widget');
Expand Down
8 changes: 7 additions & 1 deletion IPython/frontend/html/notebook/static/js/textcell.js
@@ -1,5 +1,5 @@
//----------------------------------------------------------------------------
// Copyright (C) 2008-2011 The IPython Development Team
// Copyright (C) 2008-2012 The IPython Development Team
//
// Distributed under the terms of the BSD License. The full license is in
// the file COPYING, distributed as part of this software.
Expand Down Expand Up @@ -221,7 +221,11 @@ var IPython = (function (IPython) {
if (this.rendered === false) {
var text = this.get_text();
if (text === "") { text = this.placeholder; }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inadvertent/accidental blank line here, or meant to group code for clarity?

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) {
Expand All @@ -246,6 +250,8 @@ var IPython = (function (IPython) {

return '<code class="prettyprint">' + code + '</code>';
});

IPython.mathjaxutils.queue_render()
this.rendered = true;
}
};
Expand Down