diff options
| -rwxr-xr-x | bin/mammoth | 6 | ||||
| -rw-r--r-- | lib/main.js | 32 | ||||
| -rw-r--r-- | lib/unzip.js | 2 | ||||
| -rw-r--r-- | tests/main.tests.js | 20 |
4 files changed, 55 insertions, 5 deletions
diff --git a/bin/mammoth b/bin/mammoth index 16515f1..b802dfb 100755 --- a/bin/mammoth +++ b/bin/mammoth @@ -10,7 +10,11 @@ var args = require("nomnom") .option("output-path", { position: 1, type: "string", - help: "Output path for the generated HTML. Output is written to stdout if not set." + help: "Output path for the generated HTML. Images will be stored inline in the HTML. Output is written to stdout if not set." + }) + .option("output-dir", { + type: "string", + help: "Output directory for generated HTML and images. Images will be stored in separate files. Takes precedence over output-path." }) .parse(); var main = require("../lib/main"); diff --git a/lib/main.js b/lib/main.js index 3493eba..1b56558 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,11 +1,41 @@ var fs = require("fs"); +var path = require("path"); var mammoth = require("./"); +var promises = require("./promises"); +var htmlPaths = require("./html-paths"); function main(argv) { var docxPath = argv["docx-path"]; var outputPath = argv["output-path"]; - mammoth.convertToHtml({path: docxPath}) + var outputDir = argv["output-dir"]; + + var options = {}; + + if (outputDir) { + var basename = path.basename(docxPath, ".docx"); + outputPath = path.join(outputDir, basename + ".html"); + var imageIndex = 0; + options.convertImage = function(element, html, messages, callback) { + imageIndex++; + var extension = element.contentType.split("/")[1]; + var filename = imageIndex + "." + extension; + + 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); + }; + } + + mammoth.convertToHtml({path: docxPath}, options) .then(function(result) { var outputStream = outputPath ? fs.createWriteStream(outputPath) : process.stdout; diff --git a/lib/unzip.js b/lib/unzip.js index 855d2ca..ea6f710 100644 --- a/lib/unzip.js +++ b/lib/unzip.js @@ -64,7 +64,7 @@ function unzipStream(stream) { if (encoding) { return buffer.toString(encoding); } else { - return buffer; + return buffer.toBuffer(); } }); }, diff --git a/tests/main.tests.js b/tests/main.tests.js index 3b56b0b..5e8f436 100644 --- a/tests/main.tests.js +++ b/tests/main.tests.js @@ -23,19 +23,35 @@ describe("CLI", function() { return runMammoth(testPath("single-paragraph.docx"), outputPath).then(function(result) { assert.equal(result.stderrOutput, "") assert.equal(result.output, "") - assert.equal(fs.readFileSync(outputPath), "<p>Walking on imported air</p>"); + assert.equal(fs.readFileSync(outputPath, "utf8"), "<p>Walking on imported air</p>"); }); }); }); + var imageBase64 = "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII="; + test("inline images are included in output if writing to single file", function() { return runMammoth(testPath("tiny-picture.docx")).then(function(result) { assert.equal(result.stderrOutput, "") - assert.equal(result.output, '<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>') + assert.equal(result.output, '<p><img src="data:image/png;base64,' + imageBase64 + '" /></p>') + }); + }); + + test("images are written to separate files if output dir is set", function() { + return createTempDir().then(function(tempDir) { + var outputPath = path.join(tempDir, "tiny-picture.html"); + var imagePath = path.join(tempDir, "1.png"); + return runMammoth(testPath("tiny-picture.docx"), "--output-dir", tempDir).then(function(result) { + assert.equal(result.stderrOutput, "") + assert.equal(result.output, "") + assert.equal(fs.readFileSync(outputPath, "utf8"), '<p><img src="1.png" /></p>'); + assert.equal(fs.readFileSync(imagePath, "base64"), imageBase64); + }); }); }); }); + function runMammoth() { var args = Array.prototype.slice.call(arguments, 0); var deferred = promises.defer(); |
