Skip to content
Browse files

Merge pull request #10 from wlaurance/plugins

Merging plugin work from @wlaurance
  • Loading branch information...
2 parents 2967f7c + 9c35a46 commit 98a97010e78ab3c05938dddf61156644aaf106fe @guyht committed
Showing with 30 additions and 25 deletions.
  1. +21 −18 lib/glog.js
  2. +1 −1 plugins/example.js
  3. +8 −6 test/glog_tests.js
View
39 lib/glog.js
@@ -21,7 +21,8 @@ function Glog() {
* Object variable to track pages
*/
this.pages = {};
- this.articlePlugins = [{}];
+ this.articlePrePlugins = [{}];
+ this.articlePostPlugins = [{}];
};
/*
@@ -89,14 +90,19 @@ Glog.prototype.load_configs = function(cb) {
Glog.prototype.load_plugins = function(options, cb) {
var fn = this, i;
+ this.articlePrePlugins = [{}];
+ this.articlePostPlugins = [{}];
var GlogPlugin = {
- registerArticleHook : function(hook) {
- fn.articlePlugins.push(hook);
+ registerPreArticleHook : function(hook) {
+ fn.articlePrePlugins.push(hook);
+ },
+
+ registerPostArticleHook : function(hook){
+ fn.articlePostPlugins.push(hook);
}
};
-
- for(i=0;i<options.plugins.length;i++) {
+ for(var i=0;i<options.plugins.length;i++) {
try {
var p = require('../plugins/' + options.plugins[i]);
p.load(GlogPlugin);
@@ -185,19 +191,16 @@ Glog.prototype.load_articles = function(options, cb) {
'body' : body,
'url' : [year, month, (header.url || header.title.replace(/\s/g, '-'))].join('/')
});
-
- // If that was the last article then trigger the callback
- if(files.length === articles.length) {
- // Apply plugins
- fn.articlePlugins[0] = function(wcb) {
- wcb(null, articles);
- };
- fn.articlePlugins[fn.articlePlugins.length] = function(res) {
- console.log('CALLING');
- cb(res.reverse());
- };
-
- async.waterfall(fn.articlePlugins);
+ if (files.length === articles.length && fn.articlePostPlugins.length > 1){
+ fn.articlePostPlugins[0] = function(wcb){
+ wcb(null, articles);
+ };
+ fn.articlePostPlugins[fn.articlePostPlugins.length] = function(result){
+ cb(result.reverse());
+ };
+ async.waterfall(fn.articlePostPlugins);
+ } else if (files.length === articles.length && fn.articlePostPlugins.length === 1) {
+ cb(articles.reverse());
}
});
View
2 plugins/example.js
@@ -8,7 +8,7 @@
*/
module.exports.load = function(glog) {
- glog.registerArticleHook(function(articles, cb) {
+ glog.registerPostArticleHook(function(articles, cb) {
for(var i=0;i<articles.length;i++) {
articles[i].title = 'Title changed by plugin';
}
View
14 test/glog_tests.js
@@ -55,7 +55,6 @@ describe('Glog', function() {
it('should render the articles', function(done) {
glog.load_configs(function(options) {
glog.load_articles(options, function(articles) {
-
glog.render_blog(options, articles, function() {
assert.ok(glog.pages['/']);
done();
@@ -66,16 +65,19 @@ describe('Glog', function() {
it('should handle plugins', function(done) {
glog.load_configs(function(options) {
- options.plugins = ['example'];
- glog.load_articles(options, function(articles) {
- glog.render_blog(options, articles, function() {
- assert.equal(articles[0].title, 'Title changed by plugin');
- done();
+ options.plugins.push("example");
+ glog.load_plugins(options, function(){
+ glog.load_articles(options, function(articles) {
+ glog.render_blog(options, articles, function() {
+ assert.equal(articles[0].title, 'Title changed by plugin');
+ done();
+ });
});
});
});
});
+
});

0 comments on commit 98a9701

Please sign in to comment.
Something went wrong with that request. Please try again.