Skip to content

Commit

Permalink
fix latex bracket/brace/paren/pipe parsing
Browse files Browse the repository at this point in the history
removed \left / \right hack, make it so we correctly handle vanilla
latex brackets. made text parsing slightly more robust
  • Loading branch information
jenseng committed Apr 14, 2011
1 parent 5b474af commit a499487
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
20 changes: 10 additions & 10 deletions src/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ LatexCmds.nthroot = NthRoot;

// Round/Square/Curly/Angle Brackets (aka Parens/Brackets/Braces)
function Bracket(open, close, cmd, end, replacedFragment) {
MathCommand.call(this, '\\left'+cmd,
MathCommand.call(this, cmd,
['<span><span class="paren">'+open+'</span><span></span><span class="paren">'+close+'</span></span>'],
[open, close],
replacedFragment);
this.end = '\\right'+end;
this.end = end;
}
_ = Bracket.prototype = new MathCommand;
_.initBlocks = function(replacedFragment) {
Expand All @@ -202,7 +202,10 @@ LatexCmds.lbrace = CharCmds['{'] = proto(Bracket, function(replacedFragment) {
Bracket.call(this, '{', '}', '\\{', '\\}', replacedFragment);
});
LatexCmds.langle = LatexCmds.lang = proto(Bracket, function(replacedFragment) {
Bracket.call(this,'&lang;','&rang;','\\langle ','\\rangle ',replacedFragment);
Bracket.call(this,'&lang;','&rang;','\\langle ','\\rangle ', replacedFragment);
});
LatexCmds.lbrack = LatexCmds.lbracket = CharCmds['['] = proto(Bracket, function(replacedFragment) {
Bracket.call(this, '[', ']', '\\[', '\\]', replacedFragment);
});

// Closing bracket matching opening bracket above
Expand All @@ -223,7 +226,10 @@ LatexCmds.rbrace = CharCmds['}'] = proto(CloseBracket, function(replacedFragment
CloseBracket.call(this, '{','}','\\{','\\}',replacedFragment);
});
LatexCmds.rangle = LatexCmds.rang = proto(CloseBracket, function(replacedFragment) {
CloseBracket.call(this,'&lang;','&rang;','\\langle ','\\rangle ',replacedFragment);
CloseBracket.call(this,'&lang;','&rang;','\\langle ','\\rangle ', replacedFragment);
});
LatexCmds.rbrack = LatexCmds.rbracket = CharCmds[']'] = proto(CloseBracket, function(replacedFragment) {
CloseBracket.call(this, '[', ']', '\\[', '\\]', replacedFragment);
});

function Paren(open, close, replacedFragment) {
Expand All @@ -234,9 +240,6 @@ Paren.prototype = Bracket.prototype;
LatexCmds.lparen = CharCmds['('] = proto(Paren, function(replacedFragment) {
Paren.call(this, '(', ')', replacedFragment);
});
LatexCmds.lbrack = LatexCmds.lbracket = CharCmds['['] = proto(Paren, function(replacedFragment) {
Paren.call(this, '[', ']', replacedFragment);
});

function CloseParen(open, close, replacedFragment) {
CloseBracket.call(this, open, close, open, close, replacedFragment);
Expand All @@ -246,9 +249,6 @@ CloseParen.prototype = CloseBracket.prototype;
LatexCmds.rparen = CharCmds[')'] = proto(CloseParen, function(replacedFragment) {
CloseParen.call(this, '(', ')', replacedFragment);
});
LatexCmds.rbrack = LatexCmds.rbracket = CharCmds[']'] = proto(CloseParen, function(replacedFragment) {
CloseParen.call(this, '[', ']', replacedFragment);
});

function Pipes(replacedFragment) {
Paren.call(this, '|', '|', replacedFragment);
Expand Down
24 changes: 19 additions & 5 deletions src/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ _.seek = function(target, pageX, pageY) {
};
_.writeLatex = function(latex) {
this.deleteSelection();
latex = ( latex && latex.match(/\\text\{([^{]|\\\{)*\}|\\[a-z]*|[^\s]/ig) ) || 0;
latex = ( latex && latex.match(/\\text\{([^{}]|\\\[{}])*\}|\\[\{\}\[\]]|[\(\)]|\\[a-z]*|[^\s]/ig) ) || 0;
(function writeLatexBlock(cursor) {
while (latex.length) {
var token = latex.shift(); //pop first item
Expand All @@ -202,10 +202,24 @@ _.writeLatex = function(latex) {
cursor.insertNew(cmd).insertAfter(cmd);
continue; //skip recursing through children
}
else if (token === '\\left' || token === '\\right') { //REMOVEME HACK for parens
token = latex.shift();
if (token === '\\')
token = latex.shift();
else if (token === '|') { //treat pipe as VanillaSymbol, unless it's a right pipe, i.e it has
//a previous pipe sibling w/ at least one other intermediate element
var prevPipe = cursor.prev && cursor.prev.prev;
while (prevPipe && (prevPipe.cmd != '|' || !prevPipe.isEmpty()))
prevPipe = prevPipe.prev;
if (prevPipe) {
prevPipe.remove();
cursor.selectFrom(prevPipe.next);
cursor.show().insertCh(token).insertAfter(cursor.parent.parent);
continue;
}
else {
cmd = new VanillaSymbol(token);
cursor.insertNew(cmd);
}
}
else if ($.inArray(token, ['\\lbrace', '\\{', '\\rbrace', '\\}', '\\langle', '\\lang', '\\rangle', '\\rang', '\\lparen', '(', '\\rparen', ')', '\\lbrack', '\\lbracket', '\\[', '\\rbrack', '\\rbracket', '\\]', '\\lpipe', '\\rpipe']) >= 0) {
token = token.replace(/^\\/, '');

cursor.insertCh(token);
cmd = cursor.prev || cursor.parent.parent;
Expand Down

0 comments on commit a499487

Please sign in to comment.