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 From c0a14c171f5bdfda6cb76b1589a43ada3b5768ce Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 7 Jul 2016 21:27:53 +0100 Subject: Add conversion of comments --- lib/document-matchers.js | 2 +- lib/document-to-html.js | 88 +++++++++++++++++++++++++++++++++++++----- lib/documents.js | 3 +- test/document-to-html.tests.js | 32 ++++++++++++++- 4 files changed, 112 insertions(+), 13 deletions(-) (limited to 'lib/document-to-html.js') diff --git a/lib/document-matchers.js b/lib/document-matchers.js index 60bc65f..bdc213c 100644 --- a/lib/document-matchers.js +++ b/lib/document-matchers.js @@ -4,7 +4,7 @@ exports.bold = new Matcher("bold"); exports.italic = new Matcher("italic"); exports.underline = new Matcher("underline"); exports.strikethrough = new Matcher("strikethrough"); -exports.commentReference = new Matcher("comment-reference"); +exports.commentReference = new Matcher("commentReference"); function paragraph(options) { diff --git a/lib/document-to-html.js b/lib/document-to-html.js index 2b7815a..1b3a3df 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -13,18 +13,25 @@ exports.DocumentConverter = DocumentConverter; function DocumentConverter(options) { return { - convertToHtml: function() { - var conversion = new DocumentConversion(options); - return conversion.convertToHtml.apply(this, arguments); + convertToHtml: function(element) { + var comments = _.indexBy( + element.type === documents.types.document ? element.comments : [], + "commentId" + ); + var conversion = new DocumentConversion(options, comments); + return conversion.convertToHtml(element); } }; } -function DocumentConversion(options) { +function DocumentConversion(options, comments) { var noteNumber = 1; var noteReferences = []; + var referencedComments = []; + var commentCountByInitials = {}; + options = _.extend({ignoreEmptyParagraphs: true}, options); var idPrefix = options.idPrefix === undefined ? "" : options.idPrefix; @@ -154,9 +161,9 @@ function DocumentConversion(options) { } function findHtmlPathForRunProperty(elementType, defaultTagName) { - var style = findStyle({type: elementType}); - if (style) { - return style.to; + var path = findHtmlPath({type: elementType}); + if (path) { + return path; } else if (defaultTagName) { return htmlPaths.element(defaultTagName, {}, {fresh: false}); } else { @@ -164,6 +171,11 @@ function DocumentConversion(options) { } } + 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)) { @@ -190,11 +202,19 @@ function DocumentConversion(options) { } function noteHtmlId(note) { - return htmlId(note.noteType + "-" + note.noteId); + return referentHtmlId(note.noteType, note.noteId); } function noteRefHtmlId(note) { - return htmlId(note.noteType + "-ref-" + note.noteId); + 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) { @@ -224,6 +244,49 @@ function DocumentConversion(options) { ]; } + function convertCommentReference(reference, messages) { + return findHtmlPath(reference, htmlPaths.ignore).wrap(function() { + var comment = comments[reference.commentId]; + if (commentCountByInitials[comment.authorInitials] === undefined) { + commentCountByInitials[comment.authorInitials] = 0; + } + var count = ++commentCountByInitials[comment.authorInitials]; + var label = "[" + comment.authorInitials + count + "]"; + referencedComments.push({label: label, comment: comment}); + // TODO: remove duplication with note references + return [ + Html.freshElement("a", { + href: "#" + referentHtmlId("comment", reference.commentId), + id: referenceHtmlId("comment", reference.commentId) + }, [Html.text(label)]) + ]; + }); + } + + function convertComment(referencedComment, messages) { + // TODO: remove duplication with note references + + var label = referencedComment.label; + var comment = referencedComment.comment; + var body = convertElements(comment.body).concat([ + Html.nonFreshElement("p", {}, [ + Html.text(" "), + Html.freshElement("a", {"href": "#" + referenceHtmlId("comment", comment.commentId)}, [ + Html.text("↑") + ]) + ]) + ]); + + return [ + Html.freshElement( + "dt", + {"id": referentHtmlId("comment", comment.commentId)}, + [Html.text("Comment " + label)] + ), + Html.freshElement("dd", {}, body) + ]; + } + function wrapChildrenInFreshElement(element, wrapElementName, messages) { var children = convertElements(element.children, messages); return [ @@ -239,7 +302,10 @@ function DocumentConversion(options) { }); var notesNodes = convertElements(notes, messages); return children.concat([ - Html.freshElement("ol", {}, notesNodes) + Html.freshElement("ol", {}, notesNodes), + Html.freshElement("dl", {}, flatMap(referencedComments, function(referencedComment) { + return convertComment(referencedComment, messages); + })) ]); }, "paragraph": convertParagraph, @@ -281,6 +347,8 @@ function DocumentConversion(options) { return Html.freshElement("li", {id: noteHtmlId(element)}, body); }, + "commentReference": convertCommentReference, + "comment": convertComment, "image": deferredConversion(recoveringConvertImage(options.convertImage || defaultConvertImage)), "table": convertTable, "tableRow": convertTableRow, diff --git a/lib/documents.js b/lib/documents.js index 9f01d88..428dc09 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -24,7 +24,8 @@ function Document(children, options) { return { type: types.document, children: children, - notes: options.notes || new Notes({}) + notes: options.notes || new Notes({}), + comments: options.comments || [] }; } diff --git a/test/document-to-html.tests.js b/test/document-to-html.tests.js index 436014f..af08050 100644 --- a/test/document-to-html.tests.js +++ b/test/document-to-html.tests.js @@ -486,7 +486,7 @@ test('footnotes are included after the main body', function() { }); test('comments are ignored by default', function() { - var reference = documents.commentReference("4"); + var reference = documents.commentReference({commentId: "4"}); var comment = documents.comment({ commentId: "4", body: [paragraphOfText("Who's there?")] @@ -503,7 +503,37 @@ test('comments are ignored by default', function() { assert.equal(result.value, '

Knock knock

'); assert.deepEqual(result.messages, []); }); +}); +test('comment references are linked to comment after main body', function() { + var reference = documents.commentReference({commentId: "4"}); + var comment = documents.comment({ + commentId: "4", + body: [paragraphOfText("Who's there?")], + authorName: "The Piemaker", + authorInitials: "TP" + }); + var document = documents.document([ + documents.paragraph([ + runOfText("Knock knock"), + documents.run([reference]) + ]) + ], {comments: [comment]}); + + var converter = new DocumentConverter({ + idPrefix: "doc-42-", + styleMap: [ + {from: documentMatchers.commentReference, to: htmlPaths.element("sup")} + ] + }); + return converter.convertToHtml(document).then(function(result) { + var expectedHtml = ( + '

Knock knock[TP1]

' + + '
Comment [TP1]

Who\'s there? ↑

' + ); + assert.equal(result.value, expectedHtml); + assert.deepEqual(result.messages, []); + }); }); test('images are written with data URIs', function() { -- cgit v1.2.3 From 30d04c156a26f40cb7f4cf72d66c3f29bb87ff60 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 7 Jul 2016 22:27:20 +0100 Subject: Use overall comment count for comment numbering This matches the behaviour in Word more closely. --- lib/document-to-html.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'lib/document-to-html.js') diff --git a/lib/document-to-html.js b/lib/document-to-html.js index 1b3a3df..eb7c8e1 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -30,7 +30,6 @@ function DocumentConversion(options, comments) { var noteReferences = []; var referencedComments = []; - var commentCountByInitials = {}; options = _.extend({ignoreEmptyParagraphs: true}, options); var idPrefix = options.idPrefix === undefined ? "" : options.idPrefix; @@ -247,10 +246,7 @@ function DocumentConversion(options, comments) { function convertCommentReference(reference, messages) { return findHtmlPath(reference, htmlPaths.ignore).wrap(function() { var comment = comments[reference.commentId]; - if (commentCountByInitials[comment.authorInitials] === undefined) { - commentCountByInitials[comment.authorInitials] = 0; - } - var count = ++commentCountByInitials[comment.authorInitials]; + var count = referencedComments.length + 1; var label = "[" + comment.authorInitials + count + "]"; referencedComments.push({label: label, comment: comment}); // TODO: remove duplication with note references -- cgit v1.2.3 From ade35ac659f20fc27bb4c87d8511789cc7d05c07 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 7 Jul 2016 22:32:35 +0100 Subject: Handle comments without initials --- lib/document-to-html.js | 6 +++++- test/document-to-html.tests.js | 14 +++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) (limited to 'lib/document-to-html.js') diff --git a/lib/document-to-html.js b/lib/document-to-html.js index eb7c8e1..bc61e34 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -247,7 +247,7 @@ function DocumentConversion(options, comments) { return findHtmlPath(reference, htmlPaths.ignore).wrap(function() { var comment = comments[reference.commentId]; var count = referencedComments.length + 1; - var label = "[" + comment.authorInitials + count + "]"; + var label = "[" + commentAuthorLabel(comment) + count + "]"; referencedComments.push({label: label, comment: comment}); // TODO: remove duplication with note references return [ @@ -393,3 +393,7 @@ function walkHtml(nodes, callback) { } }); } + +var commentAuthorLabel = exports.commentAuthorLabel = function commentAuthorLabel(comment) { + return comment.authorInitials || ""; +}; diff --git a/test/document-to-html.tests.js b/test/document-to-html.tests.js index af08050..cc8aff5 100644 --- a/test/document-to-html.tests.js +++ b/test/document-to-html.tests.js @@ -2,7 +2,9 @@ var assert = require("assert"); var promises = require("../lib/promises"); var documents = require("../lib/documents"); -var DocumentConverter = require("../lib/document-to-html").DocumentConverter; +var documentToHtml = require("../lib/document-to-html"); +var DocumentConverter = documentToHtml.DocumentConverter; +var commentAuthorLabel = documentToHtml.commentAuthorLabel; var test = require("./test")(module); var htmlPaths = require("../lib/html-paths"); var xml = require("../lib/xml"); @@ -612,3 +614,13 @@ function runOfText(text, properties) { var textElement = new documents.Text(text); return new documents.Run([textElement], properties); } + +test('when initials are not blank then comment author label is initials', function() { + assert.equal(commentAuthorLabel({authorInitials: "TP"}), "TP") +}); + +test('when initials are blank then comment author label is blank', function() { + assert.equal(commentAuthorLabel({authorInitials: ""}), "") + assert.equal(commentAuthorLabel({authorInitials: undefined}), "") + assert.equal(commentAuthorLabel({authorInitials: null}), "") +}); -- cgit v1.2.3