summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-01-19 23:05:45 +0000
committerMichael Williamson <mike@zwobble.org>2015-01-19 23:05:45 +0000
commit965bd9513da033b9804af8ec29c7fa609a84512c (patch)
tree28027622b24a00bb44fd4a1e9c91462493d78989
parent58cae6030e51481ffb462e52ab6aeb479e0a705d (diff)
Add support for endnotes
-rw-r--r--lib/document-to-html.js26
-rw-r--r--lib/documents.js15
-rw-r--r--lib/docx/document-xml-reader.js18
-rw-r--r--tests/document-to-html.tests.js10
-rw-r--r--tests/docx/document-xml-reader.tests.js2
-rw-r--r--tests/mammoth.tests.js9
-rw-r--r--tests/test-data/endnotes.docxbin4253 -> 4431 bytes
7 files changed, 44 insertions, 36 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) {
diff --git a/tests/document-to-html.tests.js b/tests/document-to-html.tests.js
index a5cf294..63b445d 100644
--- a/tests/document-to-html.tests.js
+++ b/tests/document-to-html.tests.js
@@ -285,8 +285,9 @@ describe('DocumentConverter', function() {
});
test('footnote reference is converted to superscript intra-page link', function() {
- var footnoteReference = new documents.FootnoteReference({
- footnoteId: "4"
+ var footnoteReference = new documents.NoteReference({
+ noteType: "footnote",
+ noteId: "4"
});
var converter = new DocumentConverter({
generateUniquifier: function() { return 42; }
@@ -297,8 +298,9 @@ describe('DocumentConverter', function() {
});
test('footnotes are included after the main body', function() {
- var footnoteReference = new documents.FootnoteReference({
- footnoteId: "4"
+ var footnoteReference = new documents.NoteReference({
+ noteType: "footnote",
+ noteId: "4"
});
var document = new documents.Document(
[new documents.Paragraph([
diff --git a/tests/docx/document-xml-reader.tests.js b/tests/docx/document-xml-reader.tests.js
index 778bf06..86c0fcb 100644
--- a/tests/docx/document-xml-reader.tests.js
+++ b/tests/docx/document-xml-reader.tests.js
@@ -361,7 +361,7 @@ describe("convertXmlToDocument: ", function() {
var documentXml = new XmlElement("w:document", {}, [bodyXml]);
var document = convertXmlToDocumentValue({root: documentXml}, {footnotes: footnotes});
- var footnote = document.notes.findFootnoteById("4");
+ var footnote = document.notes.resolve({noteType: "footnote", noteId: "4"});
assert.deepEqual(footnote.id, "4");
assert.deepEqual(footnote.body[0].type, "paragraph");
});
diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js
index 08649f4..08eafa4 100644
--- a/tests/mammoth.tests.js
+++ b/tests/mammoth.tests.js
@@ -165,17 +165,16 @@ describe('mammoth', function() {
});
test('endnotes are appended to text', function() {
- return;
var docxPath = path.join(__dirname, "test-data/endnotes.docx");
var options = {
generateUniquifier: function() { return 42; }
};
return mammoth.convertToHtml({path: docxPath}, options).then(function(result) {
var expectedOutput = '<p>Ouch' +
- '<sup><a href="#endnote-42-1" id="endnote-ref-42-1">[1]</a></sup>.' +
- '<sup><a href="#endnote-42-2" id="endnote-ref-42-2">[2]</a></sup></p>' +
- '<ol><li id="endnote-42-1"><p> A tachyon walks into a bar. <a href="#endnote-ref-42-1">↑</a></p></li>' +
- '<li id="endnote-42-2"><p> Fin. <a href="#endnote-ref-42-2">↑</a></p></li></ol>';
+ '<sup><a href="#endnote-42-2" id="endnote-ref-42-2">[1]</a></sup>.' +
+ '<sup><a href="#endnote-42-3" id="endnote-ref-42-3">[2]</a></sup></p>' +
+ '<ol><li id="endnote-42-2"><p> A tachyon walks into a bar. <a href="#endnote-ref-42-2">↑</a></p></li>' +
+ '<li id="endnote-42-3"><p> Fin. <a href="#endnote-ref-42-3">↑</a></p></li></ol>';
assert.equal(result.value, expectedOutput);
// TODO: get rid of warnings
//~ assert.deepEqual(result.messages, []);
diff --git a/tests/test-data/endnotes.docx b/tests/test-data/endnotes.docx
index 5ec7faf..1ae4f93 100644
--- a/tests/test-data/endnotes.docx
+++ b/tests/test-data/endnotes.docx
Binary files differ