blob: 26d146a269b1d0bc5fcb1e771be87fe6af4fd93d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
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"];
var outputDir = argv["output-dir"];
var styleMapPath = argv["style-map"];
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);
}
}
module.exports = main;
|