Rename files to make it easier to move files around

master
Buddy Sandidge 10 years ago
parent f7c472d762
commit 180ea98786

@ -11,6 +11,10 @@ var handlebars = require('handlebars');
var app = express(); var app = express();
var staticHandler = express['static'].bind(express); var staticHandler = express['static'].bind(express);
function toInt(num) {
return parseInt(num, 10);
}
app.use((function log() { app.use((function log() {
var text = '{{date}} {{req.method}} {{req.url}}'; var text = '{{date}} {{req.method}} {{req.url}}';
var logTemplate = handlebars.compile(text); var logTemplate = handlebars.compile(text);
@ -50,17 +54,27 @@ function index(req, res) {
app.get('/', index); app.get('/', index);
app.get('/slides', function showHandler(req, res) { function getMarkdownFiles(cb) {
fs.readdir(pathToFile('data', 'slide'), function (err, files) { fs.readdir(pathToFile('data', 'slide'), function (err, files) {
if (err) {
return cb(err);
}
files.sort(); files.sort();
var jsonFiles = _.filter(files, function (file) { var markdownFiles = _.filter(files, function (file) {
return /^\d+\.md$/.test(file); return /^\d+\.md$/.test(file);
}); });
cb(null, markdownFiles);
});
}
app.get('/slides', function showHandler(req, res) {
getMarkdownFiles(function (err, files) {
if (err) {
return renderError(res, err);
}
res.send(_.map(jsonFiles, function (file) { res.send(_.map(files, function (file, index) {
return { return {id: index + 1};
id: parseInt(file.replace(/\.md$/, ''), 10)
};
})); }));
}); });
}); });
@ -71,33 +85,23 @@ app.get(/slide\/(\d+)/, function showHandler(req, res, next) {
return; return;
} }
var slideIndex = req.params[0]; var slideIndex = toInt(req.params[0]);
fs.readdir(pathToFile('data', 'slide'), function (err, files) { getMarkdownFiles(function (err, files) {
if (err) { if (err) {
return renderError(res, err); return renderError(res, err);
} }
if (slideIndex > files.length) {
files.sort();
var markdownFiles = _.filter(files, function (file) {
return /\.md$/.test(file);
});
if (slideIndex > markdownFiles.length) {
return next(); return next();
} }
var markdownFile = markdownFiles[slideIndex - 1]; var markdownFile = files[slideIndex - 1];
var markdownPath = pathToFile('data', 'slide', markdownFile); var markdownPath = pathToFile('data', 'slide', markdownFile);
fs.readFile(markdownPath, {encoding: 'utf8'}, function (err, content) { fs.readFile(markdownPath, {encoding: 'utf8'}, function (err, content) {
if (err) { if (err) {
return renderError(res, err); return renderError(res, err);
} }
res.send({id: toInt(slideIndex), copy: content});
res.send({
id: parseInt(slideIndex, 10),
copy: content
});
}); });
}); });
}); });

Loading…
Cancel
Save