aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2017-05-24 20:57:07 +0100
committerMichael Williamson <mike@zwobble.org>2017-05-24 20:57:07 +0100
commitebedc67a1fe3203c7839aea523a6dbf5a1c89a15 (patch)
treea65c75352a5a982022911de37adc7ba538f6224f /lib
parent3d8d9c116f3f939abbfbfd28bee6bcca7a5ba076 (diff)
Pass through options during conversion
Diffstat (limited to 'lib')
-rw-r--r--lib/document-to-html.js59
1 files changed, 31 insertions, 28 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js
index 5e7079a..d2d0a24 100644
--- a/lib/document-to-html.js
+++ b/lib/document-to-html.js
@@ -33,6 +33,7 @@ function DocumentConversion(options, comments) {
options = _.extend({ignoreEmptyParagraphs: true}, options);
var idPrefix = options.idPrefix === undefined ? "" : options.idPrefix;
+ var ignoreEmptyParagraphs = options.ignoreEmptyParagraphs;
var defaultParagraphStyle = htmlPaths.topLevelElement("p");
@@ -41,7 +42,7 @@ function DocumentConversion(options, comments) {
function convertToHtml(document) {
var messages = [];
- var html = elementToHtml(document, messages);
+ var html = elementToHtml(document, messages, {});
var deferredNodes = [];
walkHtml(html, function(node) {
@@ -86,7 +87,9 @@ function DocumentConversion(options, comments) {
}
function elementToHtml(element, messages, options) {
- options = options || {};
+ if (!options) {
+ throw new Error();
+ }
var handler = elementConverters[element.type];
if (handler) {
return handler(element, messages, options);
@@ -95,10 +98,10 @@ function DocumentConversion(options, comments) {
}
}
- function convertParagraph(element, messages) {
+ function convertParagraph(element, messages, options) {
return htmlPathForParagraph(element, messages).wrap(function() {
- var content = convertElements(element.children, messages);
- if (options.ignoreEmptyParagraphs) {
+ var content = convertElements(element.children, messages, options);
+ if (ignoreEmptyParagraphs) {
return content;
} else {
return [Html.forceWrite].concat(content);
@@ -119,9 +122,9 @@ function DocumentConversion(options, comments) {
}
}
- function convertRun(run, messages) {
+ function convertRun(run, messages, options) {
var nodes = function() {
- return convertElements(run.children, messages);
+ return convertElements(run.children, messages, options);
};
var paths = [];
if (run.isStrikethrough) {
@@ -213,7 +216,7 @@ function DocumentConversion(options, comments) {
return idPrefix + suffix;
}
- function convertTable(element, messages) {
+ function convertTable(element, messages, options) {
var bodyIndex = _.findIndex(element.children, function(child) {
return !child.isHeader;
});
@@ -225,18 +228,18 @@ function DocumentConversion(options, comments) {
children = convertElements(
element.children,
messages,
- {isTableHeader: false}
+ _.extend({}, options, {isTableHeader: false})
);
} else {
var headRows = convertElements(
element.children.slice(0, bodyIndex),
messages,
- {isTableHeader: true}
+ _.extend({}, options, {isTableHeader: true})
);
var bodyRows = convertElements(
element.children.slice(bodyIndex),
messages,
- {isTableHeader: false}
+ _.extend({}, options, {isTableHeader: false})
);
children = [
Html.freshElement("thead", {}, headRows),
@@ -254,7 +257,7 @@ function DocumentConversion(options, comments) {
function convertTableCell(element, messages, options) {
var tagName = options.isTableHeader ? "th" : "td";
- var children = convertElements(element.children, messages);
+ var children = convertElements(element.children, messages, options);
var attributes = {};
if (element.colSpan !== 1) {
attributes.colspan = element.colSpan.toString();
@@ -268,7 +271,7 @@ function DocumentConversion(options, comments) {
];
}
- function convertCommentReference(reference, messages) {
+ function convertCommentReference(reference, messages, options) {
return findHtmlPath(reference, htmlPaths.ignore).wrap(function() {
var comment = comments[reference.commentId];
var count = referencedComments.length + 1;
@@ -284,12 +287,12 @@ function DocumentConversion(options, comments) {
});
}
- function convertComment(referencedComment, messages) {
+ function convertComment(referencedComment, messages, options) {
// TODO: remove duplication with note references
var label = referencedComment.label;
var comment = referencedComment.comment;
- var body = convertElements(comment.body).concat([
+ var body = convertElements(comment.body, messages, options).concat([
Html.nonFreshElement("p", {}, [
Html.text(" "),
Html.freshElement("a", {"href": "#" + referenceHtmlId("comment", comment.commentId)}, [
@@ -308,7 +311,7 @@ function DocumentConversion(options, comments) {
];
}
- function convertBreak(element, messages) {
+ function convertBreak(element, messages, options) {
return htmlPathForBreak(element).wrap(function() {
return [];
});
@@ -333,40 +336,40 @@ function DocumentConversion(options, comments) {
}
var elementConverters = {
- "document": function(document, messages) {
- var children = convertElements(document.children, messages);
+ "document": function(document, messages, options) {
+ var children = convertElements(document.children, messages, options);
var notes = noteReferences.map(function(noteReference) {
return document.notes.resolve(noteReference);
});
- var notesNodes = convertElements(notes, messages);
+ var notesNodes = convertElements(notes, messages, options);
return children.concat([
Html.freshElement("ol", {}, notesNodes),
Html.freshElement("dl", {}, flatMap(referencedComments, function(referencedComment) {
- return convertComment(referencedComment, messages);
+ return convertComment(referencedComment, messages, options);
}))
]);
},
"paragraph": convertParagraph,
"run": convertRun,
- "text": function(element, messages) {
+ "text": function(element, messages, options) {
return [Html.text(element.value)];
},
- "tab": function(element, messages) {
+ "tab": function(element, messages, options) {
return [Html.text("\t")];
},
- "hyperlink": function(element, messages) {
+ "hyperlink": function(element, messages, options) {
var href = element.anchor ? "#" + htmlId(element.anchor) : element.href;
- var children = convertElements(element.children, messages);
+ var children = convertElements(element.children, messages, options);
return [Html.freshElement("a", {href: href}, children)];
},
- "bookmarkStart": function(element, messages) {
+ "bookmarkStart": function(element, messages, options) {
var anchor = Html.freshElement("a", {
id: htmlId(element.name)
}, [Html.forceWrite]);
return [anchor];
},
- "noteReference": function(element, messages) {
+ "noteReference": function(element, messages, options) {
noteReferences.push(element);
var anchor = Html.freshElement("a", {
href: "#" + noteHtmlId(element),
@@ -375,8 +378,8 @@ function DocumentConversion(options, comments) {
return [Html.freshElement("sup", {}, [anchor])];
},
- "note": function(element, messages) {
- var children = convertElements(element.body, messages);
+ "note": function(element, messages, options) {
+ var children = convertElements(element.body, messages, options);
var backLink = Html.elementWithTag(htmlPaths.element("p", {}, {fresh: false}), [
Html.text(" "),
Html.freshElement("a", {href: "#" + noteRefHtmlId(element)}, [Html.text("↑")])