diff options
| author | Michael Williamson <mike@zwobble.org> | 2014-01-06 22:32:27 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2014-01-06 22:32:27 +0000 |
| commit | 723a7d54bd4823b2a9876fdea7b8b08cde74f669 (patch) | |
| tree | 048096d0bfbdaa9ff889143aff5f8f150e0267cc /lib | |
| parent | 278082b9fe502ce1ca083db66beb2c25c4a6ca5f (diff) | |
Use same module for node.js and browsers
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/browser.js | 98 |
1 files changed, 0 insertions, 98 deletions
diff --git a/lib/browser.js b/lib/browser.js deleted file mode 100644 index a9b9dbd..0000000 --- a/lib/browser.js +++ /dev/null @@ -1,98 +0,0 @@ -var q = require("q"); -var mammoth = require("./"); - - -exports.fileInput = fileInput; -exports.readFileInputOnChange = readFileInputOnChange; -exports.standardOptions = mammoth.standardOptions; -exports.htmlPaths = mammoth.htmlPaths; -exports.style = mammoth.style; - -exports.convertDocumentToHtml = callbackify(mammoth.convertDocumentToHtml); -exports.convertArrayBufferToHtml = convertArrayBufferToHtml; - -function callbackify(func) { - return function() { - var args = Array.prototype.slice.call(arguments, 0, arguments.length - 1); - var callback = arguments[arguments.length - 1]; - satisfyCallback(func.apply(this, args), callback); - }; -} - -function convertArrayBufferToHtml(arrayBuffer, options, callback) { - try { - var file = openFile(arrayBuffer); - var result = mammoth.read({file: file}).then(function(documentResult) { - options = options || mammoth.standardOptions; - return mammoth.convertDocumentToHtml(documentResult, options); - }); - satisfyCallback(result, callback); - } catch (error) { - callback({error: error}); - } -} - -function readFileInputOnChange(inputElement, callback) { - function handleFileSelect(event) { - var file = event.target.files[0]; - - var reader = new FileReader(); - - reader.onload = function(loadEvent) { - var arrayBuffer = loadEvent.target.result; - try { - var file = openFile(arrayBuffer); - satisfyCallback(mammoth.read({file: file}), callback); - } catch (error) { - callback({error: error}); - } - }; - - reader.readAsArrayBuffer(file); - } - inputElement.addEventListener('change', handleFileSelect, false); -} - -function fileInput(inputElement, callback) { - readFileInputOnChange(inputElement, function(documentResult) { - if (documentResult.error) { - callback(documentResult); - } else { - var options = mammoth.standardOptions; - satisfyCallback( - mammoth.convertDocumentToHtml(documentResult, options), - callback - ); - } - - }); -} - - -function openFile(arrayBuffer) { - var zipFile = new JSZip(arrayBuffer); - function exists(name) { - return zipFile.file(name) !== null; - } - - function read(name, encoding) { - var array = zipFile.file(name).asUint8Array(); - var buffer = new Buffer(array); - if (encoding) { - return q.when(buffer.toString(encoding)); - } else { - return q.when(buffer); - } - } - return { - exists: exists, - read: read - }; -} - - -function satisfyCallback(promise, callback) { - promise.then(callback, function(error) { - callback({error: error}); - }).done(); -} |
