aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-07-03 22:31:29 +0100
committerMichael Williamson <mike@zwobble.org>2015-07-03 22:31:29 +0100
commit27f3d32cabc5fae5b163ddf5a5449d93ebd959d5 (patch)
tree373e830e2c15ad5252000c06aba70e4b144127d4
parentcd3b84e15ea1a47ff8ebcca18d68ef0cdccf20b8 (diff)
Read note bodies immediately using relationships
-rw-r--r--lib/docx/document-xml-reader.js22
-rw-r--r--lib/docx/docx-reader.js89
-rw-r--r--lib/docx/notes-reader.js19
-rw-r--r--tests/docx/document-xml-reader.tests.js34
-rw-r--r--tests/docx/notes-reader.tests.js17
5 files changed, 78 insertions, 103 deletions
diff --git a/lib/docx/document-xml-reader.js b/lib/docx/document-xml-reader.js
index fdf451f..c241c70 100644
--- a/lib/docx/document-xml-reader.js
+++ b/lib/docx/document-xml-reader.js
@@ -6,35 +6,17 @@ var Result = require("../results").Result;
function DocumentXmlReader(options) {
var bodyReader = options.bodyReader;
- var rawNotes = (options.footnotes || []).concat(options.endnotes || []);
-
function convertXmlToDocument(documentXml) {
var body = documentXml.root.first("w:body");
var result = bodyReader.readXmlElements(body.children)
- .flatMap(function(children) {
- return readNotes().map(function(notes) {
- return new documents.Document(children, {notes: notes});
- });
+ .map(function(children) {
+ return new documents.Document(children, {notes: options.notes});
});
return new Result(result.value, result.messages);
}
- function readNotes() {
- return Result.combine(rawNotes.map(function(rawNote) {
- return bodyReader.readXmlElements(rawNote.body).map(function(body) {
- return new documents.Note({
- noteType: rawNote.noteType,
- noteId: rawNote.id,
- body: body
- });
- });
- })).map(function(notes) {
- return new documents.Notes(notes);
- });
- }
-
return {
convertXmlToDocument: convertXmlToDocument
};
diff --git a/lib/docx/docx-reader.js b/lib/docx/docx-reader.js
index 80842d7..a976ef3 100644
--- a/lib/docx/docx-reader.js
+++ b/lib/docx/docx-reader.js
@@ -2,6 +2,8 @@ exports.read = read;
var promises = require("../promises");
var unzip = require("../unzip");
+var documents = require("../documents");
+var Result = require("../results").Result;
var readXmlFromZipFile = require("./office-xml-reader").readXmlFromZipFile;
var BodyReader = require("./body-reader").BodyReader;
@@ -18,32 +20,45 @@ function read(options) {
docxFile: unzip.openZip(options)
}).also(function(result) {
return {
- relationships: readRelationshipsFromZipFile(result.docxFile),
contentTypes: readContentTypesFromZipFile(result.docxFile),
numbering: readNumberingFromZipFile(result.docxFile),
styles: readStylesFromZipFile(result.docxFile)
};
}).also(function(result) {
- var bodyReader = new BodyReader({
- relationships: result.relationships,
- contentTypes: result.contentTypes,
- docxFile: result.docxFile,
- numbering: result.numbering,
- styles: result.styles
- });
return {
- documentBodyReader: bodyReader,
- footnotes: readFootnotesFromZipFile(result.docxFile),
- endnotes: readEndnotesFromZipFile(result.docxFile),
- documentXml: readXmlFromZipFile(result.docxFile, "word/document.xml")
+ footnotes: readXmlFileWithBody("footnotes", result, function(bodyReader, xml) {
+ if (xml) {
+ return notesReader.createFootnotesReader(bodyReader)(xml);
+ } else {
+ return new Result([]);
+ }
+ }),
+ endnotes: readXmlFileWithBody("endnotes", result, function(bodyReader, xml) {
+ if (xml) {
+ return notesReader.createEndnotesReader(bodyReader)(xml);
+ } else {
+ return new Result([]);
+ }
+ })
+ };
+ }).also(function(result) {
+ return {
+ notes: result.footnotes.flatMap(function(footnotes) {
+ return result.endnotes.map(function(endnotes) {
+ return new documents.Notes(footnotes.concat(endnotes));
+ });
+ })
};
}).then(function(result) {
- var reader = new DocumentXmlReader({
- bodyReader: result.documentBodyReader,
- footnotes: result.footnotes,
- endnotes: result.endnotes
+ return readXmlFileWithBody("document", result, function(bodyReader, xml) {
+ return result.notes.flatMap(function(notes) {
+ var reader = new DocumentXmlReader({
+ bodyReader: bodyReader,
+ notes: notes,
+ });
+ return reader.convertXmlToDocument(xml);
+ });
});
- return reader.convertXmlToDocument(result.documentXml);
});
}
@@ -56,18 +71,34 @@ function xmlFileReader(options) {
};
}
+function readXmlFileWithBody(name, options, func) {
+ var readRelationshipsFromZipFile = xmlFileReader({
+ filename: "word/_rels/" + name + ".xml.rels",
+ readElement: relationshipsReader.readRelationships,
+ defaultValue: {}
+ });
+
+ return readRelationshipsFromZipFile(options.docxFile).then(function(relationships) {
+ var bodyReader = new BodyReader({
+ relationships: relationships,
+ contentTypes: options.contentTypes,
+ docxFile: options.docxFile,
+ numbering: options.numbering,
+ styles: options.styles
+ });
+ return readXmlFromZipFile(options.docxFile, "word/" + name + ".xml")
+ .then(function(xml) {
+ return func(bodyReader, xml);
+ });
+ });
+}
+
var readContentTypesFromZipFile = xmlFileReader({
filename: "[Content_Types].xml",
readElement: contentTypesReader.readContentTypesFromXml,
defaultValue: contentTypesReader.defaultContentTypes
});
-var readRelationshipsFromZipFile = xmlFileReader({
- filename: "word/_rels/document.xml.rels",
- readElement: relationshipsReader.readRelationships,
- defaultValue: {}
-});
-
var readNumberingFromZipFile = xmlFileReader({
filename: "word/numbering.xml",
readElement: numberingXml.readNumberingXml,
@@ -79,15 +110,3 @@ var readStylesFromZipFile = xmlFileReader({
readElement: stylesReader.readStylesXml,
defaultValue: stylesReader.defaultStyles
});
-
-var readFootnotesFromZipFile = xmlFileReader({
- filename: "word/footnotes.xml",
- 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 98cbba9..a74f79b 100644
--- a/lib/docx/notes-reader.js
+++ b/lib/docx/notes-reader.js
@@ -1,11 +1,14 @@
-exports.readFootnotesXml = createReader("footnote");
-exports.readEndnotesXml = createReader("endnote");
+var documents = require("../documents");
+var Result = require("../results").Result;
-function createReader(noteType) {
+exports.createFootnotesReader = createReader.bind(this, "footnote");
+exports.createEndnotesReader = createReader.bind(this, "endnote");
+
+function createReader(noteType, bodyReader) {
function readNotesXml(xml) {
- return xml.root.getElementsByTagName("w:" + noteType)
+ return Result.combine(xml.root.getElementsByTagName("w:" + noteType)
.filter(isFootnoteElement)
- .map(readFootnoteElement);
+ .map(readFootnoteElement));
}
function isFootnoteElement(element) {
@@ -15,8 +18,10 @@ function createReader(noteType) {
function readFootnoteElement(footnoteElement) {
var id = footnoteElement.attributes["w:id"];
- var body = footnoteElement.children;
- return {noteType: noteType, id: id, body: body};
+ return bodyReader.readXmlElements(footnoteElement.children)
+ .map(function(body) {
+ return documents.Note({noteType: noteType, noteId: id, body: body});
+ });
}
return readNotesXml;
diff --git a/tests/docx/document-xml-reader.tests.js b/tests/docx/document-xml-reader.tests.js
deleted file mode 100644
index 81283d1..0000000
--- a/tests/docx/document-xml-reader.tests.js
+++ /dev/null
@@ -1,34 +0,0 @@
-var assert = require("assert");
-
-var BodyReader = require("../../lib/docx/body-reader").BodyReader;
-var DocumentXmlReader = require("../../lib/docx/document-xml-reader").DocumentXmlReader;
-var XmlElement = require("../../lib/xmlreader").Element;
-
-var testing = require("../testing");
-var test = testing.test;
-
-function convertXmlToDocumentValue(element, options) {
- var reader = new DocumentXmlReader(options);
- var result = reader.convertXmlToDocument(element);
- assert.deepEqual(result.messages, []);
- return result.value;
-}
-
-describe("convertXmlToDocument: ", function() {
-
- test("notes of document are read", function() {
- var paragraphXml = new XmlElement("w:p", {}, []);
- 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,
- bodyReader: new BodyReader({})
- });
- var footnote = document.notes.resolve({noteType: "footnote", noteId: "4"});
- assert.deepEqual(footnote.noteId, "4");
- assert.deepEqual(footnote.body[0].type, "paragraph");
- });
-});
diff --git a/tests/docx/notes-reader.tests.js b/tests/docx/notes-reader.tests.js
index d3226f8..0cfb626 100644
--- a/tests/docx/notes-reader.tests.js
+++ b/tests/docx/notes-reader.tests.js
@@ -1,21 +1,24 @@
var assert = require("assert");
-var readFootnotesXml = require("../../lib/docx/notes-reader").readFootnotesXml;
+var createFootnotesReader = require("../../lib/docx/notes-reader").createFootnotesReader;
+var BodyReader = require("../../lib/docx/body-reader").BodyReader;
+var documents = require("../../lib/documents");
var XmlElement = require("../../lib/xmlreader").Element;
var test = require("../testing").test;
describe('readFootnotesXml', function() {
test('ID and body of footnote are read', function() {
+ var bodyReader = new BodyReader({});
var footnoteBody = [new XmlElement("w:p", {}, [])];
- var footnotes = readFootnotesXml({
+ var footnotes = createFootnotesReader(bodyReader)({
root: new XmlElement("w:footnotes", {}, [
new XmlElement("w:footnote", {"w:id": "1"}, footnoteBody)
])
});
- assert.equal(footnotes.length, 1);
- assert.strictEqual(footnotes[0].body, footnoteBody);
- assert.deepEqual(footnotes[0].id, "1");
+ assert.equal(footnotes.value.length, 1);
+ assert.deepEqual(footnotes.value[0].body, [new documents.Paragraph([])]);
+ assert.deepEqual(footnotes.value[0].noteId, "1");
});
});
@@ -25,12 +28,12 @@ footnoteTypeIsIgnored('separator');
function footnoteTypeIsIgnored(type) {
describe('footnotes of type ' + type + ' are ignored', function() {
test('ID and body of footnote are read', function() {
- var footnotes = readFootnotesXml({
+ var footnotes = createFootnotesReader()({
root: new XmlElement("w:footnotes", {}, [
new XmlElement("w:footnote", {"w:id": "1", "w:type": type}, [])
])
});
- assert.equal(footnotes.length, 0);
+ assert.equal(footnotes.value.length, 0);
});
});
}