Added support for visual depiction of unsupported commands

This commit is contained in:
Jeff Everett
2015-07-27 03:45:19 -06:00
parent 4be3931cb5
commit e1c221273c
9 changed files with 152 additions and 25 deletions

View File

@@ -129,6 +129,14 @@ Parser.prototype.parseExpression = function(pos, mode, breakOnInfix, breakOnToke
}
var atom = this.parseAtom(pos, mode);
if (!atom) {
if (!this.settings.breakOnUnsupportedCmds && lex.text[0] === "\\") {
var errorNode = this.handleUnsupportedCmd(lex.text, mode);
body.push(errorNode);
pos = lex.position;
continue;
}
break;
}
if (breakOnInfix && atom.result.type === "infix") {
@@ -204,8 +212,16 @@ Parser.prototype.handleSupSubscript = function(pos, mode, symbol, name) {
var group = this.parseGroup(pos, mode);
if (!group) {
throw new ParseError(
"Expected group after '" + symbol + "'", this.lexer, pos);
var lex = this.lexer.lex(pos, mode);
if (!this.settings.breakOnUnsupportedCmds && lex.text[0] === "\\") {
return new ParseResult(
this.handleUnsupportedCmd(lex.text, mode),
lex.position);
} else {
throw new ParseError(
"Expected group after '" + symbol + "'", this.lexer, pos);
}
} else if (group.isFunction) {
// ^ and _ have a greediness, so handle interactions with functions'
// greediness
@@ -223,6 +239,37 @@ Parser.prototype.handleSupSubscript = function(pos, mode, symbol, name) {
}
};
/**
* Converts the textual input of an unsupported command into a text node
* contained within a color node whose color is determined by errorColor
*/
Parser.prototype.handleUnsupportedCmd = function(text, mode) {
var textordArray = [];
for (var i = 0; i < text.length; i++) {
textordArray.push(new ParseNode("textord", text[i], "text"));
}
var textNode = new ParseNode(
"text",
{
body: textordArray,
type: "text"
},
mode);
var colorNode = new ParseNode(
"color",
{
color: this.settings.errorColor,
value: [textNode],
type: "color"
},
mode);
return colorNode;
};
/**
* Parses a group with optional super/subscripts.
*
@@ -498,9 +545,18 @@ Parser.prototype.parseArguments = function(pos, mode, func, funcData, args) {
arg = this.parseGroup(newPos, mode);
}
if (!arg) {
throw new ParseError(
"Expected group after '" + func + "'",
this.lexer, newPos);
var lex = this.lexer.lex(newPos, mode);
if (!this.settings.breakOnUnsupportedCmds && lex.text[0] === "\\") {
arg = new ParseFuncOrArgument(
new ParseResult(
this.handleUnsupportedCmd(lex.text, mode),
lex.position),
false);
} else {
throw new ParseError(
"Expected group after '" + func + "'", this.lexer, pos);
}
}
}
var argNode;

View File

@@ -21,6 +21,8 @@ function Settings(options) {
// allow null options
options = options || {};
this.displayMode = get(options.displayMode, false);
this.breakOnUnsupportedCmds = get(options.breakOnUnsupportedCmds, true);
this.errorColor = get(options.errorColor, "#cc0000");
}
module.exports = Settings;

View File

@@ -54,7 +54,11 @@ var mathit = function(value, mode, color, classes) {
var mathrm = function(value, mode, color, classes) {
// Decide what font to render the symbol in by its entry in the symbols
// table.
if (symbols[mode][value].font === "main") {
// Have a special case for when the value = \ because the \ is used as a
// textord in unsupported command errors but cannot be parsed as a regular
// text ordinal and is therefore not present as a symbol in the symbols
// table for text
if (value === "\\" || symbols[mode][value].font === "main") {
return makeSymbol(value, "Main-Regular", mode, color, classes);
} else {
return makeSymbol(