aboutsummaryrefslogtreecommitdiff
path: root/lib
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 /lib
parentcd3b84e15ea1a47ff8ebcca18d68ef0cdccf20b8 (diff)
Read note bodies immediately using relationships
Diffstat (limited to 'lib')
-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
3 files changed, 68 insertions, 62 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;