summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-08-12 21:32:41 +0100
committerMichael Williamson <mike@zwobble.org>2015-08-12 21:32:41 +0100
commit5f5217a4e16f7be697b24a46cf58e47753967d95 (patch)
tree5fa3113cf65ee3e3c554060b54a86cfe954f0a19
parentf71f3ea07839011e2fd60402069917e4fc2bed93 (diff)
Initial support for writing style maps to docx files
-rw-r--r--lib/docx/style-map.js31
-rw-r--r--lib/index.js24
-rw-r--r--lib/zipfile.js13
-rw-r--r--tests/mammoth.tests.js15
4 files changed, 75 insertions, 8 deletions
diff --git a/lib/docx/style-map.js b/lib/docx/style-map.js
new file mode 100644
index 0000000..dcc9f66
--- /dev/null
+++ b/lib/docx/style-map.js
@@ -0,0 +1,31 @@
+var promises = require("../promises");
+var unzip = require("../unzip");
+
+exports.writeStyleMap = writeStyleMap;
+exports.readStyleMap = readStyleMap;
+
+
+var styleMapPath = "mammoth/style-map";
+
+function writeStyleMap(options) {
+ return promises.when(unzip.openZip(options))
+ .then(function(zipFile) {
+
+ zipFile.write(styleMapPath, options.styleMap);
+
+ return {
+ toBuffer: zipFile.toBuffer
+ };
+ });
+}
+
+function readStyleMap(options) {
+ return promises.when(unzip.openZip(options))
+ .then(function(zipFile) {
+ if (zipFile.exists(styleMapPath)) {
+ return zipFile.read(styleMapPath, "utf8");
+ } else {
+ return promises.when(null);
+ }
+ });
+}
diff --git a/lib/index.js b/lib/index.js
index 74f42d0..a7afed7 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -10,6 +10,8 @@ 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;
function convertToHtml(input, options) {
return convert(input, options);
@@ -22,14 +24,22 @@ function convertToMarkdown(input, options) {
}
function convert(input, options) {
- var fullOptions = readOptions(options);
-
- return docxReader.read(input)
- .then(function(documentResult) {
- return documentResult.map(fullOptions.transformDocument);
+ options = options || {};
+ return docxStyleMap.readStyleMap(input)
+ .then(function(styleMap) {
+ if (!options.styleMap && styleMap) {
+ options.styleMap = styleMap;
+ }
})
- .then(function(documentResult) {
- return convertDocumentToHtml(documentResult, fullOptions);
+ .then(function() {
+ var fullOptions = readOptions(options);
+ return docxReader.read(input)
+ .then(function(documentResult) {
+ return documentResult.map(fullOptions.transformDocument);
+ })
+ .then(function(documentResult) {
+ return convertDocumentToHtml(documentResult, fullOptions);
+ });
});
}
diff --git a/lib/zipfile.js b/lib/zipfile.js
index 2c004b7..eb6bf66 100644
--- a/lib/zipfile.js
+++ b/lib/zipfile.js
@@ -19,9 +19,20 @@ function openArrayBuffer(arrayBuffer) {
return promises.when(buffer);
}
}
+
+ function write(name, contents) {
+ zipFile.file(name, contents);
+ }
+
+ function toBuffer() {
+ return zipFile.generate({type: "nodebuffer"});
+ }
+
return {
exists: exists,
- read: read
+ read: read,
+ write: write,
+ toBuffer: toBuffer
};
}
diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js
index ad7a058..41a3a22 100644
--- a/tests/mammoth.tests.js
+++ b/tests/mammoth.tests.js
@@ -80,6 +80,21 @@ describe('mammoth', function() {
});
});
+ test('style map can be saved and read from document buffer', function() {
+ var docxPath = path.join(__dirname, "test-data/single-paragraph.docx");
+ return promises.nfcall(fs.readFile, docxPath)
+ .then(function(buffer) {
+ return mammoth.writeStyleMapToDocx({buffer: buffer, styleMap: "p => h1"});
+ })
+ .then(function(docx) {
+ return mammoth.convertToHtml({buffer: docx.toBuffer()});
+ })
+ .then(function(result) {
+ assert.equal(result.value, "<h1>Walking on imported air</h1>");
+ assert.deepEqual(result.messages, []);
+ });
+ });
+
test('options are passed to document converter when calling mammoth.convertToHtml', function() {
var docxFile = createFakeDocxFile({
"word/document.xml": testData("simple/word/document.xml")