From b5768bced10c6dafe8bc621c6ef01aa0bd9d975c Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 7 Jul 2016 20:25:41 +0100 Subject: Store children in thunks when generating HTML nodes --- lib/document-to-html.js | 80 ++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 41 deletions(-) (limited to 'lib/document-to-html.js') 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; } } -- cgit v1.2.3