diff options
| author | Michael Williamson <mike@zwobble.org> | 2019-01-27 09:23:19 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2019-01-27 09:23:19 +0000 |
| commit | fcd3d606cfcd58e715705aaae0627a425e5484a5 (patch) | |
| tree | 282cf4389817d9393bd93ce95d11a25cc7576d8d /lib/document-to-html.js | |
| parent | 2d23b2933994c3c95ae108458021fd8ee6fcbb9b (diff) | |
Inline wrapChildrenInFreshElement
Diffstat (limited to 'lib/document-to-html.js')
| -rw-r--r-- | lib/document-to-html.js | 82 |
1 files changed, 39 insertions, 43 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js index 8d1078b..bb27476 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -26,24 +26,24 @@ function DocumentConverter(options) { function DocumentConversion(options, comments) { var noteNumber = 1; - + var noteReferences = []; - + var referencedComments = []; - + options = _.extend({ignoreEmptyParagraphs: true}, options); var idPrefix = options.idPrefix === undefined ? "" : options.idPrefix; var ignoreEmptyParagraphs = options.ignoreEmptyParagraphs; - + var defaultParagraphStyle = htmlPaths.topLevelElement("p"); - + var styleMap = options.styleMap || []; - + function convertToHtml(document) { var messages = []; - + var html = elementToHtml(document, messages, {}); - + var deferredNodes = []; walkHtml(html, function(node) { if (node.type === "deferred") { @@ -79,7 +79,7 @@ function DocumentConversion(options, comments) { return new results.Result(writer.asString(), messages); }); } - + function convertElements(elements, messages, options) { return flatMap(elements, function(element) { return elementToHtml(element, messages, options); @@ -97,7 +97,7 @@ function DocumentConversion(options, comments) { return []; } } - + function convertParagraph(element, messages, options) { return htmlPathForParagraph(element, messages).wrap(function() { var content = convertElements(element.children, messages, options); @@ -108,10 +108,10 @@ function DocumentConversion(options, comments) { } }); } - + function htmlPathForParagraph(element, messages) { var style = findStyle(element); - + if (style) { return style.to; } else { @@ -121,7 +121,7 @@ function DocumentConversion(options, comments) { return defaultParagraphStyle; } } - + function convertRun(run, messages, options) { var nodes = function() { return convertElements(run.children, messages, options); @@ -156,14 +156,14 @@ function DocumentConversion(options, comments) { messages.push(unrecognisedStyleWarning("run", run)); } paths.push(stylePath); - + paths.forEach(function(path) { nodes = path.wrap.bind(path, nodes); }); - + return nodes(); } - + function findHtmlPathForRunProperty(elementType, defaultTagName) { var path = findHtmlPath({type: elementType}); if (path) { @@ -174,12 +174,12 @@ function DocumentConversion(options, comments) { return htmlPaths.empty; } } - + function findHtmlPath(element, defaultPath) { var style = findStyle(element); return style ? style.to : defaultPath; } - + function findStyle(element) { for (var i = 0; i < styleMap.length; i++) { if (styleMap[i].from.matches(element)) { @@ -187,7 +187,7 @@ function DocumentConversion(options, comments) { } } } - + function recoveringConvertImage(convertImage) { return function(image, messages) { return promises.attempt(function() { @@ -202,23 +202,23 @@ function DocumentConversion(options, comments) { function noteHtmlId(note) { return referentHtmlId(note.noteType, note.noteId); } - + function noteRefHtmlId(note) { return referenceHtmlId(note.noteType, note.noteId); } - + function referentHtmlId(referenceType, referenceId) { return htmlId(referenceType + "-" + referenceId); } - + function referenceHtmlId(referenceType, referenceId) { return htmlId(referenceType + "-ref-" + referenceId); } - + function htmlId(suffix) { return idPrefix + suffix; } - + var defaultTablePath = htmlPaths.elements([ htmlPaths.element("table", {}, {fresh: true}) ]); @@ -261,11 +261,14 @@ function DocumentConversion(options, comments) { } return [Html.forceWrite].concat(children); } - + function convertTableRow(element, messages, options) { - return wrapChildrenInFreshElement(element, "tr", messages, options); + var children = convertElements(element.children, messages, options); + return [ + Html.freshElement("tr", {}, [Html.forceWrite].concat(children)) + ]; } - + function convertTableCell(element, messages, options) { var tagName = options.isTableHeader ? "th" : "td"; var children = convertElements(element.children, messages, options); @@ -276,12 +279,12 @@ function DocumentConversion(options, comments) { if (element.rowSpan !== 1) { attributes.rowspan = element.rowSpan.toString(); } - + return [ Html.freshElement(tagName, attributes, [Html.forceWrite].concat(children)) ]; } - + function convertCommentReference(reference, messages, options) { return findHtmlPath(reference, htmlPaths.ignore).wrap(function() { var comment = comments[reference.commentId]; @@ -297,10 +300,10 @@ function DocumentConversion(options, comments) { ]; }); } - + function convertComment(referencedComment, messages, options) { // TODO: remove duplication with note references - + var label = referencedComment.label; var comment = referencedComment.comment; var body = convertElements(comment.body, messages, options).concat([ @@ -311,7 +314,7 @@ function DocumentConversion(options, comments) { ]) ]) ]); - + return [ Html.freshElement( "dt", @@ -321,13 +324,13 @@ function DocumentConversion(options, comments) { Html.freshElement("dd", {}, body) ]; } - + function convertBreak(element, messages, options) { return htmlPathForBreak(element).wrap(function() { return []; }); } - + function htmlPathForBreak(element) { var style = findStyle(element); if (style) { @@ -338,13 +341,6 @@ function DocumentConversion(options, comments) { return htmlPaths.empty; } } - - function wrapChildrenInFreshElement(element, wrapElementName, messages, options) { - var children = convertElements(element.children, messages, options); - return [ - Html.freshElement(wrapElementName, {}, [Html.forceWrite].concat(children)) - ]; - } var elementConverters = { "document": function(document, messages, options) { @@ -390,7 +386,7 @@ function DocumentConversion(options, comments) { href: "#" + noteHtmlId(element), id: noteRefHtmlId(element) }, [Html.text("[" + (noteNumber++) + "]")]); - + return [Html.freshElement("sup", {}, [anchor])]; }, "note": function(element, messages, options) { @@ -400,7 +396,7 @@ function DocumentConversion(options, comments) { Html.freshElement("a", {href: "#" + noteRefHtmlId(element)}, [Html.text("↑")]) ]); var body = children.concat([backLink]); - + return Html.freshElement("li", {id: noteHtmlId(element)}, body); }, "commentReference": convertCommentReference, |
