Skip to content

Commit 45ad737

Browse files
committed
Get blog data from a separate reusable module with a new waitForIt.
1 parent 01c4c66 commit 45ad737

File tree

4 files changed

+122
-38
lines changed

4 files changed

+122
-38
lines changed

.jshintrc

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"node": true,
33
"browser": true,
44
"eqnull": true,
5+
"boss": true,
56
"predef": [
67
"describe",
78
"it"

blog/data.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
var fs = require('fs');
3+
var path = require('path');
4+
var async = require('async');
5+
var waitForIt = require('../lib/waitForIt');
6+
var pkg = require('../package.json');
7+
8+
var dir = path.join(__dirname, '../', pkg._buildOutput, 'blog/posts');
9+
10+
function loadJSONPosts(callback){
11+
12+
var JSONPath = dir + '/posts.json';
13+
14+
try {
15+
callback(null, require(JSONPath));
16+
} catch (e){
17+
callback(new Error(JSONPath + " doesn not exist. \n" +
18+
"Did you build the blog with 'node build/blog'?"));
19+
}
20+
21+
}
22+
23+
function loadPost(post, callback){
24+
fs.readFile(dir + '/' + post.htmlFile, function(err, str){
25+
if (err) return callback(err);
26+
post.content = str;
27+
callback(null, post);
28+
});
29+
}
30+
31+
function loadContent(posts, callback){
32+
async.map(posts, loadPost, callback);
33+
}
34+
35+
function index(posts, callback){
36+
37+
var total = {
38+
posts: posts,
39+
urls: {},
40+
tags: {}
41+
};
42+
43+
posts.forEach(function(post, i){
44+
post.date = new Date(post.date);
45+
total.urls[post.permalink] = i;
46+
if (post.tags && Array.isArray(post.tags)) post.tags.forEach(function(tag){
47+
tag = tag.toLowerCase();
48+
(total.tags[tag] || (total.tags[tag] = [])).push(i);
49+
});
50+
});
51+
52+
callback(null, total);
53+
54+
}
55+
56+
var load = async.compose(index, loadContent, loadJSONPosts);
57+
58+
module.exports = waitForIt(load);

blog/index.js

+19-38
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,24 @@
11
"use strict";
22

3-
var fs = require('fs');
4-
var path = require('path');
5-
var pkg = require('../package.json');
3+
var data = require('./data');
64

7-
var dir = path.join(__dirname, '../', pkg._buildOutput, 'blog/posts');
8-
9-
var posts;
10-
var JSONPath = dir + '/posts.json';
11-
12-
try {
13-
posts = require(JSONPath);
14-
} catch (e){
15-
console.error(JSONPath + " doesn not exist. \n" +
16-
"Did you build the blog with 'node build/blog'?");
17-
throw e;
18-
}
19-
20-
var content = posts.map(function(post){
21-
return fs.readFileSync(dir + '/' + post.htmlFile);
22-
});
23-
24-
var postsByURL = {};
25-
var tags = {};
5+
var perPage = 2;
266

27-
posts.forEach(function(post, i){
28-
post.date = new Date(post.date);
29-
postsByURL[post.permalink] = i;
30-
if (post.tags && Array.isArray(post.tags)) post.tags.forEach(function(tag){
31-
tag = tag.toLowerCase();
32-
(tags[tag] || (tags[tag] = [])).push(i);
7+
function getData(req, res, next){
8+
data.get(function(err, posts){
9+
if (err) return next(err);
10+
res.locals._posts = posts;
11+
next();
3312
});
34-
});
35-
36-
var perPage = 2;
13+
}
3714

3815
module.exports = function(app){
3916

4017
var index = function(req, res, next){
4118
var page = req.params.page ? parseInt(req.params.page, 10) : 1;
4219
var tag = req.params.tag;
20+
var posts = res.locals._posts.posts;
21+
var tags = res.locals._posts.tags;
4322

4423
// posts with this tag do not exist
4524
if (tag && !tags[tag]) return next();
@@ -68,22 +47,24 @@ module.exports = function(app){
6847

6948
};
7049

71-
app.get('/blog', index);
72-
app.get('/blog/page/:page', index);
73-
app.get('/blog/category/:tag', index);
74-
app.get('/blog/category/:tag/page/:page', index);
50+
app.get('/blog', getData, index);
51+
app.get('/blog/page/:page', getData, index);
52+
app.get('/blog/category/:tag', getData, index);
53+
app.get('/blog/category/:tag/page/:page', getData, index);
7554

76-
app.get(/\/blog\/(.+)/, function(req, res, next){
55+
app.get(/\/blog\/(.+)/, getData, function(req, res, next){
7756
var url = req.params[0];
57+
var posts = res.locals._posts.posts;
58+
var urls = res.locals._posts.urls;
7859

79-
var postIndex = postsByURL[url];
60+
var postIndex = urls[url];
8061
var post = postIndex != null && posts[postIndex];
8162
if (!post) return next();
8263

8364
res.render('blog/post', {
8465
title: "MooTools Blog: " + post.title,
8566
post: post,
86-
content: content[postIndex],
67+
content: post.content,
8768
page: 'blog'
8869
});
8970

lib/waitForIt.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"use strict";
2+
3+
module.exports = function(fn){
4+
5+
var result, isRunning = false, callbacks = [];
6+
7+
var done = function(err, res){
8+
var cbs = callbacks.slice(), cb;
9+
while (cb = cbs.shift()) cb(err, res);
10+
};
11+
12+
return {
13+
14+
get: function(callback){
15+
16+
if (result){
17+
callback(null, result);
18+
return;
19+
}
20+
21+
callbacks.push(callback);
22+
23+
if (isRunning){
24+
return;
25+
}
26+
27+
isRunning = true;
28+
29+
fn(function(err, res){
30+
if (!err) result = res;
31+
isRunning = false;
32+
done(err, res);
33+
});
34+
35+
},
36+
37+
reset: function(){
38+
result = null;
39+
callbacks = [];
40+
}
41+
42+
};
43+
44+
};

0 commit comments

Comments
 (0)