From 7a452f10ca08fcccb4a2e73c6e7ed2f643651a13 Mon Sep 17 00:00:00 2001 From: Matthias BUSSONNIER Date: Wed, 10 Jul 2013 17:03:24 +0200 Subject: [PATCH 1/4] statelessify matjaxutils stateless-ification of function that was using module level variable to share data. Now varaible are passed around, and usage of replace math require user to pass a return value of remove_math around --- .../html/static/notebook/js/mathjaxutils.js | 50 ++++++++++++------- IPython/html/static/notebook/js/textcell.js | 6 ++- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/IPython/html/static/notebook/js/mathjaxutils.js b/IPython/html/static/notebook/js/mathjaxutils.js index cf19d55ab..5472fb7d7 100644 --- a/IPython/html/static/notebook/js/mathjaxutils.js +++ b/IPython/html/static/notebook/js/mathjaxutils.js @@ -9,6 +9,8 @@ // MathJax utility functions //============================================================================ +"using strict"; + IPython.namespace('IPython.mathjaxutils'); IPython.mathjaxutils = (function (IPython) { @@ -90,8 +92,6 @@ 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. @@ -104,7 +104,7 @@ IPython.mathjaxutils = (function (IPython) { // 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, start, end, last) { var hub = MathJax.Hub; var block = blocks.slice(i, j + 1).join("").replace(/&/g, "&") // use HTML entity for & .replace(/ Date: Thu, 18 Jul 2013 18:20:49 +0200 Subject: [PATCH 2/4] linting and remove unused var --- .../html/static/notebook/js/mathjaxutils.js | 77 +++++++++---------- 1 file changed, 36 insertions(+), 41 deletions(-) diff --git a/IPython/html/static/notebook/js/mathjaxutils.js b/IPython/html/static/notebook/js/mathjaxutils.js index 5472fb7d7..c2919f72a 100644 --- a/IPython/html/static/notebook/js/mathjaxutils.js +++ b/IPython/html/static/notebook/js/mathjaxutils.js @@ -9,11 +9,11 @@ // MathJax utility functions //============================================================================ -"using strict"; IPython.namespace('IPython.mathjaxutils'); IPython.mathjaxutils = (function (IPython) { + "use strict"; var init = function () { if (window.MathJax) { @@ -31,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 = $('
') .append( @@ -71,7 +71,7 @@ IPython.mathjaxutils = (function (IPython) { $("

").addClass('dialog').html( "which will prevent this dialog from appearing." ) - ) + ); IPython.dialog.modal({ title : "Failed to retrieve MathJax from '" + window.mathjax_url + "'", body : message, @@ -79,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 @@ -94,38 +92,36 @@ IPython.mathjaxutils = (function (IPython) { var inline = "$"; // the inline math delimiter // 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
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, math, blocks, start, end, last) { + var process_math = function (i, j, pre_process, math, blocks) { var hub = MathJax.Hub; var block = blocks.slice(i, j + 1).join("").replace(/&/g, "&") // use HTML entity for & .replace(//g, ">") // use HTML entity for > ; if (hub.Browser.isMSIE) { - block = block.replace(/(%[^\n]*)\n/g, "$1
\n") + block = block.replace(/(%[^\n]*)\n/g, "$1
\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 = null; - end = null; - last = null; - return [blocks, start, end, last] - } + return blocks; + }; // Break up the text into its component parts and search // through them for math delimiters, braces, linebreaks, etc. @@ -142,7 +138,8 @@ IPython.mathjaxutils = (function (IPython) { 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: @@ -155,11 +152,15 @@ 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; }; } - + var blocks = IPython.utils.regex_split(text.replace(/\r\n?/g, "\n"),MATHSPLIT); for (var i = 1, m = blocks.length; i < m; i += 2) { @@ -180,24 +181,19 @@ IPython.mathjaxutils = (function (IPython) { // if (block === end) { if (braces) { - last = i + last = i; } else { - var res = process_math(start, i, de_tilde, math, blocks, start, end, last); - blocks = res[0]; - start = res[1]; - end = res[2]; - last = res[3]; + 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; - var res = process_math(start, i, de_tilde, math, blocks, start, end, last); - blocks = res[0]; - start = res[1]; - end = res[2]; - last = res[3]; + blocks = process_math(start, i, de_tilde, math, blocks); } start = null; end = null; @@ -205,10 +201,10 @@ IPython.mathjaxutils = (function (IPython) { braces = 0; } else if (block === "{") { - braces++ + braces++; } else if (block === "}" && braces) { - braces-- + braces--; } } else { @@ -229,28 +225,27 @@ IPython.mathjaxutils = (function (IPython) { } } if (last) { - var res = process_math(start, last, de_tilde, math, blocks, start, end, last); - blocks = res[0]; - start = res[1] - end = res[2]; - last = res[3]; + blocks = process_math(start, last, de_tilde, math, blocks); + start = null; + end = null; + last = null; } 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, math) { if (!window.MathJax) { return text; } text = text.replace(/@@(\d+)@@/g, function (match, n) { - return math[n] + return math[n]; }); return text; - } + }; return { init : init, From 42fb90acace12df9d263774caa2f707fa834d2b1 Mon Sep 17 00:00:00 2001 From: Matthias BUSSONNIER Date: Thu, 18 Jul 2013 18:29:41 +0200 Subject: [PATCH 3/4] patch header-cell and use-strict --- IPython/html/static/notebook/js/textcell.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/IPython/html/static/notebook/js/textcell.js b/IPython/html/static/notebook/js/textcell.js index 97284cade..810cf724f 100644 --- a/IPython/html/static/notebook/js/textcell.js +++ b/IPython/html/static/notebook/js/textcell.js @@ -17,6 +17,7 @@ @namespace IPython */ var IPython = (function (IPython) { + "use strict"; // TextCell base class var key = IPython.utils.keycodes; @@ -315,9 +316,9 @@ var IPython = (function (IPython) { if (this.rendered === false) { var text = this.get_text(); if (text === "") { text = this.placeholder; } - text_math = IPython.mathjaxutils.remove_math(text); - text = text_math[0] - math = text_math[1] + 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, math)); // links in markdown cells should open in new tabs @@ -521,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); From 38fc90a1b0dd2adf8a11df7660f4d11a9f80e2c6 Mon Sep 17 00:00:00 2001 From: Matthias BUSSONNIER Date: Thu, 18 Jul 2013 18:48:27 +0200 Subject: [PATCH 4/4] add mising semicolon --- IPython/html/static/notebook/js/textcell.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/IPython/html/static/notebook/js/textcell.js b/IPython/html/static/notebook/js/textcell.js index 810cf724f..699fc9e27 100644 --- a/IPython/html/static/notebook/js/textcell.js +++ b/IPython/html/static/notebook/js/textcell.js @@ -317,8 +317,8 @@ var IPython = (function (IPython) { var text = this.get_text(); if (text === "") { text = this.placeholder; } var text_math = IPython.mathjaxutils.remove_math(text); - var text = text_math[0] - var math = text_math[1] + var text = text_math[0]; + var math = text_math[1]; var html = marked.parser(marked.lexer(text)); html = $(IPython.mathjaxutils.replace_math(html, math)); // links in markdown cells should open in new tabs