summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2014-07-08 10:20:52 +0100
committerMichael Williamson <mike@zwobble.org>2014-07-08 10:20:52 +0100
commit40fc65d1f2874eb707a4fb4e93c4a66ccd59f850 (patch)
tree74fe9a043438e80843ee95e3dafc8e52adc9b950
parentd54de16a5c570bb6f4aaf3af0c8f0a2fa4987880 (diff)
Extract common code for image conversion
-rw-r--r--lib/document-to-html.js19
-rw-r--r--lib/images.js14
-rw-r--r--lib/main.js17
3 files changed, 28 insertions, 22 deletions
diff --git a/lib/document-to-html.js b/lib/document-to-html.js
index 9eef1f6..7e37255 100644
--- a/lib/document-to-html.js
+++ b/lib/document-to-html.js
@@ -4,6 +4,7 @@ var async = require("async");
var htmlPaths = require("./html-paths");
var HtmlGenerator = require("./html-generator").HtmlGenerator;
var results = require("./results");
+var images = require("./images");
exports.DocumentConverter = DocumentConverter;
@@ -121,17 +122,13 @@ function DocumentConversion(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 defaultConvertImage = images.inline(function(element) {
+ return element.read("base64").then(function(imageBuffer) {
+ return {
+ src: "data:" + element.contentType + ";base64," + imageBuffer
+ };
+ });
+ });
function footnoteUid(id) {
return uniquifier + "-" + id;
diff --git a/lib/images.js b/lib/images.js
new file mode 100644
index 0000000..613a264
--- /dev/null
+++ b/lib/images.js
@@ -0,0 +1,14 @@
+var promises = require("./promises");
+var htmlPaths = require("./html-paths");
+
+exports.inline = function(func) {
+ return function(element, html, messages, callback) {
+ return promises.when(func(element)).then(function(result) {
+ var attributes = {src: result.src};
+ if (element.altText) {
+ attributes.alt = element.altText;
+ }
+ html.selfClosing(htmlPaths.element("img", attributes));
+ }).then(callback, callback);
+ };
+};
diff --git a/lib/main.js b/lib/main.js
index 1cb9632..a9ca14d 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -3,7 +3,7 @@ var path = require("path");
var mammoth = require("./");
var promises = require("./promises");
-var htmlPaths = require("./html-paths");
+var images = require("./images");
function main(argv) {
var docxPath = argv["docx-path"];
@@ -20,23 +20,18 @@ function main(argv) {
var basename = path.basename(docxPath, ".docx");
outputPath = path.join(outputDir, basename + ".html");
var imageIndex = 0;
- options.convertImage = function(element, html, messages, callback) {
+ options.convertImage = images.inline(function(element) {
imageIndex++;
var extension = element.contentType.split("/")[1];
var filename = imageIndex + "." + extension;
- element.read().then(function(imageBuffer) {
+ return element.read().then(function(imageBuffer) {
var imagePath = path.join(outputDir, filename);
return promises.nfcall(fs.writeFile, imagePath, imageBuffer);
}).then(function() {
- var attributes = {src: filename};
- if (element.altText) {
- attributes.alt = element.altText;
- }
- html.selfClosing(htmlPaths.element("img", attributes));
- callback();
- }).fail(callback);
- };
+ return {src: filename};
+ });
+ });
}
return mammoth.convertToHtml({path: docxPath}, options)