From 26f7cefe6379e9b76f5bb1963433a6832a55caa6 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 30 Mar 2013 22:01:09 +0000 Subject: Allow path to be passed into convertToHtml --- lib/index.js | 14 ++++++++++++++ lib/main.js | 15 +-------------- lib/web.js | 13 +------------ 3 files changed, 16 insertions(+), 26 deletions(-) (limited to 'lib') diff --git a/lib/index.js b/lib/index.js index a4d51a6..5db51a8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,4 +1,6 @@ var q = require("q"); +var _ = require("underscore"); +var ZipFile = require("zipfile").ZipFile; var xmlreader = require("./xmlreader"); var documents = require("./documents"); @@ -19,6 +21,18 @@ function Converter(options) { } Converter.prototype.convertToHtml = function(docxFile) { + if (_.isString(docxFile)) { + var zipFile = new ZipFile(docxFile); + docxFile = { + read: function(path) { + return q.ninvoke(zipFile, "readFile", path, "utf-8") + .then(function(value) { + return value.toString(); + }); + } + }; + } + var documentConverter = this._documentConverter; return docxFile.read("word/document.xml").then(function(documentXmlString) { return parseXml(documentXmlString); diff --git a/lib/main.js b/lib/main.js index 771c382..2bffccb 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,24 +1,11 @@ var optimist = require("optimist"); -var ZipFile = require("zipfile").ZipFile; -var q = require("q"); var mammoth = require("./"); - var argv = optimist.argv; -var zipFile = new ZipFile(argv._[0]); -var docxFile = { - read: function(path) { - return q.ninvoke(zipFile, "readFile", path, "utf-8") - .then(function(value) { - return value.toString(); - }); - } -}; - var converter = new mammoth.Converter(); -converter.convertToHtml(docxFile) +converter.convertToHtml(argv._[0]) .then(function(result) { console.log(result); }) diff --git a/lib/web.js b/lib/web.js index 73e501d..1d10693 100644 --- a/lib/web.js +++ b/lib/web.js @@ -4,7 +4,6 @@ var url = require("url"); var fs = require("fs"); var q = require("q"); var mammoth = require("./"); -var ZipFile = require("zipfile").ZipFile; function startServer(options) { var app = connect() @@ -49,18 +48,8 @@ function handleConvert(request, response, next) { var converter = new mammoth.Converter(mammoth.standardOptions); var uploadedDocument = request.files.document; var documentPath = uploadedDocument.path - // TODO: remove duplication from index.js - var zipFile = new ZipFile(documentPath); - var docxFile = { - read: function(path) { - return q.ninvoke(zipFile, "readFile", path, "utf-8") - .then(function(value) { - return value.toString(); - }); - } - }; - converter.convertToHtml(docxFile) + converter.convertToHtml(documentPath) .then(function(result) { request.template = { name: "convert.html", -- cgit v1.2.3