From 04646ddeed3991507d31d8354e11f785b77655ce Mon Sep 17 00:00:00 2001 From: Buddy Sandidge Date: Sun, 15 Feb 2015 19:09:55 -0800 Subject: [PATCH] Add ability to get slide contents by id --- bin/deck | 73 +++++++++++++++++++++++++++++++++++++++-- data/code/prototypes.js | 7 ++++ data/copy/title.md | 3 ++ data/slide/01.json | 4 +++ package.json | 1 + 5 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 data/code/prototypes.js create mode 100644 data/copy/title.md create mode 100644 data/slide/01.json diff --git a/bin/deck b/bin/deck index ffd666d..01accc7 100755 --- a/bin/deck +++ b/bin/deck @@ -2,9 +2,12 @@ //# vi: ft=javascript var fs = require('fs'); +var path = require('path'); -var handlebars = require('handlebars'); +var _ = require('lodash'); +var async = require('async'); var express = require('express'); +var handlebars = require('handlebars'); var app = express(); var staticHandler = express['static'].bind(express); @@ -19,12 +22,24 @@ app.use((function log() { }; }())); +function renderError(res, err) { + console.error(err); + res.writeHead(500, {'Content-Type': 'text/plain'}); + res.write(err); +} + +function pathToFile() { + return path.normalize( + path.join.apply(null, [__dirname, '..'].concat(_.toArray(arguments))) + ); +} + app.get('/', function index(req, res) { - var indexFile = __dirname + '/../assets/index.html'; + var indexFile = pathToFile('assets', 'index.html'); function compileServe(err, text) { if (err) { - res.writeHead(500, {'Content-Type': 'text/plain'}, err); + renderError(res, err); return; } var tmpl = handlebars.compile(text); @@ -34,6 +49,58 @@ app.get('/', function index(req, res) { fs.readFile(indexFile, {encoding: 'utf8'}, compileServe); }); +function serveIfExists(path) { + return function _serveIfExists(cb) { + fs.exists(path, function (exists) { + if (!exists) { + cb(null, null); + return; + } + fs.readFile(path, {encoding: 'utf8'}, cb); + }); + }; +} + +app.get(/slide\/(\d+)/, function showHandler(req, res, next) { + var slideId = req.params[0]; + var slidePath = pathToFile('data', 'slide', slideId + '.json'); + + fs.exists(slidePath, function (exists) { + if (!exists) { + next(); + return; + } + + fs.readFile(slidePath, {encoding: 'utf8'}, function (err, content) { + if (err) { + renderError(res, err); + return; + } + + try { + var slide = JSON.parse(content); + } catch (err) { + renderError(res, err); + return; + } + + async.parallel({ + copy: serveIfExists(pathToFile('data', 'copy', slide.copy + '.md')), + code: serveIfExists(pathToFile('data', 'code', slide.code + '.js')) + }, function (err, slideContent) { + if (err) { + renderError(res, err); + return; + } + slideContent.id = parseInt(slideId, 10); + slideContent.title = slide.title || ''; + res.send(slideContent); + }); + }); + + }); +}); + app.use('/data', staticHandler('data')); app.use('/vendor', staticHandler('vendor')); app.use('/static', staticHandler('static')); diff --git a/data/code/prototypes.js b/data/code/prototypes.js new file mode 100644 index 0000000..aca8eb3 --- /dev/null +++ b/data/code/prototypes.js @@ -0,0 +1,7 @@ +(function (phantom) { + console.log(Function.prototype.bind); + + if (phantom) { + phantom.exit(); + } +}(this.phantom)); diff --git a/data/copy/title.md b/data/copy/title.md new file mode 100644 index 0000000..27fa35c --- /dev/null +++ b/data/copy/title.md @@ -0,0 +1,3 @@ +# PhantomJS Under the Hood + + - Buddy Sandidge diff --git a/data/slide/01.json b/data/slide/01.json new file mode 100644 index 0000000..07cd2c3 --- /dev/null +++ b/data/slide/01.json @@ -0,0 +1,4 @@ +{ + "copy": "title", + "code": "prototypes" +} diff --git a/package.json b/package.json index 1c0ab61..da35761 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "requirejs": "^2.1.15" }, "dependencies": { + "async": "^0.9.0", "express": "^4.11.2", "handlebars": "^1.3.0" }