From 7733aeb8aed17f2740648aa55d9c815d62315dd5 Mon Sep 17 00:00:00 2001 From: Lorenzo Gasparini Date: Thu, 23 Feb 2017 10:34:11 +0100 Subject: [PATCH 1/6] Fixed issue 980 --- notebook/static/notebook/js/mathjaxutils.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/notebook/static/notebook/js/mathjaxutils.js b/notebook/static/notebook/js/mathjaxutils.js index 4f6b1d5aa..fa24a93d2 100644 --- a/notebook/static/notebook/js/mathjaxutils.js +++ b/notebook/static/notebook/js/mathjaxutils.js @@ -59,7 +59,7 @@ define([ // 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; + 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. @@ -178,6 +178,11 @@ define([ end = block; braces = 0; } + else if (block === "\\\\\(") { + start = i; + end = "\\\\\)"; + braces = 0; + } else if (block.substr(1, 5) === "begin") { start = i; end = "\\end" + block.substr(6); @@ -200,7 +205,9 @@ define([ // var replace_math = function (text, math) { text = text.replace(/@@(\d+)@@/g, function (match, n) { - return math[n]; + return math[n] + .replace("\\\\\(", "\\\(") + .replace("\\\\\)", "\\\)"); }); return text; }; From 3c40901b3484172f748cd85ec04a489fb1c1b085 Mon Sep 17 00:00:00 2001 From: Lorenzo Gasparini Date: Wed, 1 Mar 2017 15:29:02 +0100 Subject: [PATCH 2/6] Add support for '\\[' and '\\]' as math delimiters, refactoring. --- notebook/static/notebook/js/mathjaxutils.js | 29 ++++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/notebook/static/notebook/js/mathjaxutils.js b/notebook/static/notebook/js/mathjaxutils.js index fa24a93d2..d55255fde 100644 --- a/notebook/static/notebook/js/mathjaxutils.js +++ b/notebook/static/notebook/js/mathjaxutils.js @@ -59,7 +59,7 @@ define([ // 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; + 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. @@ -178,10 +178,10 @@ define([ end = block; braces = 0; } - else if (block === "\\\\\(") { - start = i; - end = "\\\\\)"; - braces = 0; + else if (block === "\\\\\(" || block === "\\\\\[") { + start = i; + end = block.slice(-1) === "(" ? "\\\\\)" : "\\\\\]"; + braces = 0; } else if (block.substr(1, 5) === "begin") { start = i; @@ -204,11 +204,20 @@ define([ // and clear the math array (no need to keep it around). // var replace_math = function (text, math) { - text = text.replace(/@@(\d+)@@/g, function (match, n) { - return math[n] - .replace("\\\\\(", "\\\(") - .replace("\\\\\)", "\\\)"); - }); + var math_group_process = function (match, n) { + var math_group = math[n]; + + if (math_group.substr(0, 3) === "\\\\\(" && math_group.substr(math_group.length - 3) === "\\\\\)") { + math_group = "\\\(" + math_group.substring(3, math_group.length - 3) + "\\\)"; + } else if (math_group.substr(0, 3) === "\\\\\[" && math_group.substr(math_group.length - 3) === "\\\\\]") { + math_group = "\\\[" + math_group.substring(3, math_group.length - 3) + "\\\]"; + } + + return math_group; + }; + + text = text.replace(/@@(\d+)@@/g, math_group_process); + return text; }; From 3b50d4b4ad8bd0df68876bd60186b86a41ccc857 Mon Sep 17 00:00:00 2001 From: Lorenzo Gasparini Date: Wed, 1 Mar 2017 18:53:34 +0100 Subject: [PATCH 3/6] Add comments --- notebook/static/notebook/js/mathjaxutils.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/notebook/static/notebook/js/mathjaxutils.js b/notebook/static/notebook/js/mathjaxutils.js index d55255fde..b68fa8522 100644 --- a/notebook/static/notebook/js/mathjaxutils.js +++ b/notebook/static/notebook/js/mathjaxutils.js @@ -204,6 +204,11 @@ define([ // and clear the math array (no need to keep it around). // var replace_math = function (text, math) { + // + // Replaces a math placeholder with its corresponding group. + // The math delimiters "\\(", "\\[", "\\)" and "\\]" are replaced + // removing one backslash in order to be interpreted correctly by MathJax. + // var math_group_process = function (match, n) { var math_group = math[n]; @@ -216,6 +221,8 @@ define([ return math_group; }; + // Replace all the math group placeholders in the text + // with the saved strings. text = text.replace(/@@(\d+)@@/g, math_group_process); return text; From 44a1e99f2a40b84c8a00fabb4ec7fb3ba7a680da Mon Sep 17 00:00:00 2001 From: Michael Pacer Date: Wed, 1 Mar 2017 11:33:39 -0800 Subject: [PATCH 4/6] Remove misleading local variable declaration --- notebook/static/notebook/js/mathjaxutils.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/notebook/static/notebook/js/mathjaxutils.js b/notebook/static/notebook/js/mathjaxutils.js index b68fa8522..bd82eea73 100644 --- a/notebook/static/notebook/js/mathjaxutils.js +++ b/notebook/static/notebook/js/mathjaxutils.js @@ -55,8 +55,6 @@ define([ // Other minor modifications are also due to StackExchange and are used with // permission. - 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. var MATHSPLIT = /(\$\$?|\\(?:begin|end)\{[a-z]*\*?\}|\\[{}$]|[{}]|(?:\n\s*)+|@@\d+@@|\\\\(?:\(|\)|\[|\]))/i; @@ -173,7 +171,7 @@ define([ // Look for math start delimiters and when // found, set up the end delimiter. // - if (block === inline || block === "$$") { + if (block === "$" || block === "$$") { start = i; end = block; braces = 0; From 9a817f7bf0859b4314268e2a5f280d34d614122e Mon Sep 17 00:00:00 2001 From: michaelpacer Date: Tue, 7 Mar 2017 16:23:51 -0800 Subject: [PATCH 5/6] Add test for LaTeX parsing --- notebook/tests/notebook/markdown.js | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/notebook/tests/notebook/markdown.js b/notebook/tests/notebook/markdown.js index 344cfed3b..39f06257a 100644 --- a/notebook/tests/notebook/markdown.js +++ b/notebook/tests/notebook/markdown.js @@ -102,4 +102,35 @@ casper.notebook_test(function () { codeblock = '```aaaa\nx = 1\n```' result = '
x = 1\n
' md_render_test(codeblock, result, 'Markdown code block unknown language'); + + function mathjax_render_test(input_string, result, message){ + casper.thenEvaluate(function (text){ + window._test_result = null; + require(['notebook/js/mathjaxutils'],function(mathjaxutils){ + window._test_result = mathjaxutils.remove_math(text); + }); + }, {text: input_string}); + casper.waitFor(function() { + return casper.evaluate(function(){ + return window._test_result!==null; + }); + }); + casper.then(function(){ + var return_val = casper.evaluate(function(){ + var blah = window._test_result; + delete window._test_result; + return blah; + }); + this.test.assertEquals(return_val[0], result[0], message+" markdown"); + this.test.assertEquals(return_val[1].length, result[1].length, message+" math instance count"); + for(var i=0; i Date: Wed, 8 Mar 2017 09:36:00 -0800 Subject: [PATCH 6/6] Added more test cases, added spacing so that it was technically valid --- notebook/tests/notebook/markdown.js | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/notebook/tests/notebook/markdown.js b/notebook/tests/notebook/markdown.js index 39f06257a..314747630 100644 --- a/notebook/tests/notebook/markdown.js +++ b/notebook/tests/notebook/markdown.js @@ -128,9 +128,29 @@ casper.notebook_test(function () { }; }); }; - var input_string_1 = 'x\\\\(a_{0}+ b_{T}\\\\)y\\\\(a_{0}+ b_{T}\\\\)z'; - var expected_result_1 = ['x@@0@@y@@1@@z',['\\\\(a_{0}+ b_{T}\\\\)','\\\\(a_{0}+ b_{T}\\\\)']]; - var message_1 = "multiple inline with underscores"; + var input_string_1 = 'x \\\\(a_{0}+ b_{T}\\\\) y \\\\(a_{0}+ b_{T}\\\\) z'; + var expected_result_1 = ['x @@0@@ y @@1@@ z', ['\\\\(a_{0}+ b_{T}\\\\)','\\\\(a_{0}+ b_{T}\\\\)']]; + var message_1 = "multiple inline(LaTeX style) with underscores"; + + var input_string_2 = 'x \\\\[a_{0}+ b_{T}\\\\] y \\\\[a_{0}+ b_{T}\\\\] z'; + var expected_result_2 = ['x @@0@@ y @@1@@ z', ['\\\\[a_{0}+ b_{T}\\\\]','\\\\[a_{0}+ b_{T}\\\\]']]; + var message_2 = "multiple equation (LaTeX style) with underscores"; + + var input_string_3 = 'x $a_{0}+ b_{T}$ y $a_{0}+ b_{T}$ z'; + var expected_result_3 = ['x @@0@@ y @@1@@ z',['$a_{0}+ b_{T}$','$a_{0}+ b_{T}$']]; + var message_3 = "multiple inline(TeX style) with underscores"; + + var input_string_4 = 'x $$a_{0}+ b_{T}$$ y $$a_{0}+ b_{T}$$ z'; + var expected_result_4 = ['x @@0@@ y @@1@@ z', ['$$a_{0}+ b_{T}$$','$$a_{0}+ b_{T}$$']]; + var message_4 = "multiple equation(TeX style) with underscores"; + + var input_string_5 = 'x \\begin{equation}a_{0}+ b_{T}\\end{equation} y \\begin{equation}a_{0}+ b_{T}\\end{equation} z'; + var expected_result_5 = ['x @@0@@ y @@1@@ z',['\\begin{equation}a_{0}+ b_{T}\\end{equation}','\\begin{equation}a_{0}+ b_{T}\\end{equation}']]; + var message_5 = "multiple equations with underscores"; mathjax_render_test(input_string_1, expected_result_1, message_1); + mathjax_render_test(input_string_2, expected_result_2, message_2); + mathjax_render_test(input_string_3, expected_result_3, message_3); + mathjax_render_test(input_string_4, expected_result_4, message_4); + mathjax_render_test(input_string_5, expected_result_5, message_5); });