diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-06-09 22:32:30 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-06-20 12:06:19 +0100 |
| commit | ac3981df81148fad21c32b22496ecc145ad36e98 (patch) | |
| tree | cfba69d2fedb0cacc7d544749f199bd339a1c6ad | |
| parent | 8617c85c8626354fff63eeaeb775402715fe63a4 (diff) | |
Use simpler structure for HTML without fragments
| -rw-r--r-- | lib/document-to-html.js | 84 | ||||
| -rw-r--r-- | lib/html/ast.js | 33 | ||||
| -rw-r--r-- | lib/html/index.js | 28 | ||||
| -rw-r--r-- | lib/html/simplify.js | 78 | ||||
| -rw-r--r-- | lib/images.js | 2 | ||||
| -rw-r--r-- | tests/document-to-html.tests.js | 2 | ||||
| -rw-r--r-- | tests/html/ast.tests.js | 20 | ||||
| -rw-r--r-- | tests/html/simplify.tests.js | 90 | ||||
| -rw-r--r-- | tests/html/write.tests.js | 2 |
9 files changed, 160 insertions, 179 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js index e3cb169..33b6d46 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -57,7 +57,13 @@ function DocumentConversion(options) { function convertElements(elements, messages, callback) { async.mapSeries(elements, function(element, callback) { return elementToHtml(element, messages, callback); - }, callback); + }, function(error, results) { + if (error) { + callback(error); + } else { + callback(null, _.flatten(results, true)); + } + }); } function elementToHtml(element, messages, callback) { @@ -75,9 +81,9 @@ function DocumentConversion(options) { setImmediate(function() { convertElements(element.children, messages, function(error, children) { if (!options.ignoreEmptyParagraphs) { - children = [Html.forceWrite, Html.fragment(children)]; + children = [Html.forceWrite].concat(children); } - callback(null, Html.pathToNode(style, children)); + callback(null, Html.pathToNodes(style, children)); }); }); } @@ -103,7 +109,7 @@ function DocumentConversion(options) { } } convertElements(run.children, messages, function(error, children) { - var result = Html.fragment(children); + var result = children; if (run.isStrikethrough) { result = convertRunProperty(result, "strikethrough", "s"); } @@ -123,20 +129,20 @@ function DocumentConversion(options) { result = convertRunProperty(result, "bold", "strong"); } if (style) { - result = Html.pathToNode(style.to, [result]); + result = Html.pathToNodes(style.to, result); } callback(null, result); }); } - function wrapInNonFreshElement(node, tagName) { - return Html.nonFreshElement(tagName, {}, [node]); + function wrapInNonFreshElement(nodes, tagName) { + return [Html.nonFreshElement(tagName, {}, nodes)]; } function convertUnderline(runHtml) { var style = findStyle({type: "underline"}); if (style) { - return Html.pathToNode(style.to, [runHtml]); + return Html.pathToNodes(style.to, runHtml); } else { return runHtml; } @@ -145,7 +151,7 @@ function DocumentConversion(options) { function convertRunProperty(result, elementType, defaultTagName) { var style = findStyle({type: elementType}); if (style) { - return Html.pathToNode(style.to, [result]); + return Html.pathToNodes(style.to, result); } else { return wrapInNonFreshElement(result, defaultTagName); } @@ -172,7 +178,7 @@ function DocumentConversion(options) { convertImage(image, messages, function(error, result) { if (error) { messages.push(results.warning(error.message)); - callback(null, Html.fragment()); + callback(null, []); } else { callback(null, result); } @@ -210,17 +216,17 @@ function DocumentConversion(options) { attributes.rowspan = element.rowSpan.toString(); } - callback(null, Html.freshElement("td", attributes, [ - Html.forceWrite, - Html.fragment(children)])); + callback(null, [ + Html.freshElement("td", attributes, [Html.forceWrite].concat(children)) + ]); }); } function wrapChildrenInFreshElement(element, wrapElementName, messages, callback) { convertElements(element.children, messages, function(error, children) { - callback(null, Html.freshElement(wrapElementName, {}, [ - Html.forceWrite, - Html.fragment(children)])); + callback(null, [ + Html.freshElement(wrapElementName, {}, [Html.forceWrite].concat(children)) + ]); }); } @@ -231,53 +237,51 @@ function DocumentConversion(options) { return document.notes.resolve(noteReference); }); convertElements(notes, messages, function(error, notesNodes) { - callback(null, Html.fragment([ - Html.fragment(children), - Html.freshElement("ol", {}, notesNodes)])); + callback(null, children.concat([ + Html.freshElement("ol", {}, notesNodes) + ])); }); }); }, "paragraph": convertParagraph, "run": convertRun, "text": function(element, messages, callback) { - callback(null, Html.text(element.value)); + callback(null, [Html.text(element.value)]); }, "tab": function(element, messages, callback) { - callback(null, Html.text("\t")); + callback(null, [Html.text("\t")]); }, "hyperlink": function(element, messages, callback) { var href = element.anchor ? "#" + htmlId(element.anchor) : element.href; convertElements(element.children, messages, function(error, children) { - callback(null, Html.freshElement("a", {href: href}, children)); + callback(null, [Html.freshElement("a", {href: href}, children)]); }); }, "bookmarkStart": function(element, messages, callback) { - callback(null, Html.freshElement("a", { + var anchor = Html.freshElement("a", { id: htmlId(element.name) - }, [Html.forceWrite])); + }, [Html.forceWrite]); + callback(null, [anchor]); }, "noteReference": function(element, messages, callback) { noteReferences.push(element); + var anchor = Html.freshElement("a", { + href: "#" + noteHtmlId(element), + id: noteRefHtmlId(element) + }, [Html.text("[" + (noteNumber++) + "]")]); - callback(null, Html.freshElement("sup", {}, [ - Html.freshElement("a", { - href: "#" + noteHtmlId(element), - id: noteRefHtmlId(element) - }, [Html.text("[" + (noteNumber++) + "]")]) - ])); + callback(null, [Html.freshElement("sup", {}, [anchor])]); }, "note": function(element, messages, callback) { convertElements(element.body, messages, function(error, children) { - callback(null, Html.freshElement("li", {id: noteHtmlId(element)}, [ - Html.fragment(children), - - Html.elementWithTag(htmlPaths.element("p", {}, {fresh: false}), [ - Html.text(" "), - Html.freshElement("a", {href: "#" + noteRefHtmlId(element)}, [Html.text("↑")]), - - ]) - ])); + var backLink = Html.elementWithTag(htmlPaths.element("p", {}, {fresh: false}), [ + Html.text(" "), + Html.freshElement("a", {href: "#" + noteRefHtmlId(element)}, [Html.text("↑")]), + ]); + var body = children.concat([backLink]); + + callback(null, Html.freshElement("li", {id: noteHtmlId(element)}, body)); }); }, "image": recoveringConvertImage(options.convertImage || defaultConvertImage), @@ -285,7 +289,7 @@ function DocumentConversion(options) { "tableRow": convertTableRow, "tableCell": convertTableCell, "lineBreak": function(element, messages, callback) { - callback(null, Html.selfClosingElement("br")); + callback(null, [Html.selfClosingElement("br")]); } }; return { diff --git a/lib/html/ast.js b/lib/html/ast.js index 30e65c7..db9e690 100644 --- a/lib/html/ast.js +++ b/lib/html/ast.js @@ -1,15 +1,6 @@ -var _ = require("underscore"); - var htmlPaths = require("../html-paths"); -function fragment(children) { - return { - type: "fragment", - children: collapseFragments(children) - }; -} - function nonFreshElement(tagName, attributes, children) { return elementWithTag( htmlPaths.element(tagName, attributes, {fresh: false}), @@ -26,18 +17,9 @@ function elementWithTag(tag, children) { return { type: "element", tag: tag, - children: collapseFragments(children), - isEmpty: _.all(children, function(child) { - return child.isEmpty; - }), - appendChild: appendChild + children: children || [] }; } - -function appendChild(child) { - this.children.push(child); - this.isEmpty = this.isEmpty && child.isEmpty; -} function selfClosingElement(tagName, attributes) { return { @@ -50,23 +32,14 @@ function selfClosingElement(tagName, attributes) { function text(value) { return { type: "text", - value: value, - isEmpty: !value + value: value }; } var forceWrite = { - type: "forceWrite", - isEmpty: false + type: "forceWrite" }; -function collapseFragments(elements) { - return _.flatten((elements || []).map(function(element) { - return element.type === "fragment" ? element.children : [element]; - }), true); -} - -exports.fragment = fragment; exports.freshElement = freshElement; exports.nonFreshElement = nonFreshElement; exports.elementWithTag = elementWithTag; diff --git a/lib/html/index.js b/lib/html/index.js index 8fa1fcf..edc8256 100644 --- a/lib/html/index.js +++ b/lib/html/index.js @@ -1,6 +1,5 @@ var ast = require("./ast"); -exports.fragment = ast.fragment; exports.freshElement = ast.freshElement; exports.nonFreshElement = ast.nonFreshElement; exports.elementWithTag = ast.elementWithTag; @@ -8,40 +7,37 @@ exports.selfClosingElement = ast.selfClosingElement; exports.text = ast.text; exports.forceWrite = ast.forceWrite; -function pathToNode(path, children) { - var result = ast.fragment(children); +function pathToNodes(path, children) { + var result = children; for (var index = path.length - 1; index >= 0; index--) { - result = ast.elementWithTag(path[index], [result]); + result = [ast.elementWithTag(path[index], result)]; } return result; } -exports.pathToNode = pathToNode; +exports.pathToNodes = pathToNodes; exports.simplify = require("./simplify"); -function write(writer, node) { +function write(writer, nodes) { + nodes.forEach(function(node) { + writeNode(writer, node); + }); +} + +function writeNode(writer, node) { toStrings[node.type](writer, node); } var toStrings = { - fragment: generateFragmentString, element: generateElementString, selfClosingElement: generateSelfClosingElementString, text: generateTextString, forceWrite: function() { } }; -function generateFragmentString(writer, node) { - node.children.forEach(function(child) { - write(writer, child); - }); -} - function generateElementString(writer, node) { writer.open(node.tag.tagName, node.tag.attributes); - node.children.forEach(function(child) { - write(writer, child); - }); + write(writer, node.children); writer.close(node.tag.tagName); } diff --git a/lib/html/simplify.js b/lib/html/simplify.js index 24a3447..081c3f8 100644 --- a/lib/html/simplify.js +++ b/lib/html/simplify.js @@ -1,79 +1,87 @@ +var _ = require("underscore"); + var ast = require("./ast"); -function simplify(node) { - return collapse(removeEmpty(node)); +function simplify(nodes) { + return collapse(removeEmpty(nodes)); +} + +function collapse(nodes) { + var children = []; + + nodes.map(collapseNode).forEach(function(child) { + appendChild(children, child); + }); + return children; } -function collapse(node) { +function collapseNode(node) { return collapsers[node.type](node); } var collapsers = { - fragment: collapseFragment, element: collapseElement, selfClosingElement: identity, text: identity, forceWrite: identity }; -function collapseFragment(node) { - return ast.fragment(collapseNodes(node.children)); -} - function collapseElement(node) { - return ast.elementWithTag(node.tag, collapseNodes(node.children)); + return ast.elementWithTag(node.tag, collapse(node.children)); } function identity(value) { return value; } -function collapseNodes(nodes) { - var children = []; - - nodes.map(collapse).forEach(function(child) { - appendChild(children, child, children.push.bind(children)); - }); - return children; -} - -function appendChild(children, child, append) { +function appendChild(children, child) { var lastChild = children[children.length - 1]; if (child.type === "element" && !child.tag.fresh && lastChild && lastChild.type === "element" && child.tag.matchesElement(lastChild.tag)) { child.children.forEach(function(grandChild) { // Mutation is fine since simplifying elements create a copy of the children. - appendChild(lastChild.children, grandChild, lastChild.appendChild.bind(lastChild)); + appendChild(lastChild.children, grandChild); }); } else { - append(child); + children.push(child); } } -function removeEmpty(node) { - return emptiers[node.type](node); +function removeEmpty(nodes) { + return flatMap(nodes, function(node) { + return emptiers[node.type](node); + }); +} + +function flatMap(values, func) { + return _.flatten(_.map(values, func), true); } var emptiers = { - fragment: fragmentEmptier, element: elementEmptier, - selfClosingElement: identity, - text: identity, - forceWrite: identity + selfClosingElement: neverEmpty, + text: textEmptier, + forceWrite: neverEmpty }; -function fragmentEmptier(fragment) { - return ast.fragment(listEmptier(fragment.children)); +function neverEmpty(node) { + return [node]; } function elementEmptier(element) { - return ast.elementWithTag(element.tag, listEmptier(element.children)); + var children = removeEmpty(element.children); + if (children.length === 0) { + return []; + } else { + return ast.elementWithTag(element.tag, children); + } } -function listEmptier(elements) { - return elements.map(removeEmpty).filter(function(child) { - return !child.isEmpty; - }); +function textEmptier(node) { + if (node.value.length === 0) { + return []; + } else { + return [node]; + } } - module.exports = simplify; diff --git a/lib/images.js b/lib/images.js index 7529673..6aac18a 100644 --- a/lib/images.js +++ b/lib/images.js @@ -10,7 +10,7 @@ exports.imgElement = function(func) { if (element.altText) { attributes.alt = element.altText; } - return Html.selfClosingElement("img", attributes); + return [Html.selfClosingElement("img", attributes)]; }).then(function(value) { callback(null, value); }, callback); diff --git a/tests/document-to-html.tests.js b/tests/document-to-html.tests.js index 103b921..cca4d7f 100644 --- a/tests/document-to-html.tests.js +++ b/tests/document-to-html.tests.js @@ -530,7 +530,7 @@ describe('DocumentConverter', function() { var converter = new DocumentConverter({ convertImage: function(element, messages, callback) { element.read("utf8").then(function(altText) { - callback(null, Html.selfClosingElement("img", {alt: altText})); + callback(null, [Html.selfClosingElement("img", {alt: altText})]); }); } }); diff --git a/tests/html/ast.tests.js b/tests/html/ast.tests.js deleted file mode 100644 index 04db5f7..0000000 --- a/tests/html/ast.tests.js +++ /dev/null @@ -1,20 +0,0 @@ -var assert = require("assert"); - -var test = require("../testing").test; -var html = require("../../lib/html"); - - -describe("HTML ast", function() { - test("nested fragments are collapsed", function() { - assert.deepEqual( - html.fragment([html.text("Hello")]), - html.fragment([html.fragment([html.text("Hello")])])); - }); - - test("children of elements are collapsed", function() { - assert.deepEqual( - html.freshElement("p", {}, [html.text("Hello")]), - html.freshElement("p", {}, [html.fragment([html.text("Hello")])])); - }); -}); - diff --git a/tests/html/simplify.tests.js b/tests/html/simplify.tests.js index db7beb4..ccc5610 100644 --- a/tests/html/simplify.tests.js +++ b/tests/html/simplify.tests.js @@ -1,46 +1,52 @@ var assert = require("assert"); +var _ = require("underscore"); + var test = require("../testing").test; var html = require("../../lib/html"); var htmlPaths = require("../../lib/html-paths"); - var nonFreshElement = html.nonFreshElement; -var fragment = html.fragment; var text = html.text; -var pathToNode = html.pathToNode; +var pathToNodes = html.pathToNodes; describe("simplify", function() { test("empty text nodes are removed", function() { assert.deepEqual( - html.simplify(fragment([text("")])), - fragment([])); + simplifyNode(text("")), + [] + ); }); test("elements with no children are removed", function() { assert.deepEqual( - html.simplify(fragment([nonFreshElement("p", {}, [])])), - fragment([])); + simplifyNode(nonFreshElement("p", {}, [])), + [] + ); }); test("elements only containing empty nodes are removed", function() { assert.deepEqual( - html.simplify(fragment([nonFreshElement("p", {}, [text("")])])), - fragment([])); + simplifyNode(nonFreshElement("p", {}, [text("")])), + [] + ); }); test("empty children of element are removed", function() { assert.deepEqual( - html.simplify(fragment([nonFreshElement("p", {}, [text("Hello"), text("")])])), - fragment([nonFreshElement("p", {}, [text("Hello")])])); + simplifyNode(nonFreshElement("p", {}, [text("Hello"), text("")])), + [nonFreshElement("p", {}, [text("Hello")])] + ); }); test("successive fresh elements are not collapsed", function() { var path = htmlPaths.elements([ - htmlPaths.element("p", {}, {fresh: true})]); - var original = fragment([ - pathToNode(path, [text("Hello")]), - pathToNode(path, [text(" there")])]); + htmlPaths.element("p", {}, {fresh: true}) + ]); + var original = concat( + pathToNodes(path, [text("Hello")]), + pathToNodes(path, [text(" there")]) + ); assert.deepEqual( html.simplify(original), @@ -49,13 +55,15 @@ describe("simplify", function() { test("successive plain non-fresh elements are collapsed if they have the same tag name", function() { var path = htmlPaths.elements([ - htmlPaths.element("p", {}, {fresh: false})]); + htmlPaths.element("p", {}, {fresh: false}) + ]); assert.deepEqual( - html.simplify(fragment([ - pathToNode(path, [text("Hello")]), - pathToNode(path, [text(" there")])])), - fragment([ - pathToNode(path, [text("Hello"), text(" there")])])); + html.simplify(concat( + pathToNodes(path, [text("Hello")]), + pathToNodes(path, [text(" there")]) + )), + pathToNodes(path, [text("Hello"), text(" there")]) + ); }); test("non-fresh can collapse into preceding fresh element", function() { @@ -64,19 +72,22 @@ describe("simplify", function() { var nonFreshPath = htmlPaths.elements([ htmlPaths.element("p", {}, {fresh: false})]); assert.deepEqual( - html.simplify(fragment([ - pathToNode(freshPath, [text("Hello")]), - pathToNode(nonFreshPath, [text(" there")])])), - fragment([ - pathToNode(freshPath, [text("Hello"), text(" there")])])); + html.simplify(concat( + pathToNodes(freshPath, [text("Hello")]), + pathToNodes(nonFreshPath, [text(" there")]) + )), + pathToNodes(freshPath, [text("Hello"), text(" there")]) + ); }); test("children of collapsed element can collapse with children of another collapsed element", function() { assert.deepEqual( - html.simplify(fragment([ + html.simplify([ nonFreshElement("blockquote", {}, [nonFreshElement("p", {}, [text("Hello")])]), - nonFreshElement("blockquote", {}, [nonFreshElement("p", {}, [text("there")])])])), - fragment([nonFreshElement("blockquote", {}, [nonFreshElement("p", {}, [text("Hello"), text("there")])])])); + nonFreshElement("blockquote", {}, [nonFreshElement("p", {}, [text("there")])]) + ]), + [nonFreshElement("blockquote", {}, [nonFreshElement("p", {}, [text("Hello"), text("there")])])] + ); }); test("empty elements are removed before collapsing", function() { @@ -85,11 +96,20 @@ describe("simplify", function() { var nonFreshPath = htmlPaths.elements([ htmlPaths.element("p", {}, {fresh: false})]); assert.deepEqual( - html.simplify(fragment([ - pathToNode(nonFreshPath, [text("Hello")]), - pathToNode(freshPath, []), - pathToNode(nonFreshPath, [text(" there")])])), - fragment([ - pathToNode(nonFreshPath, [text("Hello"), text(" there")])])); + html.simplify(concat( + pathToNodes(nonFreshPath, [text("Hello")]), + pathToNodes(freshPath, []), + pathToNodes(nonFreshPath, [text(" there")]) + )), + pathToNodes(nonFreshPath, [text("Hello"), text(" there")]) + ); }); }); + +function simplifyNode(node) { + return html.simplify([node]); +} + +function concat() { + return _.flatten(arguments, true); +} diff --git a/tests/html/write.tests.js b/tests/html/write.tests.js index e4a5813..531995f 100644 --- a/tests/html/write.tests.js +++ b/tests/html/write.tests.js @@ -39,6 +39,6 @@ describe("HTML write", function() { function generateString(node) { var writer = writers.writer(); - html.write(writer, node); + html.write(writer, [node]); return writer.asString(); } |
