Serve up markdown based on filename in the data slide folder. Use the index to find what file to use. Removed ability to run code on slide on the server. Wasn't that useful for how much of a pain it is.
110 lines
2.5 KiB
JavaScript
Executable File
110 lines
2.5 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
//# vi: ft=javascript
|
|
|
|
var fs = require('fs');
|
|
var path = require('path');
|
|
|
|
var _ = require('lodash');
|
|
var express = require('express');
|
|
var handlebars = require('handlebars');
|
|
|
|
var app = express();
|
|
var staticHandler = express['static'].bind(express);
|
|
|
|
app.use((function log() {
|
|
var text = '{{date}} {{req.method}} {{req.url}}';
|
|
var logTemplate = handlebars.compile(text);
|
|
|
|
return function (req, res, next) {
|
|
console.log(logTemplate({date: new Date().toISOString(), req: req}));
|
|
next();
|
|
};
|
|
}()));
|
|
|
|
function renderError(res, err) {
|
|
console.error(err.toString());
|
|
res.writeHead(500, {'Content-Type': 'text/plain'});
|
|
res.send(err.toString());
|
|
}
|
|
|
|
function pathToFile() {
|
|
return path.normalize(
|
|
path.join.apply(null, [__dirname, '..'].concat(_.toArray(arguments)))
|
|
);
|
|
}
|
|
|
|
function index(req, res) {
|
|
var indexFile = pathToFile('assets', 'index.html');
|
|
|
|
function compileServe(err, text) {
|
|
if (err) {
|
|
renderError(res, err);
|
|
return;
|
|
}
|
|
var tmpl = handlebars.compile(text);
|
|
res.send(tmpl());
|
|
}
|
|
|
|
fs.readFile(indexFile, {encoding: 'utf8'}, compileServe);
|
|
}
|
|
|
|
app.get('/', index);
|
|
|
|
app.get('/slides', function showHandler(req, res) {
|
|
fs.readdir(pathToFile('data', 'slide'), function (err, files) {
|
|
files.sort();
|
|
var jsonFiles = _.filter(files, function (file) {
|
|
return /^\d+\.md$/.test(file);
|
|
});
|
|
|
|
res.send(_.map(jsonFiles, function (file) {
|
|
return {
|
|
id: parseInt(file.replace(/\.md$/, ''), 10)
|
|
};
|
|
}));
|
|
});
|
|
});
|
|
|
|
app.get(/slide\/(\d+)/, function showHandler(req, res, next) {
|
|
if (!req.xhr) {
|
|
index(req, res);
|
|
return;
|
|
}
|
|
|
|
var slideIndex = req.params[0];
|
|
|
|
fs.readdir(pathToFile('data', 'slide'), function (err, files) {
|
|
if (err) {
|
|
return renderError(res, err);
|
|
}
|
|
|
|
files.sort();
|
|
var markdownFiles = _.filter(files, function (file) {
|
|
return /\.md$/.test(file);
|
|
});
|
|
|
|
if (slideIndex > markdownFiles.length) {
|
|
return next();
|
|
}
|
|
|
|
var markdownFile = markdownFiles[slideIndex - 1];
|
|
var markdownPath = pathToFile('data', 'slide', markdownFile);
|
|
fs.readFile(markdownPath, {encoding: 'utf8'}, function (err, content) {
|
|
if (err) {
|
|
return renderError(res, err);
|
|
}
|
|
|
|
res.send({
|
|
id: parseInt(slideIndex, 10),
|
|
copy: content
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
app.use('/data', staticHandler('data'));
|
|
app.use('/vendor', staticHandler('vendor'));
|
|
app.use('/static', staticHandler('static'));
|
|
|
|
app.listen(8000);
|