diff options
| author | Michael Williamson <mike@zwobble.org> | 2014-06-10 16:23:15 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2014-06-10 16:23:15 +0100 |
| commit | 8aec6e713d08a532194b50591fa338ecb9a251f9 (patch) | |
| tree | 00004b0324527dac50add38cdf6ac39cec604227 /lib | |
| parent | ee2fc5da880df5f9ea8cdaebc506d130c3009ce3 (diff) | |
Basic support for footnotes
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/docx/document-xml-reader.js | 21 | ||||
| -rw-r--r-- | lib/docx/docx-reader.js | 13 | ||||
| -rw-r--r-- | lib/docx/footnotes-reader.js | 12 | ||||
| -rw-r--r-- | lib/results.js | 5 |
4 files changed, 37 insertions, 14 deletions
diff --git a/lib/docx/document-xml-reader.js b/lib/docx/document-xml-reader.js index 348bccf..6bf20dc 100644 --- a/lib/docx/document-xml-reader.js +++ b/lib/docx/document-xml-reader.js @@ -14,19 +14,34 @@ function DocumentXmlReader(options) { var docxFile = options.docxFile; var numbering = options.numbering; var styles = options.styles; - var footnotes = options.footnotes; + var rawFootnotes = options.footnotes || []; function convertXmlToDocument(documentXml) { var body = documentXml.root.first("w:body"); var result = readXmlElements(body.children) - .map(function(children) { - return new documents.Document(children, footnotes); + .flatMap(function(children) { + return readFootnotes().map(function(footnotes) { + return new documents.Document(children, {footnotes: footnotes}); + }); }); result.document = result.value; return result; } + + function readFootnotes() { + return Result.combine(rawFootnotes.map(function(rawFootnote) { + return readXmlElements(rawFootnote.body).map(function(body) { + return new documents.Footnote({ + id: rawFootnote.id, + body: body + }); + }); + })).map(function(footnotes) { + return new documents.Footnotes(_.indexBy(footnotes, "id")); + }); + } function readXmlElements(elements) { var results = elements.map(readXmlElement); diff --git a/lib/docx/docx-reader.js b/lib/docx/docx-reader.js index ae09adc..3afafee 100644 --- a/lib/docx/docx-reader.js +++ b/lib/docx/docx-reader.js @@ -10,6 +10,7 @@ var relationshipsReader = require("./relationships-reader"); var contentTypesReader = require("./content-types-reader"); var numberingXml = require("./numbering-xml"); var stylesReader = require("./styles-reader"); +var footnotesReader = require("./footnotes-reader"); function read(options) { @@ -20,14 +21,16 @@ function read(options) { readContentTypesFromZipFile(docxFile), readNumberingFromZipFile(docxFile), readStylesFromZipFile(docxFile), + readFootnotesFromZipFile(docxFile), readXmlFromZipFile(docxFile, "word/document.xml"), - ]).spread(function(relationships, contentTypes, numbering, styles, documentXml) { + ]).spread(function(relationships, contentTypes, numbering, styles, footnotes, documentXml) { var reader = new DocumentXmlReader({ relationships: relationships, contentTypes: contentTypes, docxFile: docxFile, numbering: numbering, - styles: styles + styles: styles, + footnotes: footnotes }); return reader.convertXmlToDocument(documentXml); }); @@ -66,3 +69,9 @@ var readStylesFromZipFile = xmlFileReader({ readElement: stylesReader.readStylesXml, defaultValue: stylesReader.defaultStyles }); + +var readFootnotesFromZipFile = xmlFileReader({ + filename: "word/footnotes.xml", + readElement: footnotesReader.readFootnotesXml, + defaultValue: undefined +}); diff --git a/lib/docx/footnotes-reader.js b/lib/docx/footnotes-reader.js index c7ec98c..a653b8a 100644 --- a/lib/docx/footnotes-reader.js +++ b/lib/docx/footnotes-reader.js @@ -3,17 +3,11 @@ exports.readFootnotesXml = readFootnotesXml; var Footnotes = require("../documents").Footnotes; function readFootnotesXml(xml) { - var footnotes = {}; - - xml.root.getElementsByTagName("w:footnote").forEach(function(footnoteElement) { - var id = footnoteElement.attributes["w:id"]; - footnotes[id] = readFootnoteElement(footnoteElement); - }); - - return new Footnotes(footnotes); + return xml.root.getElementsByTagName("w:footnote").map(readFootnoteElement); } function readFootnoteElement(footnoteElement) { + var id = footnoteElement.attributes["w:id"]; var body = footnoteElement.children; - return {body: body}; + return {id: id, body: body}; } diff --git a/lib/results.js b/lib/results.js index fe9f632..bd66430 100644 --- a/lib/results.js +++ b/lib/results.js @@ -14,6 +14,11 @@ Result.prototype.map = function(func) { return new Result(func(this.value), this.messages); }; +Result.prototype.flatMap = function(func) { + var funcResult = func(this.value); + return new Result(funcResult.value, combineMessages([this, funcResult])); +}; + Result.prototype.flatMapThen = function(func) { var that = this; return func(this.value).then(function(otherResult) { |
