Skip to content

Commit

Permalink
Merge pull request #2220 from delftswa2017/fix-issue-980
Browse files Browse the repository at this point in the history
Fixed issue #980: incorrect LaTeX capturing and MathJax rendering
  • Loading branch information
rgbkrk authored Mar 9, 2017
2 parents ecb309d + b7b6ec2 commit ead73b9
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
35 changes: 28 additions & 7 deletions notebook/static/notebook/js/mathjaxutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ 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;
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.
Expand Down Expand Up @@ -173,11 +171,16 @@ 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;
}
else if (block === "\\\\\(" || block === "\\\\\[") {
start = i;
end = block.slice(-1) === "(" ? "\\\\\)" : "\\\\\]";
braces = 0;
}
else if (block.substr(1, 5) === "begin") {
start = i;
end = "\\end" + block.substr(6);
Expand All @@ -199,9 +202,27 @@ 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];
});
//
// 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];

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;
};

// Replace all the math group placeholders in the text
// with the saved strings.
text = text.replace(/@@(\d+)@@/g, math_group_process);

return text;
};

Expand Down
51 changes: 51 additions & 0 deletions notebook/tests/notebook/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,55 @@ casper.notebook_test(function () {
codeblock = '```aaaa\nx = 1\n```'
result = '<pre><code class="cm-s-ipython language-aaaa">x = 1\n</code></pre>'
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<return_val[1].length; i++){
this.test.assertEquals(return_val[1][i], result[1][i], message+" math instance "+i);
};
});
};
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);
});

0 comments on commit ead73b9

Please sign in to comment.