From d6e8f5801e036bf3adccf5d0d826f5e2f7f60bda Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 30 Mar 2013 21:50:00 +0000 Subject: Add hacked together web interface --- .gitignore | 2 + lib/web.js | 109 +++++++++++++++++++++++++++++++++++++++++++++++++ makefile | 7 ++++ package.json | 3 +- templates/convert.html | 23 +++++++++++ templates/index.html | 31 ++++++++++++++ 6 files changed, 174 insertions(+), 1 deletion(-) create mode 100644 lib/web.js create mode 100644 templates/convert.html create mode 100644 templates/index.html diff --git a/.gitignore b/.gitignore index 07e6e47..928aae9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /node_modules +/_build +/static/bootstrap diff --git a/lib/web.js b/lib/web.js new file mode 100644 index 0000000..20bc44f --- /dev/null +++ b/lib/web.js @@ -0,0 +1,109 @@ +var http = require("http"); +var connect = require("connect"); +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() + .use(connect.bodyParser()) + .use("/static/", connect.static(__dirname + '/../static')) + .use(handlePage) + .use(renderTemplate); + http.createServer(app).listen(options.port); +} + + +var handlers = { + "/": handleIndex, + "/convert": handleConvert +}; + +function handlePage(request, response, next) { + var pathname = url.parse(request.url).pathname; + var handler = handlers[pathname]; + if (handler) { + handler(request, response, next); + } else { + next(); + } +} + +function handleIndex(request, response, next) { + if (url.parse(request.url).pathname !== "/") { + return next(); + } + request.template = { + name: "index.html" + }; + next(); +} + +function handleConvert(request, response, next) { + if (url.parse(request.url).pathname !== "/convert") { + return next(); + } + + var converter = new mammoth.Converter(); + 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) + .then(function(result) { + request.template = { + name: "convert.html", + values: { + documentName: uploadedDocument.name, + documentHtml: result.html + } + }; + next(); + }) + .fin(function() { + fs.unlink(documentPath); + }); +} + +function renderTemplate(request, response, next) { + if (!request.template) { + return next(); + } + var templateName = request.template.name; + q.nfcall(fs.readFile, "templates/" + templateName, "utf-8") + .then(function(templateContent) { + var values = request.template.values || {}; + var content = templateContent; + for (var key in values) { + if (Object.prototype.hasOwnProperty.call(values, key)) { + content = content.replace("{{" + key + "}}", values[key]); + } + } + response.writeHead(200, { + "content-type": "text/html; charset=utf-8" + }); + response.end(content); + }) + .fail(function(error) { + console.log(error); + next(error); + }); +} + +if (require.main === module) { + var argv = require("optimist").argv; + var port = argv.port ? parseInt(argv.port, 10) : 3000; + startServer({port: port}); + console.log("Server running on http://localhost:" + port); +} diff --git a/makefile b/makefile index 6fb8f49..c95ce77 100644 --- a/makefile +++ b/makefile @@ -2,3 +2,10 @@ test: node_modules/.bin/mocha tests + +setup: + mkdir -p _build/bootstrap + curl -L https://github.com/twitter/bootstrap/archive/v2.3.1.tar.gz | tar xz --directory _build/bootstrap --strip-components 1 + cd _build/bootstrap; npm install; make bootstrap + cp -rT _build/bootstrap/bootstrap static/bootstrap + diff --git a/package.json b/package.json index 0d7da11..d1a1073 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "underscore": "~1.4.4", "lop": "~0.2.6", "zipfile": "~0.3.4", - "optimist": "~0.3.5" + "optimist": "~0.3.5", + "connect": "~2.7.3" }, "devDependencies": { "mocha": "~1.8" diff --git a/templates/convert.html b/templates/convert.html new file mode 100644 index 0000000..6624e3a --- /dev/null +++ b/templates/convert.html @@ -0,0 +1,23 @@ + + + + Mammoth + + + +
+
+

Mammoth

+
+
+
+

Uploaded document:
{{documentName}}

+
+
+ {{documentHtml}} +
+
+
+ + + diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..7eaae65 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,31 @@ + + + + Mammoth + + + +
+
+

Mammoth

+
+
+
+
+

+ +

+

+ +

+
+
+
+
+
+
+ + -- cgit v1.2.3