* Take an entire parse tree, and build it into an appropriate set of HTML * nodes.
(tree, options)
| 6548 | |
| 6549 | |
| 6550 | function buildHTML(tree, options) { |
| 6551 | // Strip off outer tag wrapper for processing below. |
| 6552 | var tag = null; |
| 6553 | |
| 6554 | if (tree.length === 1 && tree[0].type === "tag") { |
| 6555 | tag = tree[0].tag; |
| 6556 | tree = tree[0].body; |
| 6557 | } // Build the expression contained in the tree |
| 6558 | |
| 6559 | |
| 6560 | var expression = buildHTML_buildExpression(tree, options, true); |
| 6561 | var children = []; // Create one base node for each chunk between potential line breaks. |
| 6562 | // The TeXBook [p.173] says "A formula will be broken only after a |
| 6563 | // relation symbol like $=$ or $<$ or $\rightarrow$, or after a binary |
| 6564 | // operation symbol like $+$ or $-$ or $\times$, where the relation or |
| 6565 | // binary operation is on the ``outer level'' of the formula (i.e., not |
| 6566 | // enclosed in {...} and not part of an \over construction)." |
| 6567 | |
| 6568 | var parts = []; |
| 6569 | |
| 6570 | for (var i = 0; i < expression.length; i++) { |
| 6571 | parts.push(expression[i]); |
| 6572 | |
| 6573 | if (expression[i].hasClass("mbin") || expression[i].hasClass("mrel") || expression[i].hasClass("allowbreak")) { |
| 6574 | // Put any post-operator glue on same line as operator. |
| 6575 | // Watch for \nobreak along the way, and stop at \newline. |
| 6576 | var nobreak = false; |
| 6577 | |
| 6578 | while (i < expression.length - 1 && expression[i + 1].hasClass("mspace") && !expression[i + 1].hasClass("newline")) { |
| 6579 | i++; |
| 6580 | parts.push(expression[i]); |
| 6581 | |
| 6582 | if (expression[i].hasClass("nobreak")) { |
| 6583 | nobreak = true; |
| 6584 | } |
| 6585 | } // Don't allow break if \nobreak among the post-operator glue. |
| 6586 | |
| 6587 | |
| 6588 | if (!nobreak) { |
| 6589 | children.push(buildHTMLUnbreakable(parts, options)); |
| 6590 | parts = []; |
| 6591 | } |
| 6592 | } else if (expression[i].hasClass("newline")) { |
| 6593 | // Write the line except the newline |
| 6594 | parts.pop(); |
| 6595 | |
| 6596 | if (parts.length > 0) { |
| 6597 | children.push(buildHTMLUnbreakable(parts, options)); |
| 6598 | parts = []; |
| 6599 | } // Put the newline at the top level |
| 6600 | |
| 6601 | |
| 6602 | children.push(expression[i]); |
| 6603 | } |
| 6604 | } |
| 6605 | |
| 6606 | if (parts.length > 0) { |
| 6607 | children.push(buildHTMLUnbreakable(parts, options)); |
no test coverage detected