diff options
| author | Michael Williamson <mike@zwobble.org> | 2013-04-01 21:59:37 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2013-04-01 21:59:37 +0100 |
| commit | a70e8e05948c593fdc253b6d0a256d7aa656d988 (patch) | |
| tree | 369f835579f914b236d5770b93fad625a72fbeb0 /lib/docx-reader.js | |
| parent | 7bfcfcfe2012f9be26e93b06f7c1a09e1ec52ae7 (diff) | |
Add reading of inline pictures
Diffstat (limited to 'lib/docx-reader.js')
| -rw-r--r-- | lib/docx-reader.js | 30 |
1 files changed, 25 insertions, 5 deletions
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 { |
