aboutsummaryrefslogtreecommitdiff
path: root/lib/document-to-html.js
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-07-07 20:25:41 +0100
committerMichael Williamson <mike@zwobble.org>2016-07-07 20:25:41 +0100
commitb5768bced10c6dafe8bc621c6ef01aa0bd9d975c (patch)
treef6cd37ba5dad336f72d6a7e9c688eb177de0c23f /lib/document-to-html.js
parent3047481420f215fb60b0380fa187975948ba54be (diff)
Store children in thunks when generating HTML nodes
Diffstat (limited to 'lib/document-to-html.js')
-rw-r--r--lib/document-to-html.js80
1 files changed, 39 insertions, 41 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js
index f2d5229..2b7815a 100644
--- a/lib/document-to-html.js
+++ b/lib/document-to-html.js
@@ -89,16 +89,17 @@ function DocumentConversion(options) {
}
function convertParagraph(element, messages) {
- var style = styleForParagraph(element, messages);
-
- var children = convertElements(element.children, messages);
- if (!options.ignoreEmptyParagraphs) {
- children = [Html.forceWrite].concat(children);
- }
- return Html.pathToNodes(style, children);
+ return htmlPathForParagraph(element, messages).wrap(function() {
+ var content = convertElements(element.children, messages);
+ if (options.ignoreEmptyParagraphs) {
+ return content;
+ } else {
+ return [Html.forceWrite].concat(content);
+ }
+ });
}
- function styleForParagraph(element, messages) {
+ function htmlPathForParagraph(element, messages) {
var style = findStyle(element);
if (style) {
@@ -112,57 +113,54 @@ function DocumentConversion(options) {
}
function convertRun(run, messages) {
- var style;
- if (run.styleId) {
- style = findStyle(run);
- if (!style) {
- messages.push(unrecognisedStyleWarning("run", run));
- }
- }
- var result = convertElements(run.children, messages);
+ var nodes = function() {
+ return convertElements(run.children, messages);
+ };
+ var paths = [];
if (run.isStrikethrough) {
- result = convertRunProperty(result, "strikethrough", "s");
+ paths.push(findHtmlPathForRunProperty("strikethrough", "s"));
}
if (run.isUnderline) {
- result = convertUnderline(result);
+ paths.push(findHtmlPathForRunProperty("underline"));
}
if (run.verticalAlignment === documents.verticalAlignment.subscript) {
- result = wrapInNonFreshElement(result, "sub");
+ paths.push(htmlPaths.element("sub", {}, {fresh: false}));
}
if (run.verticalAlignment === documents.verticalAlignment.superscript) {
- result = wrapInNonFreshElement(result, "sup");
+ paths.push(htmlPaths.element("sup", {}, {fresh: false}));
}
if (run.isItalic) {
- result = convertRunProperty(result, "italic", "em");
+ paths.push(findHtmlPathForRunProperty("italic", "em"));
}
if (run.isBold) {
- result = convertRunProperty(result, "bold", "strong");
+ paths.push(findHtmlPathForRunProperty("bold", "strong"));
}
- if (style) {
- result = Html.pathToNodes(style.to, result);
- }
- return result;
- }
-
- function wrapInNonFreshElement(nodes, tagName) {
- return [Html.nonFreshElement(tagName, {}, nodes)];
- }
-
- function convertUnderline(runHtml) {
- var style = findStyle({type: "underline"});
- if (style) {
- return Html.pathToNodes(style.to, runHtml);
- } else {
- return runHtml;
+ var stylePath = htmlPaths.empty;
+ if (run.styleId) {
+ var style = findStyle(run);
+ if (style) {
+ stylePath = style.to;
+ } else {
+ messages.push(unrecognisedStyleWarning("run", run));
+ }
}
+ paths.push(stylePath);
+
+ paths.forEach(function(path) {
+ nodes = path.wrap.bind(path, nodes);
+ });
+
+ return nodes();
}
- function convertRunProperty(result, elementType, defaultTagName) {
+ function findHtmlPathForRunProperty(elementType, defaultTagName) {
var style = findStyle({type: elementType});
if (style) {
- return Html.pathToNodes(style.to, result);
+ return style.to;
+ } else if (defaultTagName) {
+ return htmlPaths.element(defaultTagName, {}, {fresh: false});
} else {
- return wrapInNonFreshElement(result, defaultTagName);
+ return htmlPaths.empty;
}
}