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

18
db.js
View File

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

View File

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

View File

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