Permalink
Browse files

converting blog to prototyped object

  • Loading branch information...
1 parent dcfd8c3 commit b68b6ad44c502d7d28390877f1880300d28b4bc6 @selfcontained committed Apr 16, 2012
View
@@ -1,101 +1,96 @@
-var Blog = function(cfg) {
- //todo: convert to protoype for consistency
- var app,
- config = cfg,
- api = require('./article.api.js').create(),
- handlers = [
- require('./handlers/assets.js'),
- require('./handlers/error.js'),
- require('./handlers/homepage.js'),
- require('./handlers/article.js'),
- require('./handlers/archive.js'),
- require('./handlers/feed.js'),
- require('./handlers/tag.js')
- ];
-
- return {
-
- /**
- * Create and setup http server for serving blog dynamically
- */
- createApp : function(config) {
- var express = require('express'),
- self = this;
-
- app = express.createServer();
- app.configure(function(){
- app.set('views', config.templates);
- app.set('view engine', config.view_engine);
- app.set('view options', config.view_options);
- app.set('view cache', config.view_cache);
- app.use(express.errorHandler({ showStack: true, dumpExceptions: true }));
- app.use(express.bodyParser());
- app.use(express.static(__dirname + '/../theme/static'));
- app.use(app.router);
- });
- handlers.forEach(function(handler) {
- handler.register && handler.register(self);
- });
- return this;
- },
-
- /**
- * Load article data into memory
- */
- load : function(cb) {
- var self = this,
- articlePath = require('path').normalize(__dirname+'/..'+config.articles);
-
- api.load(articlePath, function() {
- cb.call(self);
- });
- },
-
- /**
- * Create static version of blog in specified directory
- */
- generate : function(dir) {
- var fs = require('fs'),
- self = this;
-
- if(require('path').existsSync(dir)) {
- require('wrench').rmdirSyncRecursive(dir);
- console.log('removed directory: ', dir);
- }
- fs.mkdir(dir, function(err) {
- if(err) {
- console.log(err);
- }else {
- console.log('create directory: ', dir);
- handlers.forEach(function(handler) {
- handler.generate(self, dir);
- });
- }
- });
- },
-
- /**
- * Pass-thru
- */
- listen : function(port) {
- app.listen(port);
- console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
- return this;
- },
-
- config : function() {
- return config;
- },
-
- app : function() {
- return app;
- },
-
- api : function() {
- return api;
+var Blog = function(config) {
+ this.config = config;
+ this.api = require('./article.api.js').create(),
+ this.handlers = [
+ require('./handlers/assets.js'),
+ require('./handlers/error.js'),
+ require('./handlers/homepage.js'),
+ require('./handlers/article.js'),
+ require('./handlers/archive.js'),
+ require('./handlers/feed.js'),
+ require('./handlers/tag.js')
+ ];
+};
+
+Blog.prototype = {
+
+ app : null,
+
+ config : null,
+
+ api : null,
+
+ handlers : null,
+
+ /**
+ * Create and setup http server for serving blog dynamically
+ */
+ createApp : function(config) {
+ var express = require('express'),
+ self = this;
+
+ this.app = express.createServer();
+ this.app.configure(function(){
+ console.log(self.config);
+ self.app.set('views', config.templates);
+ self.app.set('view engine', config.view_engine);
+ self.app.set('view options', config.view_options);
+ self.app.set('view cache', config.view_cache);
+ self.app.use(express.errorHandler({ showStack: true, dumpExceptions: true }));
+ self.app.use(express.bodyParser());
+ self.app.use(express.static(__dirname + '/../theme/static'));
+ self.app.use(self.app.router);
+ });
+ this.handlers.forEach(function(handler) {
+ handler.register && handler.register(self);
+ });
+ return this;
+ },
+
+ /**
+ * Load article data into memory
+ */
+ load : function(cb) {
+ var self = this,
+ articlePath = require('path').normalize(__dirname+'/..'+this.config.articles);
+
+ this.api.load(articlePath, function() {
+ cb.call(self);
+ });
+ },
+
+ /**
+ * Create static version of blog in specified directory
+ */
+ generate : function(dir) {
+ var fs = require('fs'),
+ self = this;
+
+ if(require('path').existsSync(dir)) {
+ require('wrench').rmdirSyncRecursive(dir);
+ console.log('removed directory: ', dir);
}
+ fs.mkdir(dir, function(err) {
+ if(err) {
+ console.log(err);
+ }else {
+ console.log('create directory: ', dir);
+ self.handlers.forEach(function(handler) {
+ handler.generate(self, dir);
+ });
+ }
+ });
+ },
+
+ /**
+ * Pass-thru
+ */
+ listen : function(port) {
+ this.app.listen(port);
+ console.log("Express server listening on port %d in %s mode", this.app.address().port, this.app.settings.env);
+ return this;
+ }
- };
};
module.exports = {
@@ -1,10 +1,10 @@
var handler = require('./handler.js');
function getTemplateData(blog) {
- var config = blog.config();
+ var config = blog.config;
return {
- recent_articles : blog.api().getRecent(),
- articles : blog.api().getAll(),
+ recent_articles : blog.api.getRecent(),
+ articles : blog.api.getAll(),
title : 'archive - ' + config.title,
keywords : config.keywords
};
@@ -13,7 +13,7 @@ function getTemplateData(blog) {
module.exports = {
register : function(blog) {
- blog.app().get('/archive/', function(req, res) {
+ blog.app.get('/archive/', function(req, res) {
res.render('archive', getTemplateData(blog));
});
},
@@ -4,7 +4,7 @@ var path = require('path'),
function getTemplateData(blog, article) {
return {
- recent_articles : blog.api().getRecent(),
+ recent_articles : blog.api.getRecent(),
article : article,
title : article.title,
keywords : article.tags.join()
@@ -14,21 +14,21 @@ function getTemplateData(blog, article) {
module.exports = {
register : function(blog) {
- blog.app().get(/^\/(\d){4}\/(\d){2}\/(\d){2}\/(.+)\/$/, function(req, res) {
- var article = blog.api().get(req.params[3]);
+ blog.app.get(/^\/(\d){4}\/(\d){2}\/(\d){2}\/(.+)\/$/, function(req, res) {
+ var article = blog.api.get(req.params[3]);
if(article && article.publish) {
res.render('article', getTemplateData(blog, article));
}else {
res.render('404', {
status: 404,
- recent_articles : blog.api().getRecent()
+ recent_articles : blog.api.getRecent()
});
}
});
},
generate : function(blog, dir) {
- blog.api().getAll().forEach(function(article) {
+ blog.api.getAll().forEach(function(article) {
if(article.publish) {
handler.createHtmlFile(
path.join(dir, article.path, 'index.html'),
@@ -27,7 +27,7 @@ function compileCSS(filename, cb) {
module.exports = {
register : function(blog) {
- blog.app().get('/css/:filename.css', function(req, res) {
+ blog.app.get('/css/:filename.css', function(req, res) {
var filename = req.param('filename')+'.less';
compileCSS(filename, function(css) {
@@ -2,9 +2,9 @@ var path = require('path'),
handler = require('./handler.js');
function getTemplateData(blog) {
- var config = blog.config();
+ var config = blog.config;
return {
- recent_articles : blog.api().getRecent(),
+ recent_articles : blog.api.getRecent(),
title : config.title,
keywords : config.keywords
};
@@ -3,15 +3,15 @@ var path = require('path'),
function getTemplateData(blog) {
return {
- articles : blog.api().getAll(),
- title : blog.config().title
+ articles : blog.api.getAll(),
+ title : blog.config.title
};
}
module.exports = {
register : function(blog) {
- blog.app().get('/feed/rss.xml', function(req, res) {
+ blog.app.get('/feed/rss.xml', function(req, res) {
res.contentType('application/xml');
res.render('feed', getTemplateData(blog));
});
@@ -2,10 +2,10 @@ var path = require('path'),
handler = require('./handler.js');
function getTemplateData(blog) {
- var config = blog.config();
+ var config = blog.config;
return {
- articles : blog.api().getRecent(10),
- recent_articles : blog.api().getRecent(),
+ articles : blog.api.getRecent(10),
+ recent_articles : blog.api.getRecent(),
title : config.title,
keywords : config.keywords
};
@@ -14,7 +14,7 @@ function getTemplateData(blog) {
module.exports = {
register : function(blog) {
- blog.app().get('/', function(req, res) {
+ blog.app.get('/', function(req, res) {
res.render('homepage', getTemplateData(blog));
});
},
View
@@ -2,10 +2,10 @@ var path = require('path'),
handler = require('./handler.js');
function getTemplateData(blog, tag) {
- var config = blog.config();
+ var config = blog.config;
return {
- articles : blog.api().getByTag(tag),
- recent_articles : blog.api().getRecent(),
+ articles : blog.api.getByTag(tag),
+ recent_articles : blog.api.getRecent(),
tag : tag,
title : tag + ' - ' + config.title,
keywords : config.keywords
@@ -15,13 +15,13 @@ function getTemplateData(blog, tag) {
module.exports = {
register : function(blog) {
- blog.app().get('/tag/:tag/', function(req, res) {
+ blog.app.get('/tag/:tag/', function(req, res) {
res.render('tag', getTemplateData(blog, req.param('tag')));
});
},
generate : function(blog, dir) {
- Object.keys(blog.api().tags()).forEach(function(tag) {
+ Object.keys(blog.api.tags()).forEach(function(tag) {
handler.createHtmlFile(
path.join(dir, 'tag', tag, 'index.html'),
'tag',

0 comments on commit b68b6ad

Please sign in to comment.