aboutsummaryrefslogtreecommitdiff
path: root/lib/index.js
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-08-12 21:57:45 +0100
committerMichael Williamson <mike@zwobble.org>2015-08-12 21:57:45 +0100
commit4b4ec85bc4f49e72244f7219539124b45b876d8e (patch)
tree0aa4c2005526fa5130bccbfd26f3a9cab6f8d4cb /lib/index.js
parent5f5217a4e16f7be697b24a46cf58e47753967d95 (diff)
Open zip file before passing to readers
Diffstat (limited to 'lib/index.js')
-rw-r--r--lib/index.js35
1 files changed, 26 insertions, 9 deletions
diff --git a/lib/index.js b/lib/index.js
index a7afed7..a988745 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -1,7 +1,9 @@
var docxReader = require("./docx/docx-reader");
+var docxStyleMap = require("./docx/style-map");
var DocumentConverter = require("./document-to-html").DocumentConverter;
var readStyle = require("./style-reader").readStyle;
var readOptions = require("./options-reader").readOptions;
+var unzip = require("./unzip");
exports.convertToHtml = convertToHtml;
exports.convertToMarkdown = convertToMarkdown;
@@ -10,8 +12,7 @@ exports.extractRawText = extractRawText;
exports.images = require("./images");
exports.transforms = require("./transforms");
exports.underline = require("./underline");
-var docxStyleMap = require("./docx/style-map");
-exports.writeStyleMapToDocx = docxStyleMap.writeStyleMap;
+exports.writeStyleMapToDocx = writeStyleMapToDocx;
function convertToHtml(input, options) {
return convert(input, options);
@@ -25,15 +26,18 @@ function convertToMarkdown(input, options) {
function convert(input, options) {
options = options || {};
- return docxStyleMap.readStyleMap(input)
- .then(function(styleMap) {
- if (!options.styleMap && styleMap) {
- options.styleMap = styleMap;
+
+ return unzip.openZip(input)
+ .tap(function(docxFile) {
+ if (!options.styleMap) {
+ return docxStyleMap.readStyleMap(docxFile).then(function(styleMap) {
+ options.styleMap = styleMap || "";
+ });
}
})
- .then(function() {
+ .then(function(docxFile) {
var fullOptions = readOptions(options);
- return docxReader.read(input)
+ return docxReader.read(docxFile)
.then(function(documentResult) {
return documentResult.map(fullOptions.transformDocument);
})
@@ -41,6 +45,7 @@ function convert(input, options) {
return convertDocumentToHtml(documentResult, fullOptions);
});
});
+
}
function convertDocumentToHtml(documentResult, options) {
@@ -54,7 +59,8 @@ function convertDocumentToHtml(documentResult, options) {
function extractRawText(input) {
- return docxReader.read(input)
+ return unzip.openZip(input)
+ .then(docxReader.read)
.then(function(documentResult) {
return documentResult.map(convertElementToRawText);
});
@@ -69,6 +75,17 @@ function convertElementToRawText(element) {
}
}
+function writeStyleMapToDocx(input, styleMap) {
+ return unzip.openZip(input)
+ .then(function(docxFile) {
+ docxStyleMap.writeStyleMap(docxFile, styleMap);
+
+ return {
+ toBuffer: docxFile.toBuffer
+ };
+ });
+}
+
exports.styleMapping = function() {
throw new Error('Use a raw string instead of mammoth.styleMapping e.g. "p[style-name=\'Title\'] => h1" instead of mammoth.styleMapping("p[style-name=\'Title\'] => h1")');
};