diff options
| -rw-r--r-- | lib/document-to-html.js | 8 | ||||
| -rw-r--r-- | lib/documents.js | 37 | ||||
| -rw-r--r-- | lib/docx/document-xml-reader.js | 21 | ||||
| -rw-r--r-- | lib/docx/docx-reader.js | 12 | ||||
| -rw-r--r-- | lib/docx/notes-reader.js | 1 | ||||
| -rw-r--r-- | tests/document-to-html.tests.js | 5 | ||||
| -rw-r--r-- | tests/docx/document-xml-reader.tests.js | 6 |
7 files changed, 57 insertions, 33 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js index fa1c63b..030df32 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -200,7 +200,7 @@ function DocumentConversion(options) { html.closeAll(); html.open(htmlPaths.element("ol")); var footnotes = footnoteIds.map(function(id) { - return document.footnotes.findFootnoteById(id); + return document.notes.findFootnoteById(id); }); convertElements(footnotes, html, messages, function(error) { if (error) { @@ -248,17 +248,17 @@ function DocumentConversion(options) { html.close(); callback(); }, - "footnote": function(element, html, messages, callback) { + "note": function(element, html, messages, callback) { var uid = footnoteUid(element.id); - html.open(htmlPaths.element("li", {id: "footnote-" + uid})); + html.open(htmlPaths.element("li", {id: element.noteType + "-" + uid})); var footnoteHtml = new HtmlGenerator(); convertElements(element.body, footnoteHtml, messages, function(err) { if (err) { callback(err); } else { footnoteHtml.text(" "); - footnoteHtml.open(htmlPaths.element("a", {href: "#footnote-ref-" + uid})); + footnoteHtml.open(htmlPaths.element("a", {href: "#" + element.noteType + "-ref-" + uid})); footnoteHtml.text("↑"); footnoteHtml.closeAll(); html.append(footnoteHtml); diff --git a/lib/documents.js b/lib/documents.js index b0959cc..aea2b39 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -1,3 +1,5 @@ +var _ = require("underscore"); + var types = { document: "document", paragraph: "paragraph", @@ -7,7 +9,7 @@ var types = { hyperlink: "hyperlink", footnoteReference: "footnoteReference", image: "image", - footnote: "footnote", + note: "note", table: "table", tableRow: "tableRow", tableCell: "tableCell", @@ -19,7 +21,7 @@ function Document(children, options) { return { type: types.document, children: children, - footnotes: options.footnotes || new Footnotes({}) + notes: options.notes || new Notes({}) }; } @@ -83,22 +85,33 @@ function FootnoteReference(options) { }; } -function Footnotes(footnotes) { - this._footnotes = footnotes; +function Notes(notes) { + this._notes = _.indexBy(notes, function(note) { + return noteKey(note.noteType, note.id); + }); } -Footnotes.prototype.findFootnoteById = function(id) { - return this._footnotes[id] || null; +Notes.prototype.findFootnoteById = function(id) { + return this.findNoteByKey(noteKey("footnote", id)); +}; + +Notes.prototype.findNoteByKey = function(key) { + return this._notes[key] || null; }; -function Footnote(options) { +function Note(options) { return { - type: types.footnote, - body: options.body, - id: options.id + type: types.note, + noteType: options.noteType, + id: options.id, + body: options.body }; } +function noteKey(noteType, id) { + return noteType + "-" + id; +} + function Image(options) { return { type: types.image, @@ -142,8 +155,8 @@ exports.Text = Text; exports.Tab = Tab; exports.Hyperlink = Hyperlink; exports.FootnoteReference = FootnoteReference; -exports.Footnotes = Footnotes; -exports.Footnote = Footnote; +exports.Notes = Notes; +exports.Note = Note; exports.Image = Image; exports.Table = Table; exports.TableRow = TableRow; diff --git a/lib/docx/document-xml-reader.js b/lib/docx/document-xml-reader.js index 97241a7..c41a29f 100644 --- a/lib/docx/document-xml-reader.js +++ b/lib/docx/document-xml-reader.js @@ -13,7 +13,7 @@ function DocumentXmlReader(options) { var docxFile = options.docxFile; var numbering = options.numbering; var styles = options.styles; - var rawFootnotes = options.footnotes || []; + var rawNotes = (options.footnotes || []).concat(options.endnotes || []); function convertXmlToDocument(documentXml) { @@ -21,24 +21,25 @@ function DocumentXmlReader(options) { var result = readXmlElements(body.children) .flatMap(function(children) { - return readFootnotes().map(function(footnotes) { - return new documents.Document(children, {footnotes: footnotes}); + return readNotes().map(function(notes) { + return new documents.Document(children, {notes: notes}); }); }); result.document = result.value; return result; } - function readFootnotes() { - return Result.combine(rawFootnotes.map(function(rawFootnote) { - return readXmlElements(rawFootnote.body).map(function(body) { - return new documents.Footnote({ - id: rawFootnote.id, + function readNotes() { + return Result.combine(rawNotes.map(function(rawNote) { + return readXmlElements(rawNote.body).map(function(body) { + return new documents.Note({ + noteType: rawNote.noteType, + id: rawNote.id, body: body }); }); - })).map(function(footnotes) { - return new documents.Footnotes(_.indexBy(footnotes, "id")); + })).map(function(notes) { + return new documents.Notes(notes, "key"); }); } diff --git a/lib/docx/docx-reader.js b/lib/docx/docx-reader.js index 9326588..23f821c 100644 --- a/lib/docx/docx-reader.js +++ b/lib/docx/docx-reader.js @@ -21,15 +21,17 @@ function read(options) { readNumberingFromZipFile(docxFile), readStylesFromZipFile(docxFile), readFootnotesFromZipFile(docxFile), + readEndnotesFromZipFile(docxFile), readXmlFromZipFile(docxFile, "word/document.xml"), - ]).spread(function(relationships, contentTypes, numbering, styles, footnotes, documentXml) { + ]).spread(function(relationships, contentTypes, numbering, styles, footnotes, endnotes, documentXml) { var reader = new DocumentXmlReader({ relationships: relationships, contentTypes: contentTypes, docxFile: docxFile, numbering: numbering, styles: styles, - footnotes: footnotes + footnotes: footnotes, + endnotes: endnotes }); return reader.convertXmlToDocument(documentXml); }); @@ -74,3 +76,9 @@ var readFootnotesFromZipFile = xmlFileReader({ readElement: notesReader.readFootnotesXml, defaultValue: undefined }); + +var readEndnotesFromZipFile = xmlFileReader({ + filename: "word/endnotes.xml", + readElement: notesReader.readEndnotesXml, + defaultValue: undefined +}); diff --git a/lib/docx/notes-reader.js b/lib/docx/notes-reader.js index 00bcce5..98cbba9 100644 --- a/lib/docx/notes-reader.js +++ b/lib/docx/notes-reader.js @@ -1,4 +1,5 @@ exports.readFootnotesXml = createReader("footnote"); +exports.readEndnotesXml = createReader("endnote"); function createReader(noteType) { function readNotesXml(xml) { diff --git a/tests/document-to-html.tests.js b/tests/document-to-html.tests.js index a1c7a61..a5cf294 100644 --- a/tests/document-to-html.tests.js +++ b/tests/document-to-html.tests.js @@ -306,8 +306,9 @@ describe('DocumentConverter', function() { new documents.Run([footnoteReference]) ])], { - footnotes: new documents.Footnotes({ - 4: new documents.Footnote({ + notes: new documents.Notes({ + 4: new documents.Note({ + noteType: "footnote", id: "4", body: [paragraphOfText("Who's there?")] }) diff --git a/tests/docx/document-xml-reader.tests.js b/tests/docx/document-xml-reader.tests.js index 175b3c4..778bf06 100644 --- a/tests/docx/document-xml-reader.tests.js +++ b/tests/docx/document-xml-reader.tests.js @@ -353,15 +353,15 @@ describe("readXmlElement: ", function() { describe("convertXmlToDocument: ", function() { - test("footnotes of document are read", function() { + test("notes of document are read", function() { var paragraphXml = new XmlElement("w:p", {}, []); - var footnotes = [{id: "4", body: [paragraphXml]}]; + var footnotes = [{noteType: "footnote", id: "4", body: [paragraphXml]}]; var bodyXml = new XmlElement("w:body", {}, []); var documentXml = new XmlElement("w:document", {}, [bodyXml]); var document = convertXmlToDocumentValue({root: documentXml}, {footnotes: footnotes}); - var footnote = document.footnotes.findFootnoteById("4"); + var footnote = document.notes.findFootnoteById("4"); assert.deepEqual(footnote.id, "4"); assert.deepEqual(footnote.body[0].type, "paragraph"); }); |
