Update sample code

This commit is contained in:
Tim Caswell 2012-01-18 18:42:29 +01:00
parent 5a1b0f85f4
commit 540dcaa1e3
4 changed files with 13 additions and 14 deletions

2
app.js
View File

@ -1,7 +1,6 @@
var Express = require('express'); var Express = require('express');
var Routes = require('./routes'); var Routes = require('./routes');
// TODO: Uncomment when we have internet
// var TryCatch = require('./trycatch'); // var TryCatch = require('./trycatch');
var App = module.exports = Express.createServer(); var App = module.exports = Express.createServer();
@ -12,7 +11,6 @@ App.configure(function(){
App.set('views', __dirname + '/views'); App.set('views', __dirname + '/views');
App.set('view engine', 'jade'); App.set('view engine', 'jade');
// This gives us scoped errors with long stack traces // This gives us scoped errors with long stack traces
// TODO: Uncomment when we have internet
// App.use(function (req, res, next) { // App.use(function (req, res, next) {
// TryCatch(next, next); // TryCatch(next, next);
// }); // });

18
db.js
View File

@ -8,26 +8,26 @@ function pathFromName(name) {
return Path.join(__dirname, "pages", name + ".markdown"); return Path.join(__dirname, "pages", name + ".markdown");
} }
// Load a file from disk and parse out the title and generate the HTML // Load a file, parse the title and generate the HTML
exports.loadPage = function (name, callback) { exports.loadPage = function (name, callback) {
// Attempt to load the file from disk
var path = pathFromName(name); var path = pathFromName(name);
FS.readFile(path, 'utf8', function (err, markdown) { FS.readFile(path, 'utf8', function (err, markdown) {
var exists = true; var exists = true;
if (err) { if (err) {
// If it's not there, generate a placeholder body
if (err.code === "ENOENT") { if (err.code === "ENOENT") {
markdown = "# " + name.replace(/_/g, " ") + "\n\n" + // Generate a placeholder body.
"This page does not exist yet. Be the first to write it."; markdown = "# " + name.replace(/_/g, " ") +
"\n\n" + "This page does not exist yet.";
exists = false; exists = false;
} else { } else {
// Forward all other errors on // Forward on all other errors.
return callback(err); return callback(err);
} }
} }
// Parse the markdown extracting the first header as the title // Parse and render the markdown.
// and then render as an HTML string
var tree = Markdown.parse(markdown); var tree = Markdown.parse(markdown);
var title = name; var title = name;
for (var i = 1, l = tree.length; i < l; i++) { for (var i = 1, l = tree.length; i < l; i++) {
@ -39,7 +39,6 @@ exports.loadPage = function (name, callback) {
} }
var html = Markdown.toHTML(tree); var html = Markdown.toHTML(tree);
// Send back the page as an object
callback(null, { callback(null, {
name: name, name: name,
title: title, title: title,
@ -47,6 +46,7 @@ exports.loadPage = function (name, callback) {
markdown: markdown, markdown: markdown,
html: html, html: html,
}); });
}); });
}; };

View File

@ -5,6 +5,7 @@
"dependencies": { "dependencies": {
"express": "2.5.5", "express": "2.5.5",
"jade": ">= 0.0.1", "jade": ">= 0.0.1",
"markdown": "~0.3.1" "markdown": "~0.3.1",
"trycatch": "~0.1.0"
} }
} }

View File

@ -1,3 +1,3 @@
h1= title h1= title
.article!= html .article!= html
a(href="/" + name + "/edit")= (exists ? "Edit" : "Create") + " this Page" a(href="/" + name + "/edit")= "Edit this Page"