diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-01-19 23:05:45 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-01-19 23:05:45 +0000 |
| commit | 965bd9513da033b9804af8ec29c7fa609a84512c (patch) | |
| tree | 28027622b24a00bb44fd4a1e9c91462493d78989 /lib | |
| parent | 58cae6030e51481ffb462e52ab6aeb479e0a705d (diff) | |
Add support for endnotes
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/document-to-html.js | 26 | ||||
| -rw-r--r-- | lib/documents.js | 15 | ||||
| -rw-r--r-- | lib/docx/document-xml-reader.js | 18 |
3 files changed, 33 insertions, 26 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js index 030df32..eec329f 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -28,9 +28,9 @@ function DocumentConverter(options) { } function DocumentConversion(options) { - var footnoteNumber = 1; + var noteNumber = 1; - var footnoteIds = []; + var noteReferences = []; options = options || {}; var generateUniquifier = options.generateUniquifier || function() { @@ -140,7 +140,7 @@ function DocumentConversion(options) { }); }); - function footnoteUid(id) { + function noteUid(id) { return uniquifier + "-" + id; } @@ -199,10 +199,10 @@ function DocumentConversion(options) { } else { html.closeAll(); html.open(htmlPaths.element("ol")); - var footnotes = footnoteIds.map(function(id) { - return document.notes.findFootnoteById(id); + var notes = noteReferences.map(function(noteReference) { + return document.notes.resolve(noteReference); }); - convertElements(footnotes, html, messages, function(error) { + convertElements(notes, html, messages, function(error) { if (error) { callback(error); } else { @@ -234,22 +234,22 @@ function DocumentConversion(options) { } }); }, - "footnoteReference": function(element, html, messages, callback) { - var uid = footnoteUid(element.footnoteId); + "noteReference": function(element, html, messages, callback) { + var uid = noteUid(element.noteId); html.open(htmlPaths.element("sup")); html.open(htmlPaths.element("a", { - href: "#footnote-" + uid, - id: "footnote-ref-" + uid + href: "#" + element.noteType + "-" + uid, + id: element.noteType + "-ref-" + uid })); - footnoteIds.push(element.footnoteId); - html.text("[" + (footnoteNumber++) + "]"); + noteReferences.push(element); + html.text("[" + (noteNumber++) + "]"); html.close(); html.close(); callback(); }, "note": function(element, html, messages, callback) { - var uid = footnoteUid(element.id); + var uid = noteUid(element.id); html.open(htmlPaths.element("li", {id: element.noteType + "-" + uid})); var footnoteHtml = new HtmlGenerator(); diff --git a/lib/documents.js b/lib/documents.js index aea2b39..5ec8aaf 100644 --- a/lib/documents.js +++ b/lib/documents.js @@ -7,7 +7,7 @@ var types = { text: "text", tab: "tab", hyperlink: "hyperlink", - footnoteReference: "footnoteReference", + noteReference: "noteReference", image: "image", note: "note", table: "table", @@ -78,10 +78,11 @@ function Hyperlink(children, options) { }; } -function FootnoteReference(options) { +function NoteReference(options) { return { - type: types.footnoteReference, - footnoteId: options.footnoteId + type: types.noteReference, + noteType: options.noteType, + noteId: options.noteId }; } @@ -91,8 +92,8 @@ function Notes(notes) { }); } -Notes.prototype.findFootnoteById = function(id) { - return this.findNoteByKey(noteKey("footnote", id)); +Notes.prototype.resolve = function(reference) { + return this.findNoteByKey(noteKey(reference.noteType, reference.noteId)); }; Notes.prototype.findNoteByKey = function(key) { @@ -154,7 +155,7 @@ exports.Run = Run; exports.Text = Text; exports.Tab = Tab; exports.Hyperlink = Hyperlink; -exports.FootnoteReference = FootnoteReference; +exports.NoteReference = NoteReference; exports.Notes = Notes; exports.Note = Note; exports.Image = Image; diff --git a/lib/docx/document-xml-reader.js b/lib/docx/document-xml-reader.js index c41a29f..2be726a 100644 --- a/lib/docx/document-xml-reader.js +++ b/lib/docx/document-xml-reader.js @@ -94,6 +94,16 @@ function DocumentXmlReader(options) { } } + function noteReferenceReader(noteType) { + return function(element) { + var noteId = element.attributes["w:id"]; + return new Result(new documents.NoteReference({ + noteType: noteType, + noteId: noteId + })); + }; + } + function readChildElements(element) { return readXmlElements(element.children); } @@ -175,12 +185,8 @@ function DocumentXmlReader(options) { "w:tc": function(element) { return readXmlElements(element.children).map(documents.TableCell); }, - "w:footnoteReference": function(element) { - var footnoteId = element.attributes["w:id"]; - return new Result(new documents.FootnoteReference({ - footnoteId: footnoteId - })); - }, + "w:footnoteReference": noteReferenceReader("footnote"), + "w:endnoteReference": noteReferenceReader("endnote"), "w:br": function(element) { var breakType = element.attributes["w:type"]; if (breakType) { |
