aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-03-30 21:50:00 +0000
committerMichael Williamson <mike@zwobble.org>2013-03-30 21:50:00 +0000
commitd6e8f5801e036bf3adccf5d0d826f5e2f7f60bda (patch)
treeeabc0df2f16b3e0b3629011dd1b485b2d48d5d35
parent2bae91155de4c8af88db552f26105b9d09fd972e (diff)
Add hacked together web interface
-rw-r--r--.gitignore2
-rw-r--r--lib/web.js109
-rw-r--r--makefile7
-rw-r--r--package.json3
-rw-r--r--templates/convert.html23
-rw-r--r--templates/index.html31
6 files changed, 174 insertions, 1 deletions
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 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Mammoth</title>
+ <link rel="stylesheet" type="text/css" href="/static/bootstrap/css/bootstrap.min.css">
+ </head>
+ <body>
+ <div class="container-fluid">
+ <div class="row-fluid">
+ <h1>Mammoth</h1>
+ </div>
+ <div class="row-fluid">
+ <div class="span4 well">
+ <p>Uploaded document:<br /> <strong>{{documentName}}</strong></p>
+ </div>
+ <div class="span8">
+ {{documentHtml}}
+ </div>
+ </div>
+ </div>
+ </body>
+</html>
+
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 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Mammoth</title>
+ <link rel="stylesheet" type="text/css" href="/static/bootstrap/css/bootstrap.min.css">
+ </head>
+ <body>
+ <div class="container-fluid">
+ <div class="row-fluid">
+ <h1>Mammoth</h1>
+ </div>
+ <div class="row-fluid">
+ <div class="span4 well">
+ <form action="/convert" method="POST" enctype="multipart/form-data">
+ <p>
+ <label>
+ Select <code>.docx</code> file:<br>
+ <input type="file" name="document">
+ </label>
+ </p>
+ <p>
+ <input type="submit" value="Convert" class="btn">
+ </p>
+ </form>
+ </div>
+ <div class="span8">
+ </div>
+ </div>
+ </div>
+ </body>
+</html>