Skip to content
Browse files

Pre plugins

Run this set of plugins before markdown is applied.
  • Loading branch information...
1 parent ec7edff commit 2f4d6b5b521d49d8e94ceffbe137e462523d6445 @wlaurance wlaurance committed Jun 20, 2012
Showing with 58 additions and 11 deletions.
  1. +51 −10 lib/glog.js
  2. +6 −1 plugins/example.js
  3. +1 −0 test/glog_tests.js
View
61 lib/glog.js
@@ -150,7 +150,7 @@ Glog.prototype.load_articles = function(options, cb) {
var parts = data.split('\n\n');
var header = JSON.parse(parts[0]);
parts.splice(0,1);
- var body = marked(parts.join('\n\n'));
+ var body = parts.join('\n\n');
console.log('Read articles with headers: ');
console.log(header);
@@ -188,15 +188,56 @@ Glog.prototype.load_articles = function(options, cb) {
'body' : body,
'url' : [year, month, (header.url || header.title.replace(/\s/g, '-'))].join('/')
});
- 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) {
+ if (files.length === articles.length && (fn.articlePostPlugins.length > 1 || fn.articlePrePlugins.length > 1)){
+ var plugins = [];
+ var postlength = Number(fn.articlePostPlugins.length);
+ var prelength = Number(fn.articlePrePlugins.length);
+ if(fn.articlePrePlugins.length > 1){
+ console.log('preplugins');
+ fn.articlePrePlugins[0] = function(wcb){
+ wcb(null, articles);
+ }
+ fn.articlePrePlugins[fn.articlePrePlugins.length] = function(result, wcb){
+ if(postlength > 1){
+ console.info('moving on to post plugins');
+ wcb(null, result);
+ } else {
+ for(var k in result){
+ result[k].body = marked(result[k].body);
+ }
+ cb(result.reverse());
+ }
+ }
+ for(var p in fn.articlePrePlugins){
+ plugins.push(fn.articlePrePlugins[p]);
+ }
+ }
+ if(fn.articlePostPlugins.length > 1){
+ if(prelength > 1){
+ fn.articlePostPlugins[0] = function(result, wcb){
+ for(var k in result){
+ result[k].body = marked(result[k].body);
+ }
+ wcb(null, result);
+ }
+ } else {
+ fn.articlePostPlugins[0] = function(wcb){
+ wcb(null, articles);
+ };
+ }
+ fn.articlePostPlugins[fn.articlePostPlugins.length] = function(result){
+ cb(result.reverse());
+ };
+ }
+ for(var f in fn.articlePostPlugins){
+ plugins.push(fn.articlePostPlugins[f]);
+ }
+ async.waterfall(plugins);
+ } else if (files.length === articles.length && fn.articlePostPlugins.length === 1 && fn.articlePrePlugins.length === 1) {
+ for(var g in articles){
+ articles[g].body = marked(articles[g].body);
+ }
+
cb(articles.reverse());
}
View
7 plugins/example.js
@@ -14,5 +14,10 @@ module.exports.load = function(glog) {
}
cb(null, articles);
});
-
+ glog.registerPreArticleHook(function(articles, cb) {
+ for(var i=0;i<articles.length;i++){
+ articles[i].year = '1988'
+ }
+ cb(null, articles);
+ });
}
View
1 test/glog_tests.js
@@ -75,6 +75,7 @@ describe('Glog', function() {
glog.load_articles(options, function(articles) {
glog.render_blog(options, articles, function() {
assert.equal(articles[0].title, 'Title changed by plugin');
+ assert.equal(articles[0].year, '1988');
done();
});
});

0 comments on commit 2f4d6b5

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