Spaces:
Running
on
T4
Running
on
T4
File size: 14,472 Bytes
b498cbf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
/**
* This file converts a parse tree into a cooresponding MathML tree. The main
* entry point is the `buildMathML` function, which takes a parse tree from the
* parser.
*/
var buildCommon = require("./buildCommon");
var fontMetrics = require("./fontMetrics");
var mathMLTree = require("./mathMLTree");
var ParseError = require("./ParseError");
var symbols = require("./symbols");
var utils = require("./utils");
var makeSpan = buildCommon.makeSpan;
var fontMap = buildCommon.fontMap;
/**
* Takes a symbol and converts it into a MathML text node after performing
* optional replacement from symbols.js.
*/
var makeText = function(text, mode) {
if (symbols[mode][text] && symbols[mode][text].replace) {
text = symbols[mode][text].replace;
}
return new mathMLTree.TextNode(text);
};
/**
* Returns the math variant as a string or null if none is required.
*/
var getVariant = function(group, options) {
var font = options.font;
if (!font) {
return null;
}
var mode = group.mode;
if (font === "mathit") {
return "italic";
}
var value = group.value;
if (utils.contains(["\\imath", "\\jmath"], value)) {
return null;
}
if (symbols[mode][value] && symbols[mode][value].replace) {
value = symbols[mode][value].replace;
}
var fontName = fontMap[font].fontName;
if (fontMetrics.getCharacterMetrics(value, fontName)) {
return fontMap[options.font].variant;
}
return null;
};
/**
* Functions for handling the different types of groups found in the parse
* tree. Each function should take a parse group and return a MathML node.
*/
var groupTypes = {};
groupTypes.mathord = function(group, options) {
var node = new mathMLTree.MathNode(
"mi",
[makeText(group.value, group.mode)]);
var variant = getVariant(group, options);
if (variant) {
node.setAttribute("mathvariant", variant);
}
return node;
};
groupTypes.textord = function(group, options) {
var text = makeText(group.value, group.mode);
var variant = getVariant(group, options) || "normal";
var node;
if (/[0-9]/.test(group.value)) {
// TODO(kevinb) merge adjacent <mn> nodes
// do it as a post processing step
node = new mathMLTree.MathNode("mn", [text]);
if (options.font) {
node.setAttribute("mathvariant", variant);
}
} else {
node = new mathMLTree.MathNode("mi", [text]);
node.setAttribute("mathvariant", variant);
}
return node;
};
groupTypes.bin = function(group) {
var node = new mathMLTree.MathNode(
"mo", [makeText(group.value, group.mode)]);
return node;
};
groupTypes.rel = function(group) {
var node = new mathMLTree.MathNode(
"mo", [makeText(group.value, group.mode)]);
return node;
};
groupTypes.open = function(group) {
var node = new mathMLTree.MathNode(
"mo", [makeText(group.value, group.mode)]);
return node;
};
groupTypes.close = function(group) {
var node = new mathMLTree.MathNode(
"mo", [makeText(group.value, group.mode)]);
return node;
};
groupTypes.inner = function(group) {
var node = new mathMLTree.MathNode(
"mo", [makeText(group.value, group.mode)]);
return node;
};
groupTypes.punct = function(group) {
var node = new mathMLTree.MathNode(
"mo", [makeText(group.value, group.mode)]);
node.setAttribute("separator", "true");
return node;
};
groupTypes.ordgroup = function(group, options) {
var inner = buildExpression(group.value, options);
var node = new mathMLTree.MathNode("mrow", inner);
return node;
};
groupTypes.text = function(group, options) {
var inner = buildExpression(group.value.body, options);
var node = new mathMLTree.MathNode("mtext", inner);
return node;
};
groupTypes.color = function(group, options) {
var inner = buildExpression(group.value.value, options);
var node = new mathMLTree.MathNode("mstyle", inner);
node.setAttribute("mathcolor", group.value.color);
return node;
};
groupTypes.supsub = function(group, options) {
var children = [buildGroup(group.value.base, options)];
if (group.value.sub) {
children.push(buildGroup(group.value.sub, options));
}
if (group.value.sup) {
children.push(buildGroup(group.value.sup, options));
}
var nodeType;
if (!group.value.sub) {
nodeType = "msup";
} else if (!group.value.sup) {
nodeType = "msub";
} else {
nodeType = "msubsup";
}
var node = new mathMLTree.MathNode(nodeType, children);
return node;
};
groupTypes.genfrac = function(group, options) {
var node = new mathMLTree.MathNode(
"mfrac",
[buildGroup(group.value.numer, options),
buildGroup(group.value.denom, options)]);
if (!group.value.hasBarLine) {
node.setAttribute("linethickness", "0px");
}
if (group.value.leftDelim != null || group.value.rightDelim != null) {
var withDelims = [];
if (group.value.leftDelim != null) {
var leftOp = new mathMLTree.MathNode(
"mo", [new mathMLTree.TextNode(group.value.leftDelim)]);
leftOp.setAttribute("fence", "true");
withDelims.push(leftOp);
}
withDelims.push(node);
if (group.value.rightDelim != null) {
var rightOp = new mathMLTree.MathNode(
"mo", [new mathMLTree.TextNode(group.value.rightDelim)]);
rightOp.setAttribute("fence", "true");
withDelims.push(rightOp);
}
var outerNode = new mathMLTree.MathNode("mrow", withDelims);
return outerNode;
}
return node;
};
groupTypes.array = function(group, options) {
return new mathMLTree.MathNode(
"mtable", group.value.body.map(function(row) {
return new mathMLTree.MathNode(
"mtr", row.map(function(cell) {
return new mathMLTree.MathNode(
"mtd", [buildGroup(cell, options)]);
}));
}));
};
groupTypes.sqrt = function(group, options) {
var node;
if (group.value.index) {
node = new mathMLTree.MathNode(
"mroot", [
buildGroup(group.value.body, options),
buildGroup(group.value.index, options),
]);
} else {
node = new mathMLTree.MathNode(
"msqrt", [buildGroup(group.value.body, options)]);
}
return node;
};
groupTypes.leftright = function(group, options) {
var inner = buildExpression(group.value.body, options);
if (group.value.left !== ".") {
var leftNode = new mathMLTree.MathNode(
"mo", [makeText(group.value.left, group.mode)]);
leftNode.setAttribute("fence", "true");
inner.unshift(leftNode);
}
if (group.value.right !== ".") {
var rightNode = new mathMLTree.MathNode(
"mo", [makeText(group.value.right, group.mode)]);
rightNode.setAttribute("fence", "true");
inner.push(rightNode);
}
var outerNode = new mathMLTree.MathNode("mrow", inner);
return outerNode;
};
groupTypes.accent = function(group, options) {
var accentNode = new mathMLTree.MathNode(
"mo", [makeText(group.value.accent, group.mode)]);
var node = new mathMLTree.MathNode(
"mover",
[buildGroup(group.value.base, options),
accentNode]);
node.setAttribute("accent", "true");
return node;
};
groupTypes.spacing = function(group) {
var node;
if (group.value === "\\ " || group.value === "\\space" ||
group.value === " " || group.value === "~") {
node = new mathMLTree.MathNode(
"mtext", [new mathMLTree.TextNode("\u00a0")]);
} else {
node = new mathMLTree.MathNode("mspace");
node.setAttribute(
"width", buildCommon.spacingFunctions[group.value].size);
}
return node;
};
groupTypes.op = function(group) {
var node;
// TODO(emily): handle big operators using the `largeop` attribute
if (group.value.symbol) {
// This is a symbol. Just add the symbol.
node = new mathMLTree.MathNode(
"mo", [makeText(group.value.body, group.mode)]);
} else {
// This is a text operator. Add all of the characters from the
// operator's name.
// TODO(emily): Add a space in the middle of some of these
// operators, like \limsup.
node = new mathMLTree.MathNode(
"mi", [new mathMLTree.TextNode(group.value.body.slice(1))]);
}
return node;
};
groupTypes.katex = function(group) {
var node = new mathMLTree.MathNode(
"mtext", [new mathMLTree.TextNode("KaTeX")]);
return node;
};
groupTypes.font = function(group, options) {
var font = group.value.font;
return buildGroup(group.value.body, options.withFont(font));
};
groupTypes.delimsizing = function(group) {
var children = [];
if (group.value.value !== ".") {
children.push(makeText(group.value.value, group.mode));
}
var node = new mathMLTree.MathNode("mo", children);
if (group.value.delimType === "open" ||
group.value.delimType === "close") {
// Only some of the delimsizing functions act as fences, and they
// return "open" or "close" delimTypes.
node.setAttribute("fence", "true");
} else {
// Explicitly disable fencing if it's not a fence, to override the
// defaults.
node.setAttribute("fence", "false");
}
return node;
};
groupTypes.styling = function(group, options) {
var inner = buildExpression(group.value.value, options);
var node = new mathMLTree.MathNode("mstyle", inner);
var styleAttributes = {
"display": ["0", "true"],
"text": ["0", "false"],
"script": ["1", "false"],
"scriptscript": ["2", "false"],
};
var attr = styleAttributes[group.value.style];
node.setAttribute("scriptlevel", attr[0]);
node.setAttribute("displaystyle", attr[1]);
return node;
};
groupTypes.sizing = function(group, options) {
var inner = buildExpression(group.value.value, options);
var node = new mathMLTree.MathNode("mstyle", inner);
// TODO(emily): This doesn't produce the correct size for nested size
// changes, because we don't keep state of what style we're currently
// in, so we can't reset the size to normal before changing it. Now
// that we're passing an options parameter we should be able to fix
// this.
node.setAttribute(
"mathsize", buildCommon.sizingMultiplier[group.value.size] + "em");
return node;
};
groupTypes.overline = function(group, options) {
var operator = new mathMLTree.MathNode(
"mo", [new mathMLTree.TextNode("\u203e")]);
operator.setAttribute("stretchy", "true");
var node = new mathMLTree.MathNode(
"mover",
[buildGroup(group.value.body, options),
operator]);
node.setAttribute("accent", "true");
return node;
};
groupTypes.underline = function(group, options) {
var operator = new mathMLTree.MathNode(
"mo", [new mathMLTree.TextNode("\u203e")]);
operator.setAttribute("stretchy", "true");
var node = new mathMLTree.MathNode(
"munder",
[buildGroup(group.value.body, options),
operator]);
node.setAttribute("accentunder", "true");
return node;
};
groupTypes.rule = function(group) {
// TODO(emily): Figure out if there's an actual way to draw black boxes
// in MathML.
var node = new mathMLTree.MathNode("mrow");
return node;
};
groupTypes.llap = function(group, options) {
var node = new mathMLTree.MathNode(
"mpadded", [buildGroup(group.value.body, options)]);
node.setAttribute("lspace", "-1width");
node.setAttribute("width", "0px");
return node;
};
groupTypes.rlap = function(group, options) {
var node = new mathMLTree.MathNode(
"mpadded", [buildGroup(group.value.body, options)]);
node.setAttribute("width", "0px");
return node;
};
groupTypes.phantom = function(group, options, prev) {
var inner = buildExpression(group.value.value, options);
return new mathMLTree.MathNode("mphantom", inner);
};
/**
* Takes a list of nodes, builds them, and returns a list of the generated
* MathML nodes. A little simpler than the HTML version because we don't do any
* previous-node handling.
*/
var buildExpression = function(expression, options) {
var groups = [];
for (var i = 0; i < expression.length; i++) {
var group = expression[i];
groups.push(buildGroup(group, options));
}
return groups;
};
/**
* Takes a group from the parser and calls the appropriate groupTypes function
* on it to produce a MathML node.
*/
var buildGroup = function(group, options) {
if (!group) {
return new mathMLTree.MathNode("mrow");
}
if (groupTypes[group.type]) {
// Call the groupTypes function
return groupTypes[group.type](group, options);
} else {
throw new ParseError(
"Got group of unknown type: '" + group.type + "'");
}
};
/**
* Takes a full parse tree and settings and builds a MathML representation of
* it. In particular, we put the elements from building the parse tree into a
* <semantics> tag so we can also include that TeX source as an annotation.
*
* Note that we actually return a domTree element with a `<math>` inside it so
* we can do appropriate styling.
*/
var buildMathML = function(tree, texExpression, options) {
var expression = buildExpression(tree, options);
// Wrap up the expression in an mrow so it is presented in the semantics
// tag correctly.
var wrapper = new mathMLTree.MathNode("mrow", expression);
// Build a TeX annotation of the source
var annotation = new mathMLTree.MathNode(
"annotation", [new mathMLTree.TextNode(texExpression)]);
annotation.setAttribute("encoding", "application/x-tex");
var semantics = new mathMLTree.MathNode(
"semantics", [wrapper, annotation]);
var math = new mathMLTree.MathNode("math", [semantics]);
// You can't style <math> nodes, so we wrap the node in a span.
return makeSpan(["katex-mathml"], [math]);
};
module.exports = buildMathML;
|