aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-04-11 20:21:10 +0100
committerMichael Williamson <mike@zwobble.org>2013-04-11 20:21:10 +0100
commit1d9da5d8ed67e3f31ebda8f12082e6e607c673f0 (patch)
tree379df4a95ecafbae772d8053e5a96b2fb90e73d1
parentc57b900666a82b4ccce033a923e53906255078c0 (diff)
Allow Uint8Array to be input for conversion
-rw-r--r--lib/docx-reader.js12
-rw-r--r--package.json3
-rw-r--r--tests/mammoth.tests.js18
3 files changed, 29 insertions, 4 deletions
diff --git a/lib/docx-reader.js b/lib/docx-reader.js
index 832ff31..09bc8c1 100644
--- a/lib/docx-reader.js
+++ b/lib/docx-reader.js
@@ -8,6 +8,7 @@ var unzip = {
var path = require("path");
var fs = require("fs");
var Buffers = require("buffers");
+var streamBuffers = require("stream-buffers");
var xmlreader = require("./xmlreader");
var documents = require("./documents");
@@ -19,9 +20,14 @@ function read(options) {
var docxFile;
if (options.path) {
var zipPath = docxFile;
- docxFile = openZip(options.path);
+ docxFile = openZipStream(fs.createReadStream(options.path));
} else if (options.file) {
docxFile = options.file;
+ } else if (options.uint8Array) {
+ var buffer = new Buffer(options.uint8Array);
+ var stream = new streamBuffers.ReadableStreamBuffer();
+ stream.put(buffer);
+ docxFile = openZipStream(stream);
} else {
return q.reject(new Error("Could not find file in options"));
}
@@ -38,12 +44,12 @@ function read(options) {
});
}
-function openZip(zipPath) {
+function openZipStream(stream) {
var deferred = q.defer();
var names = [];
var files = {};
- fs.createReadStream(zipPath)
+ stream
.pipe(unzip.Parse())
.on("entry", function(entry) {
names.push(entry.path);
diff --git a/package.json b/package.json
index 993834c..fdfe60d 100644
--- a/package.json
+++ b/package.json
@@ -18,7 +18,8 @@
"connect": "~2.7.3",
"dustjs-linkedin": "~1.2.1",
"async": "~0.2.6",
- "buffers": "~0.1.1"
+ "buffers": "~0.1.1",
+ "stream-buffers": "~0.2.4"
},
"devDependencies": {
"mocha": "~1.8",
diff --git a/tests/mammoth.tests.js b/tests/mammoth.tests.js
index 9e81a84..fe5d72a 100644
--- a/tests/mammoth.tests.js
+++ b/tests/mammoth.tests.js
@@ -1,5 +1,8 @@
var assert = require("assert");
var path = require("path");
+var fs = require("fs");
+
+var q = require("q");
var mammoth = require("../")
var htmlPaths = require("../lib/html-paths");
@@ -19,6 +22,21 @@ describe('mammoth', function() {
});
});
+ test('should convert Uint8Array containing one one paragraph to single p element', function() {
+ var docxPath = path.join(__dirname, "test-data/single-paragraph.docx");
+
+ return q.nfcall(fs.readFile, docxPath).then(function(buffer) {
+ var array = new Uint8Array(buffer.length);
+ for (var i = 0; i < buffer.length; i++) {
+ array[i] = buffer[i];
+ }
+ var converter = new mammoth.Converter();
+ return converter.convertToHtml({uint8Array: array}).then(function(result) {
+ assert.equal("<p>Walking on imported air</p>", result.html);
+ });
+ });
+ });
+
test('options are passed to document converter', function() {
var docxFile = createFakeDocxFile({
"word/document.xml": testData("simple/word/document.xml")