aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-06-09 22:32:30 +0100
committerMichael Williamson <mike@zwobble.org>2016-06-20 12:06:19 +0100
commitac3981df81148fad21c32b22496ecc145ad36e98 (patch)
treecfba69d2fedb0cacc7d544749f199bd339a1c6ad /lib
parent8617c85c8626354fff63eeaeb775402715fe63a4 (diff)
Use simpler structure for HTML without fragments
Diffstat (limited to 'lib')
-rw-r--r--lib/document-to-html.js84
-rw-r--r--lib/html/ast.js33
-rw-r--r--lib/html/index.js28
-rw-r--r--lib/html/simplify.js78
-rw-r--r--lib/images.js2
5 files changed, 103 insertions, 122 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);