diff options
| author | Michael Williamson <mike@zwobble.org> | 2018-01-26 18:12:34 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2018-01-26 18:12:34 +0000 |
| commit | 818cedc5fd9e83a633df5fae5c341996abcd9e80 (patch) | |
| tree | a28388f679d106917831aeea7c84cb0cb047d737 /lib | |
| parent | 41ca73ffe6330bcfdd09cea2f3b27bc23de8f818 (diff) | |
Extract function for finding part paths
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/docx/docx-reader.js | 59 |
1 files changed, 40 insertions, 19 deletions
diff --git a/lib/docx/docx-reader.js b/lib/docx/docx-reader.js index dd1b857..ac021b9 100644 --- a/lib/docx/docx-reader.js +++ b/lib/docx/docx-reader.js @@ -20,30 +20,34 @@ var Files = require("./files").Files; function read(docxFile, input) { input = input || {}; + return promises.props({ contentTypes: readContentTypesFromZipFile(docxFile), - numbering: readNumberingFromZipFile(docxFile), - styles: readStylesFromZipFile(docxFile), - packageRelationships: readFileRelationships(docxFile), + partPaths: findPartPaths(docxFile), docxFile: docxFile, files: new Files(input.path ? path.dirname(input.path) : null) }).also(function(result) { return { - footnotes: readXmlFileWithBody("word/footnotes.xml", result, function(bodyReader, xml) { + numbering: readNumberingFromZipFile(docxFile, result.partPaths.numbering), + styles: readStylesFromZipFile(docxFile, result.partPaths.styles) + }; + }).also(function(result) { + return { + footnotes: readXmlFileWithBody(result.partPaths.footnotes, result, function(bodyReader, xml) { if (xml) { return notesReader.createFootnotesReader(bodyReader)(xml); } else { return new Result([]); } }), - endnotes: readXmlFileWithBody("word/endnotes.xml", result, function(bodyReader, xml) { + endnotes: readXmlFileWithBody(result.partPaths.endnotes, result, function(bodyReader, xml) { if (xml) { return notesReader.createEndnotesReader(bodyReader)(xml); } else { return new Result([]); } }), - comments: readXmlFileWithBody("word/comments.xml", result, function(bodyReader, xml) { + comments: readXmlFileWithBody(result.partPaths.comments, result, function(bodyReader, xml) { if (xml) { return commentsReader.createCommentsReader(bodyReader)(xml); } else { @@ -60,8 +64,7 @@ function read(docxFile, input) { }) }; }).then(function(result) { - var documentFilename = findDocumentFilename(docxFile, result.packageRelationships); - return readXmlFileWithBody(documentFilename, result, function(bodyReader, xml) { + return readXmlFileWithBody(result.partPaths.mainDocument, result, function(bodyReader, xml) { return result.notes.flatMap(function(notes) { return result.comments.flatMap(function(comments) { var reader = new DocumentXmlReader({ @@ -76,6 +79,20 @@ function read(docxFile, input) { }); } +function findPartPaths(docxFile) { + return readPackageRelationships(docxFile).then(function(packageRelationships) { + var documentFilename = findDocumentFilename(docxFile, packageRelationships); + return { + mainDocument: documentFilename, + comments: "word/comments.xml", + endnotes: "word/endnotes.xml", + footnotes: "word/footnotes.xml", + numbering: "word/numbering.xml", + styles: "word/styles.xml" + }; + }); +} + function findDocumentFilename(docxFile, relationships) { var officeDocumentType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"; var targets = relationships.findTargetsByType(officeDocumentType).concat(["word/document.xml"]); @@ -144,19 +161,23 @@ var readContentTypesFromZipFile = xmlFileReader({ defaultValue: contentTypesReader.defaultContentTypes }); -var readNumberingFromZipFile = xmlFileReader({ - filename: "word/numbering.xml", - readElement: numberingXml.readNumberingXml, - defaultValue: numberingXml.defaultNumbering -}); +function readNumberingFromZipFile(zipFile, path) { + return xmlFileReader({ + filename: path, + readElement: numberingXml.readNumberingXml, + defaultValue: numberingXml.defaultNumbering + })(zipFile); +} -var readStylesFromZipFile = xmlFileReader({ - filename: "word/styles.xml", - readElement: stylesReader.readStylesXml, - defaultValue: stylesReader.defaultStyles -}); +function readStylesFromZipFile(zipFile, path) { + return xmlFileReader({ + filename: path, + readElement: stylesReader.readStylesXml, + defaultValue: stylesReader.defaultStyles + })(zipFile); +} -var readFileRelationships = xmlFileReader({ +var readPackageRelationships = xmlFileReader({ filename: "_rels/.rels", readElement: relationshipsReader.readRelationships, defaultValue: relationshipsReader.defaultValue |
