Add support for '\\[' and '\\]' as math delimiters, refactoring.

Lorenzo Gasparini 9 years ago
parent 0447a20598
commit 3c40901b34

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

Loading…
Cancel
Save