extract handling of implicit leftright groups (#1047)

* extract handling of implicit leftright groups

* address feedback from code review
This commit is contained in:
Kevin Barabash
2018-01-22 18:50:11 -05:00
committed by GitHub
parent 853e2a4fb7
commit c71ee4bdd5
2 changed files with 28 additions and 26 deletions

View File

@@ -464,7 +464,6 @@ export default class Parser {
* \textrm, where instead of keeping a style we just pretend that there is an
* implicit grouping after it until the end of the group. E.g.
* small text {\Large large text} small text again
* It is also used for \left and \right to get the correct grouping.
*/
parseImplicitGroup(breakOnTokenText?: "]" | "}" | "$"): ?ParseNode {
const start = this.parseSymbol();
@@ -497,25 +496,6 @@ export default class Parser {
style: "text",
value: body,
}, "math");
} else if (func === "\\left") {
// If we see a left:
// Parse the entire left function (including the delimiter)
const left = this.parseGivenFunction(start);
// Parse out the implicit body
++this.leftrightDepth;
const body = this.parseExpression(false);
--this.leftrightDepth;
// Check the next token
this.expect("\\right", false);
const right = this.parseFunction();
if (!right) {
throw new ParseError('failed to parse function after \\right');
}
return new ParseNode("leftright", {
body: body,
left: left.value.value,
right: right.value.value,
}, this.mode);
} else if (func === "\\begin") {
// begin...end is similar to left...right
const begin = this.parseGivenFunction(start);

View File

@@ -128,12 +128,34 @@ defineFunction({
handler: (context, args) => {
const delim = checkDelimiter(args[0], context);
// \left and \right are caught somewhere in Parser.js, which is
// why this data doesn't match what is in buildHTML.
if (context.funcName === "\\left") {
const parser = context.parser;
// Parse out the implicit body
++parser.leftrightDepth;
// parseExpression stops before '\\right'
const body = parser.parseExpression(false);
--parser.leftrightDepth;
// Check the next token
parser.expect("\\right", false);
const right = parser.parseFunction();
if (!right) {
throw new ParseError('failed to parse function after \\right');
}
return {
type: "leftright",
body: body,
left: delim.value,
right: right.value.value,
};
} else {
// This is a little weird. We return this object which gets turned
// into a ParseNode which gets returned by
// `const right = parser.parseFunction();` up above.
return {
type: "leftright",
value: delim.value,
};
}
},
htmlBuilder: (group, options) => {
// Build the inner expression