aboutsummaryrefslogtreecommitdiff
path: root/lib
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
parent3047481420f215fb60b0380fa187975948ba54be (diff)
Store children in thunks when generating HTML nodes
Diffstat (limited to 'lib')
-rw-r--r--lib/document-to-html.js80
-rw-r--r--lib/html-paths.js26
-rw-r--r--lib/html/index.js9
3 files changed, 63 insertions, 52 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;
}
}
diff --git a/lib/html-paths.js b/lib/html-paths.js
index 04bc655..4021887 100644
--- a/lib/html-paths.js
+++ b/lib/html-paths.js
@@ -1,5 +1,7 @@
var _ = require("underscore");
+var html = require("./html");
+
exports.topLevelElement = topLevelElement;
exports.elements = elements;
exports.element = element;
@@ -9,15 +11,27 @@ function topLevelElement(tagName, attributes) {
}
function elements(elementStyles) {
- return elementStyles.map(function(elementStyle) {
+ return new HtmlPath(elementStyles.map(function(elementStyle) {
if (_.isString(elementStyle)) {
return element(elementStyle);
} else {
return elementStyle;
}
- });
+ }));
+}
+
+function HtmlPath(elements) {
+ this._elements = elements;
}
+HtmlPath.prototype.wrap = function wrap(children) {
+ var result = children();
+ for (var index = this._elements.length - 1; index >= 0; index--) {
+ result = this._elements[index].wrapNodes(result);
+ }
+ return result;
+};
+
function element(tagName, attributes, options) {
options = options || {};
return new Element(tagName, attributes, options);
@@ -44,4 +58,12 @@ Element.prototype.matchesElement = function(element) {
return this.tagNames[element.tagName] && _.isEqual(this.attributes || {}, element.attributes || {});
};
+Element.prototype.wrap = function wrap(generateNodes) {
+ return this.wrapNodes(generateNodes());
+};
+
+Element.prototype.wrapNodes = function wrapNodes(nodes) {
+ return [html.elementWithTag(this, nodes)];
+};
+
exports.empty = elements([]);
diff --git a/lib/html/index.js b/lib/html/index.js
index edc8256..600d783 100644
--- a/lib/html/index.js
+++ b/lib/html/index.js
@@ -7,15 +7,6 @@ exports.selfClosingElement = ast.selfClosingElement;
exports.text = ast.text;
exports.forceWrite = ast.forceWrite;
-function pathToNodes(path, children) {
- var result = children;
- for (var index = path.length - 1; index >= 0; index--) {
- result = [ast.elementWithTag(path[index], result)];
- }
- return result;
-}
-
-exports.pathToNodes = pathToNodes;
exports.simplify = require("./simplify");
function write(writer, nodes) {