Files
KaTeX/src/functions/color.js
Ashish Myles 5b6ffd7b9d Flatten a bunch of non-pervasive ParseNode types (part 2) (#1552)
* Flatten "operatorname" ParseNode.

* Flatten "overline" ParseNode.

* Flatten "raisebox" ParseNode.

* Flatten "rule" ParseNode.

* Flatten "sizing" ParseNode.

* Flatten "smash" ParseNode.

* Flatten "sqrt" ParseNode.

* Flatten "underline" ParseNode.

* Flatten "enclose" ParseNode.

* Flatten "environment" ParseNode.

* Flatten "genfrac" ParseNode.

* Flatten "htmlmathml" ParseNode.

* Flatten "infix" ParseNode.

* Flatten "kern" ParseNode.

* Flatten "lap" ParseNode.

* Flatten "color" ParseNode.
2018-08-06 14:59:44 +09:00

118 lines
3.3 KiB
JavaScript

// @flow
import defineFunction, {ordargument} from "../defineFunction";
import buildCommon from "../buildCommon";
import mathMLTree from "../mathMLTree";
import {assertNodeType} from "../parseNode";
import * as html from "../buildHTML";
import * as mml from "../buildMathML";
const htmlBuilder = (group, options) => {
const elements = html.buildExpression(
group.body,
options.withColor(group.color),
false
);
// \color isn't supposed to affect the type of the elements it contains.
// To accomplish this, we wrap the results in a fragment, so the inner
// elements will be able to directly interact with their neighbors. For
// example, `\color{red}{2 +} 3` has the same spacing as `2 + 3`
return new buildCommon.makeFragment(elements);
};
const mathmlBuilder = (group, options) => {
const inner = mml.buildExpression(group.body, options);
const node = new mathMLTree.MathNode("mstyle", inner);
node.setAttribute("mathcolor", group.color);
return node;
};
defineFunction({
type: "color",
names: ["\\textcolor"],
props: {
numArgs: 2,
allowedInText: true,
greediness: 3,
argTypes: ["color", "original"],
},
handler({parser}, args) {
const color = assertNodeType(args[0], "color-token");
const body = args[1];
return {
type: "color",
mode: parser.mode,
color: color.value,
body: ordargument(body),
};
},
htmlBuilder,
mathmlBuilder,
});
// TODO(kevinb): define these using macros
defineFunction({
type: "color",
names: [
"\\blue", "\\orange", "\\pink", "\\red",
"\\green", "\\gray", "\\purple",
"\\blueA", "\\blueB", "\\blueC", "\\blueD", "\\blueE",
"\\tealA", "\\tealB", "\\tealC", "\\tealD", "\\tealE",
"\\greenA", "\\greenB", "\\greenC", "\\greenD", "\\greenE",
"\\goldA", "\\goldB", "\\goldC", "\\goldD", "\\goldE",
"\\redA", "\\redB", "\\redC", "\\redD", "\\redE",
"\\maroonA", "\\maroonB", "\\maroonC", "\\maroonD", "\\maroonE",
"\\purpleA", "\\purpleB", "\\purpleC", "\\purpleD", "\\purpleE",
"\\mintA", "\\mintB", "\\mintC",
"\\grayA", "\\grayB", "\\grayC", "\\grayD", "\\grayE",
"\\grayF", "\\grayG", "\\grayH", "\\grayI",
"\\kaBlue", "\\kaGreen",
],
props: {
numArgs: 1,
allowedInText: true,
greediness: 3,
},
handler({parser, funcName}, args) {
const body = args[0];
return {
type: "color",
mode: parser.mode,
color: "katex-" + funcName.slice(1),
body: ordargument(body),
};
},
htmlBuilder,
mathmlBuilder,
});
defineFunction({
type: "color",
names: ["\\color"],
props: {
numArgs: 1,
allowedInText: true,
greediness: 3,
argTypes: ["color"],
},
handler({parser, breakOnTokenText}, args) {
const color = assertNodeType(args[0], "color-token");
// If we see a styling function, parse out the implicit body
const body = parser.parseExpression(true, breakOnTokenText);
return {
type: "color",
mode: parser.mode,
color: color.value,
body,
};
},
htmlBuilder,
mathmlBuilder,
});