diff options
| author | Michael Williamson <mike@zwobble.org> | 2014-03-18 17:11:55 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2014-03-18 17:11:55 +0000 |
| commit | fab0bf4a2bbfd8dd2219563dff702c629bd16e5b (patch) | |
| tree | b51cbb35a71334b81a3d67236a398aacae58e98a /lib/main.js | |
| parent | ccdf6f46ae285469ac5ef75e3590bdd7e2e81ec4 (diff) | |
Add --output-dir argument to CLI
Diffstat (limited to 'lib/main.js')
| -rw-r--r-- | lib/main.js | 32 |
1 files changed, 31 insertions, 1 deletions
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; |
