mirror of
https://github.com/Smaug123/KaTeX
synced 2025-10-06 11:48:41 +00:00
extract font functions and implicit color function (#1119)
* extract font functions and implicit color function * remove oldFontFuncs from Parser.js * allow old font commands in text mode
This commit is contained in:
@@ -434,17 +434,6 @@ export default class Parser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Old font functions
|
|
||||||
static oldFontFuncs = {
|
|
||||||
"\\rm": "mathrm",
|
|
||||||
"\\sf": "mathsf",
|
|
||||||
"\\tt": "mathtt",
|
|
||||||
"\\bf": "mathbf",
|
|
||||||
"\\it": "mathit",
|
|
||||||
//"\\sl": "textsl",
|
|
||||||
//"\\sc": "textsc",
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses an implicit group, which is a group that starts at the end of a
|
* Parses an implicit group, which is a group that starts at the end of a
|
||||||
* specified, and ends right before a higher explicit group ends, or at EOL. It
|
* specified, and ends right before a higher explicit group ends, or at EOL. It
|
||||||
@@ -515,34 +504,6 @@ export default class Parser {
|
|||||||
endNameToken);
|
endNameToken);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
} else if (func in Parser.oldFontFuncs) {
|
|
||||||
const style = Parser.oldFontFuncs[func];
|
|
||||||
// If we see an old font function, parse out the implicit body
|
|
||||||
this.consumeSpaces();
|
|
||||||
const body = this.parseExpression(true, breakOnTokenText);
|
|
||||||
if (style.slice(0, 4) === 'text') {
|
|
||||||
return new ParseNode("text", {
|
|
||||||
style: style,
|
|
||||||
body: new ParseNode("ordgroup", body, this.mode),
|
|
||||||
}, this.mode);
|
|
||||||
} else {
|
|
||||||
return new ParseNode("font", {
|
|
||||||
font: style,
|
|
||||||
body: new ParseNode("ordgroup", body, this.mode),
|
|
||||||
}, this.mode);
|
|
||||||
}
|
|
||||||
} else if (func === "\\color") {
|
|
||||||
// If we see a styling function, parse out the implicit body
|
|
||||||
const color = this.parseColorGroup(false);
|
|
||||||
if (!color) {
|
|
||||||
throw new ParseError("\\color not followed by color");
|
|
||||||
}
|
|
||||||
const body = this.parseExpression(true, breakOnTokenText);
|
|
||||||
return new ParseNode("color", {
|
|
||||||
type: "color",
|
|
||||||
color: color.result.value,
|
|
||||||
value: body,
|
|
||||||
}, this.mode);
|
|
||||||
} else {
|
} else {
|
||||||
// Defer to parseGivenFunction if it's not a function we handle
|
// Defer to parseGivenFunction if it's not a function we handle
|
||||||
return this.parseGivenFunction(start, breakOnTokenText);
|
return this.parseGivenFunction(start, breakOnTokenText);
|
||||||
|
@@ -422,11 +422,6 @@ groupTypes.spacing = function(group, options) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
groupTypes.font = function(group, options) {
|
|
||||||
const font = group.value.font;
|
|
||||||
return buildGroup(group.value.body, options.withFontFamily(font));
|
|
||||||
};
|
|
||||||
|
|
||||||
groupTypes.horizBrace = function(group, options) {
|
groupTypes.horizBrace = function(group, options) {
|
||||||
const style = options.style;
|
const style = options.style;
|
||||||
|
|
||||||
|
@@ -242,11 +242,6 @@ groupTypes.spacing = function(group) {
|
|||||||
return node;
|
return node;
|
||||||
};
|
};
|
||||||
|
|
||||||
groupTypes.font = function(group, options) {
|
|
||||||
const font = group.value.font;
|
|
||||||
return buildGroup(group.value.body, options.withFontFamily(font));
|
|
||||||
};
|
|
||||||
|
|
||||||
groupTypes.horizBrace = function(group, options) {
|
groupTypes.horizBrace = function(group, options) {
|
||||||
const accentNode = stretchy.mathMLnode(group.value.label);
|
const accentNode = stretchy.mathMLnode(group.value.label);
|
||||||
return new mathMLTree.MathNode(
|
return new mathMLTree.MathNode(
|
||||||
|
@@ -33,14 +33,6 @@ import "./functions/color";
|
|||||||
|
|
||||||
import "./functions/text";
|
import "./functions/text";
|
||||||
|
|
||||||
// \color is handled in Parser.js's parseImplicitGroup
|
|
||||||
defineFunction(["\\color"], {
|
|
||||||
numArgs: 1,
|
|
||||||
allowedInText: true,
|
|
||||||
greediness: 3,
|
|
||||||
argTypes: ["color"],
|
|
||||||
}, null);
|
|
||||||
|
|
||||||
import "./functions/enclose";
|
import "./functions/enclose";
|
||||||
|
|
||||||
import "./functions/overline";
|
import "./functions/overline";
|
||||||
@@ -98,13 +90,6 @@ defineFunction(["\\stackrel"], {
|
|||||||
|
|
||||||
import "./functions/mod";
|
import "./functions/mod";
|
||||||
|
|
||||||
const fontAliases = {
|
|
||||||
"\\Bbb": "\\mathbb",
|
|
||||||
"\\bold": "\\mathbf",
|
|
||||||
"\\frak": "\\mathfrak",
|
|
||||||
"\\bm": "\\boldsymbol",
|
|
||||||
};
|
|
||||||
|
|
||||||
const singleCharIntegrals: {[string]: string} = {
|
const singleCharIntegrals: {[string]: string} = {
|
||||||
"\u222b": "\\int",
|
"\u222b": "\\int",
|
||||||
"\u222c": "\\iint",
|
"\u222c": "\\iint",
|
||||||
@@ -183,36 +168,7 @@ import "./functions/sizing";
|
|||||||
|
|
||||||
import "./functions/styling";
|
import "./functions/styling";
|
||||||
|
|
||||||
// Old font changing functions
|
import "./functions/font";
|
||||||
defineFunction([
|
|
||||||
"\\rm", "\\sf", "\\tt", "\\bf", "\\it", //"\\sl", "\\sc",
|
|
||||||
], {numArgs: 0}, null);
|
|
||||||
|
|
||||||
defineFunction([
|
|
||||||
// styles
|
|
||||||
"\\mathrm", "\\mathit", "\\mathbf", "\\boldsymbol",
|
|
||||||
|
|
||||||
// families
|
|
||||||
"\\mathbb", "\\mathcal", "\\mathfrak", "\\mathscr", "\\mathsf",
|
|
||||||
"\\mathtt",
|
|
||||||
|
|
||||||
// aliases
|
|
||||||
"\\Bbb", "\\bold", "\\frak", "\\bm",
|
|
||||||
], {
|
|
||||||
numArgs: 1,
|
|
||||||
greediness: 2,
|
|
||||||
}, function(context, args) {
|
|
||||||
const body = args[0];
|
|
||||||
let func = context.funcName;
|
|
||||||
if (func in fontAliases) {
|
|
||||||
func = fontAliases[func];
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
type: "font",
|
|
||||||
font: func.slice(1),
|
|
||||||
body: body,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
import "./functions/accent";
|
import "./functions/accent";
|
||||||
|
|
||||||
|
@@ -2,6 +2,7 @@
|
|||||||
import defineFunction, {ordargument} from "../defineFunction";
|
import defineFunction, {ordargument} from "../defineFunction";
|
||||||
import buildCommon from "../buildCommon";
|
import buildCommon from "../buildCommon";
|
||||||
import mathMLTree from "../mathMLTree";
|
import mathMLTree from "../mathMLTree";
|
||||||
|
import ParseError from "../ParseError";
|
||||||
|
|
||||||
import * as html from "../buildHTML";
|
import * as html from "../buildHTML";
|
||||||
import * as mml from "../buildMathML";
|
import * as mml from "../buildMathML";
|
||||||
@@ -86,3 +87,33 @@ defineFunction({
|
|||||||
htmlBuilder,
|
htmlBuilder,
|
||||||
mathmlBuilder,
|
mathmlBuilder,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
defineFunction({
|
||||||
|
type: "color",
|
||||||
|
names: ["\\color"],
|
||||||
|
props: {
|
||||||
|
numArgs: 1,
|
||||||
|
allowedInText: true,
|
||||||
|
greediness: 3,
|
||||||
|
argTypes: ["color"],
|
||||||
|
},
|
||||||
|
handler(context, args) {
|
||||||
|
const {parser, breakOnTokenText} = context;
|
||||||
|
|
||||||
|
const color = args[0];
|
||||||
|
if (!color) {
|
||||||
|
throw new ParseError("\\color not followed by color");
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we see a styling function, parse out the implicit body
|
||||||
|
const body = parser.parseExpression(true, breakOnTokenText);
|
||||||
|
|
||||||
|
return {
|
||||||
|
type: "color",
|
||||||
|
color: color.value,
|
||||||
|
value: body,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
htmlBuilder,
|
||||||
|
mathmlBuilder,
|
||||||
|
});
|
||||||
|
92
src/functions/font.js
Normal file
92
src/functions/font.js
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
// @flow
|
||||||
|
// TODO(kevinb): implement \\sl and \\sc
|
||||||
|
|
||||||
|
import defineFunction from "../defineFunction";
|
||||||
|
import ParseNode from "../ParseNode";
|
||||||
|
|
||||||
|
import * as html from "../buildHTML";
|
||||||
|
import * as mml from "../buildMathML";
|
||||||
|
|
||||||
|
|
||||||
|
const htmlBuilder = (group, options) => {
|
||||||
|
const font = group.value.font;
|
||||||
|
return html.buildGroup(group.value.body, options.withFontFamily(font));
|
||||||
|
};
|
||||||
|
|
||||||
|
const mathmlBuilder = (group, options) => {
|
||||||
|
const font = group.value.font;
|
||||||
|
return mml.buildGroup(group.value.body, options.withFontFamily(font));
|
||||||
|
};
|
||||||
|
|
||||||
|
const fontAliases = {
|
||||||
|
"\\Bbb": "\\mathbb",
|
||||||
|
"\\bold": "\\mathbf",
|
||||||
|
"\\frak": "\\mathfrak",
|
||||||
|
"\\bm": "\\boldsymbol",
|
||||||
|
};
|
||||||
|
|
||||||
|
defineFunction({
|
||||||
|
type: "font",
|
||||||
|
names: [
|
||||||
|
// styles
|
||||||
|
"\\mathrm", "\\mathit", "\\mathbf", "\\boldsymbol",
|
||||||
|
|
||||||
|
// families
|
||||||
|
"\\mathbb", "\\mathcal", "\\mathfrak", "\\mathscr", "\\mathsf",
|
||||||
|
"\\mathtt",
|
||||||
|
|
||||||
|
// aliases
|
||||||
|
"\\Bbb", "\\bold", "\\frak", "\\bm",
|
||||||
|
],
|
||||||
|
props: {
|
||||||
|
numArgs: 1,
|
||||||
|
greediness: 2,
|
||||||
|
},
|
||||||
|
handler: (context, args) => {
|
||||||
|
const body = args[0];
|
||||||
|
let func = context.funcName;
|
||||||
|
if (func in fontAliases) {
|
||||||
|
func = fontAliases[func];
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
type: "font",
|
||||||
|
font: func.slice(1),
|
||||||
|
body: body,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
htmlBuilder,
|
||||||
|
mathmlBuilder,
|
||||||
|
});
|
||||||
|
|
||||||
|
const oldFontFuncsMap = {
|
||||||
|
"\\rm": "mathrm",
|
||||||
|
"\\sf": "mathsf",
|
||||||
|
"\\tt": "mathtt",
|
||||||
|
"\\bf": "mathbf",
|
||||||
|
"\\it": "mathit",
|
||||||
|
};
|
||||||
|
|
||||||
|
// Old font changing functions
|
||||||
|
defineFunction({
|
||||||
|
type: "font",
|
||||||
|
names: Object.keys(oldFontFuncsMap),
|
||||||
|
props: {
|
||||||
|
numArgs: 0,
|
||||||
|
allowedInText: true,
|
||||||
|
},
|
||||||
|
handler: (context, args) => {
|
||||||
|
const {parser, funcName, breakOnTokenText} = context;
|
||||||
|
|
||||||
|
parser.consumeSpaces();
|
||||||
|
const body = parser.parseExpression(true, breakOnTokenText);
|
||||||
|
const style = oldFontFuncsMap[funcName];
|
||||||
|
|
||||||
|
return {
|
||||||
|
type: "font",
|
||||||
|
font: style,
|
||||||
|
body: new ParseNode("ordgroup", body, parser.mode),
|
||||||
|
};
|
||||||
|
},
|
||||||
|
htmlBuilder,
|
||||||
|
mathmlBuilder,
|
||||||
|
});
|
@@ -158,6 +158,7 @@ exports[`An implicit group parser within optional groups should work wwith old f
|
|||||||
"type": "font",
|
"type": "font",
|
||||||
"mode": "math",
|
"mode": "math",
|
||||||
"value": {
|
"value": {
|
||||||
|
"type": "font",
|
||||||
"body": {
|
"body": {
|
||||||
"type": "ordgroup",
|
"type": "ordgroup",
|
||||||
"mode": "math",
|
"mode": "math",
|
||||||
|
Reference in New Issue
Block a user