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 /lib/document-to-html.js | |
| parent | 8617c85c8626354fff63eeaeb775402715fe63a4 (diff) | |
Use simpler structure for HTML without fragments
Diffstat (limited to 'lib/document-to-html.js')
| -rw-r--r-- | lib/document-to-html.js | 84 |
1 files changed, 44 insertions, 40 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 { |
