aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2014-03-18 18:17:37 +0000
committerMichael Williamson <mike@zwobble.org>2014-03-18 18:17:37 +0000
commit60bb80f6acfa026b53ec618644a23d6adfa80621 (patch)
treea4abc5c55eae8a87df7a1abf1af64277a46b8bb2
parentb9e608b6d7182876a2b81ee1e5847b4e242cbf29 (diff)
Add --style-map argument to CLI
-rwxr-xr-xbin/mammoth4
-rw-r--r--lib/main.js72
-rw-r--r--tests/main.tests.js11
3 files changed, 57 insertions, 30 deletions
diff --git a/bin/mammoth b/bin/mammoth
index b802dfb..c60e737 100755
--- a/bin/mammoth
+++ b/bin/mammoth
@@ -16,6 +16,10 @@ var args = require("nomnom")
type: "string",
help: "Output directory for generated HTML and images. Images will be stored in separate files. Takes precedence over output-path."
})
+ .option("--style-map", {
+ type: "string",
+ help: "File containg a style map."
+ })
.parse();
var main = require("../lib/main");
diff --git a/lib/main.js b/lib/main.js
index 1b56558..26d146a 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -9,39 +9,51 @@ function main(argv) {
var docxPath = argv["docx-path"];
var outputPath = argv["output-path"];
var outputDir = argv["output-dir"];
+ var styleMapPath = argv["style-map"];
- 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);
+ readStyleMap(styleMapPath).then(function(styleMap) {
+ var options = {
+ styleMap: styleMap
};
+
+ 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);
+ };
+ }
+
+ return mammoth.convertToHtml({path: docxPath}, options)
+ .then(function(result) {
+ var outputStream = outputPath ? fs.createWriteStream(outputPath) : process.stdout;
+
+ outputStream.write(result.value);
+ })
+ }).done();
+}
+
+function readStyleMap(styleMapPath) {
+ if (styleMapPath) {
+ return promises.nfcall(fs.readFile, styleMapPath, "utf8");
+ } else {
+ return promises.resolve(null);
}
-
- mammoth.convertToHtml({path: docxPath}, options)
- .then(function(result) {
- var outputStream = outputPath ? fs.createWriteStream(outputPath) : process.stdout;
-
- outputStream.write(result.value);
- })
- .done();
}
module.exports = main;
diff --git a/tests/main.tests.js b/tests/main.tests.js
index 5e8f436..01004a8 100644
--- a/tests/main.tests.js
+++ b/tests/main.tests.js
@@ -49,6 +49,17 @@ describe("CLI", function() {
});
});
});
+
+ test("style map is used if set", function() {
+ return createTempDir().then(function(tempDir) {
+ var styleMapPath = path.join(tempDir, "style-map");
+ fs.writeFileSync(styleMapPath, "p => span:fresh");
+ return runMammoth(testPath("single-paragraph.docx"), "--style-map", styleMapPath).then(function(result) {
+ assert.equal(result.stderrOutput, "")
+ assert.equal(result.output, "<span>Walking on imported air</span>")
+ });
+ });
+ });
});