summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-04-27 22:38:01 +0100
committerMichael Williamson <mike@zwobble.org>2013-04-27 22:38:01 +0100
commit2642cdd90f25528df0d9501200bc95417e60ec82 (patch)
tree616d495bccaa000c550353412d648d6b56279f89
parentcb18e8aa92108f8576c5d4312e89e6ddbabd88c8 (diff)
Add content-types to image elements
-rw-r--r--lib/document-xml-reader.js12
-rw-r--r--lib/documents.js7
-rw-r--r--lib/docx-reader.js7
-rw-r--r--tests/document-to-html.tests.js14
-rw-r--r--tests/document-xml-reader.tests.js12
5 files changed, 35 insertions, 17 deletions
diff --git a/lib/document-xml-reader.js b/lib/document-xml-reader.js
index 55675e0..ec9682e 100644
--- a/lib/document-xml-reader.js
+++ b/lib/document-xml-reader.js
@@ -9,7 +9,7 @@ var Result = require("./results").Result;
var warning = require("./results").warning;
-function DocumentXmlReader(relationships, docxFile) {
+function DocumentXmlReader(relationships, contentTypes, docxFile) {
function convertXmlToDocument(documentXml) {
var body = documentXml.root.first("w:body");
@@ -127,11 +127,15 @@ function DocumentXmlReader(relationships, docxFile) {
var results = blips.map(function(blip) {
var relationshipId = blip.attributes["r:embed"];
- var imagePath = relationships[relationshipId].target;
- var readImage = docxFile.read.bind(docxFile, path.join("word", imagePath));
+ var imagePath = path.join("word", relationships[relationshipId].target);
+ var readImage = docxFile.read.bind(docxFile, imagePath);
var altText = element.first("wp:docPr").attributes.descr;
- var image = documents.Image(readImage, altText)
+ var image = documents.Image({
+ readImage: readImage,
+ altText: altText,
+ contentType: contentTypes.findContentType(imagePath)
+ });
return new Result(image);
});
diff --git a/lib/documents.js b/lib/documents.js
index 986cd9c..63d5664 100644
--- a/lib/documents.js
+++ b/lib/documents.js
@@ -50,11 +50,12 @@ function ElementWithChildren(type) {
};
}
-function Image(read, altText) {
+function Image(options) {
return {
type: "image",
- read: read,
- altText: altText
+ read: options.readImage,
+ altText: options.altText,
+ contentType: options.contentType
};
}
diff --git a/lib/docx-reader.js b/lib/docx-reader.js
index ff464d6..02841a5 100644
--- a/lib/docx-reader.js
+++ b/lib/docx-reader.js
@@ -29,7 +29,12 @@ function read(options) {
readXmlFile(docxFile, "word/document.xml")
]).spread(function(relationshipsXml, documentXml) {
var relationships = relationshipsXml ? readRelationships(relationshipsXml) : {};
- var reader = new DocumentXmlReader(relationships, docxFile);
+ var contentTypes = {
+ findContentType: function(path) {
+ return "image/png";
+ }
+ };
+ var reader = new DocumentXmlReader(relationships, contentTypes, docxFile);
return reader.convertXmlToDocument(documentXml);
});
});
diff --git a/tests/document-to-html.tests.js b/tests/document-to-html.tests.js
index a01018a..864e3ee 100644
--- a/tests/document-to-html.tests.js
+++ b/tests/document-to-html.tests.js
@@ -176,11 +176,11 @@ describe('DocumentConverter', function() {
test('images are written with data URIs', function() {
var imageBuffer = new Buffer("Not an image at all!");
- var image = new documents.Image(
- function(encoding) {
+ var image = new documents.Image({
+ readImage: function(encoding) {
return q.when(imageBuffer.toString(encoding));
}
- );
+ });
var converter = new DocumentConverter();
return converter.convertToHtml(image).then(function(result) {
assert.equal(result.value, '<img src="data:image/png;base64,' + imageBuffer.toString("base64") + '" />');
@@ -189,12 +189,12 @@ describe('DocumentConverter', function() {
test('images have alt attribute if available', function() {
var imageBuffer = new Buffer("Not an image at all!");
- var image = new documents.Image(
- function() {
+ var image = new documents.Image({
+ readImage: function() {
return q.when(imageBuffer);
},
- "It's a hat"
- );
+ altText: "It's a hat"
+ });
var converter = new DocumentConverter();
return converter.convertToHtml(image)
.then(function(result) {
diff --git a/tests/document-xml-reader.tests.js b/tests/document-xml-reader.tests.js
index 96b307d..75ce8d8 100644
--- a/tests/document-xml-reader.tests.js
+++ b/tests/document-xml-reader.tests.js
@@ -18,6 +18,12 @@ function readXmlElementValue(element) {
return result.value;
}
+var fakeContentTypes = {
+ findContentType: function(path) {
+ return "<content-type: " + path + ">";
+ }
+};
+
describe("readXmlElement: ", function() {
test("paragraph has no style if it has no properties", function() {
var paragraphXml = new XmlElement("w:p", {}, []);
@@ -104,13 +110,13 @@ describe("readXmlElement: ", function() {
relationshipId: "rId5",
description: "It's a hat"
});
-
var imageBuffer = new Buffer("Not an image at all!");
var reader = new DocumentXmlReader(
{
"rId5": {target: "media/hat.png"}
},
+ fakeContentTypes,
createFakeDocxFile({
"word/media/hat.png": imageBuffer
})
@@ -120,9 +126,10 @@ describe("readXmlElement: ", function() {
var element = single(result.value);
assert.equal("image", element.type);
assert.equal(element.altText, "It's a hat");
+ assert.equal(element.contentType, "<content-type: word/media/hat.png>");
return element.read()
.then(function(readValue) {
- assert.equal(readValue, imageBuffer)
+ assert.equal(readValue, imageBuffer);
});
});
@@ -147,6 +154,7 @@ describe("readXmlElement: ", function() {
{
"rId5": {target: "media/hat.png"}
},
+ fakeContentTypes,
createFakeDocxFile({
"word/media/hat.png": imageBuffer
})