summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-04-28 12:52:24 +0100
committerMichael Williamson <mike@zwobble.org>2013-04-28 12:52:24 +0100
commit6b9aa0d1e370d2f982ed85882181e735fa4431d2 (patch)
treeabdea0a54ba60311790e5e96f60331a0ecf5e495
parent8149a2ea6867b07a3f8b17e09ee0cd296bcfb329 (diff)
Allow custom image to HTML conversion
-rw-r--r--lib/document-to-html.js24
-rw-r--r--tests/document-to-html.tests.js21
2 files changed, 34 insertions, 11 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js
index 25892d5..47e7e86 100644
--- a/lib/document-to-html.js
+++ b/lib/document-to-html.js
@@ -80,6 +80,18 @@ function DocumentConverter(options) {
}
});
}
+
+ function defaultConvertImage(element, html, messages, callback) {
+ element.read("base64").then(function(imageBuffer) {
+ var src = "data:" + element.contentType + ";base64," + imageBuffer;
+ var attributes = {src: src};
+ if (element.altText) {
+ attributes.alt = element.altText;
+ }
+ html.selfClosing(htmlPaths.element("img", attributes));
+ callback();
+ }).fail(callback);
+ }
var elementConverters = {
"document": function(document, html, messages, callback) {
@@ -102,17 +114,7 @@ function DocumentConverter(options) {
}
});
},
- "image": function(element, html, messages, callback) {
- element.read("base64").then(function(imageBuffer) {
- var src = "data:" + element.contentType + ";base64," + imageBuffer;
- var attributes = {src: src};
- if (element.altText) {
- attributes.alt = element.altText;
- }
- html.selfClosing(htmlPaths.element("img", attributes));
- callback();
- }).fail(callback);
- }
+ "image": options.convertImage || defaultConvertImage
};
return {
convertToHtml: convertToHtml
diff --git a/tests/document-to-html.tests.js b/tests/document-to-html.tests.js
index c7b7bcf..65ce341 100644
--- a/tests/document-to-html.tests.js
+++ b/tests/document-to-html.tests.js
@@ -206,6 +206,27 @@ describe('DocumentConverter', function() {
assert.equal(htmlImageElement.attributes.alt, "It's a hat");
});
});
+
+ test('can add custom handler for images', function() {
+ var imageBuffer = new Buffer("Not an image at all!");
+ var image = new documents.Image({
+ readImage: function(encoding) {
+ return q.when(imageBuffer.toString(encoding));
+ },
+ contentType: "image/png"
+ });
+ var converter = new DocumentConverter({
+ convertImage: function(element, html, messages, callback) {
+ element.read("utf8").then(function(altText) {;
+ html.selfClosing(htmlPaths.element("img", {alt: altText}));
+ callback();
+ });
+ }
+ });
+ return converter.convertToHtml(image).then(function(result) {
+ assert.equal(result.value, '<img alt="Not an image at all!" />');
+ });
+ });
});
function paragraphOfText(text, styleName) {