aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-04-01 21:59:37 +0100
committerMichael Williamson <mike@zwobble.org>2013-04-01 21:59:37 +0100
commita70e8e05948c593fdc253b6d0a256d7aa656d988 (patch)
tree369f835579f914b236d5770b93fad625a72fbeb0
parent7bfcfcfe2012f9be26e93b06f7c1a09e1ec52ae7 (diff)
Add reading of inline pictures
-rw-r--r--lib/documents.js8
-rw-r--r--lib/docx-reader.js30
-rw-r--r--tests/docx-reader.tests.js33
3 files changed, 66 insertions, 5 deletions
diff --git a/lib/documents.js b/lib/documents.js
index 8fc3df7..b28281f 100644
--- a/lib/documents.js
+++ b/lib/documents.js
@@ -33,8 +33,16 @@ function ElementWithChildren(type) {
};
}
+function Image(read) {
+ return {
+ type: "image",
+ read: read
+ };
+}
+
exports.Document = Document;
exports.Paragraph = Paragraph;
exports.Run = Run;
exports.Text = Text;
exports.Hyperlink = Hyperlink;
+exports.Image = Image;
diff --git a/lib/docx-reader.js b/lib/docx-reader.js
index 865f78c..d15ec3e 100644
--- a/lib/docx-reader.js
+++ b/lib/docx-reader.js
@@ -13,10 +13,14 @@ function read(docxFile) {
if (_.isString(docxFile)) {
var zipFile = new ZipFile(docxFile);
docxFile = {
- read: function(path) {
- return q.ninvoke(zipFile, "readFile", path, "utf-8")
+ read: function(path, encoding) {
+ return q.ninvoke(zipFile, "readFile", path)
.then(function(value) {
- return value.toString();
+ if (encoding) {
+ return value;
+ } else {
+ return value.toString(encoding);
+ }
});
},
exists: function(path) {
@@ -30,7 +34,7 @@ function read(docxFile) {
readXmlFile(docxFile, "word/document.xml")
]).spread(function(relationshipsXml, documentXml) {
var relationships = relationshipsXml ? readRelationships(relationshipsXml) : {};
- var reader = new DocumentReader(relationships);
+ var reader = new DocumentReader(relationships, docxFile);
return reader.convertXmlToDocument(documentXml);
});
}
@@ -56,7 +60,7 @@ function readRelationships(relationshipsXml) {
return relationships;
}
-function DocumentReader(relationships) {
+function DocumentReader(relationships, docxFile) {
function convertXmlToDocument(documentXml) {
var body = documentXml.root.first("w:body");
@@ -149,6 +153,22 @@ function DocumentReader(relationships) {
.map(function(children) {
return new documents.Hyperlink(children, {href: href});
});
+ },
+
+ "w:drawing": function(element) {
+ return readXmlElement(element.first("wp:inline"));
+ },
+ "wp:inline": function(element) {
+ var relationshipId = element
+ .first("a:graphic")
+ .first("a:graphicData")
+ .first("pic:pic")
+ .first("pic:blipFill")
+ .first("a:blip")
+ .attributes["r:embed"];
+ var imagePath = relationships[relationshipId].target;
+ var image = documents.Image(docxFile.read.bind(docxFile, imagePath));
+ return new Result(image);
}
};
return {
diff --git a/tests/docx-reader.tests.js b/tests/docx-reader.tests.js
index 68efb0a..9f431b7 100644
--- a/tests/docx-reader.tests.js
+++ b/tests/docx-reader.tests.js
@@ -124,6 +124,39 @@ describe("readElement", function() {
assert.deepEqual(result.messages, []);
assert.equal(null, result.value);
});
+
+ test("can read inline pictures", function() {
+ var drawing = new XmlElement("w:drawing", {}, [
+ new XmlElement("wp:inline", {}, [
+ new XmlElement("a:graphic", {}, [
+ new XmlElement("a:graphicData", {}, [
+ new XmlElement("pic:pic", {}, [
+ new XmlElement("pic:blipFill", {}, [
+ new XmlElement("a:blip", {"r:embed": "rId5"})
+ ])
+ ])
+ ])
+ ])
+ ])
+ ]);
+
+ var imageBuffer = new Buffer("Not an image at all!");
+ var reader = new docxReader.DocumentReader(
+ {
+ "rId5": {target: "media/hat.png"}
+ },
+ createFakeDocxFile({
+ "media/hat.png": imageBuffer
+ })
+ );
+ var result = reader.readXmlElement(drawing);
+ assert.deepEqual(result.messages, []);
+ assert.deepEqual("image", result.value.type);
+ return result.value.read()
+ .then(function(readValue) {
+ assert.equal(readValue, imageBuffer)
+ });
+ });
});
function createRunPropertiesXml(children) {