Skip to content

Commit

Permalink
Adding support to categories/tags on articles
Browse files Browse the repository at this point in the history
  • Loading branch information
acmarques committed Oct 28, 2010
1 parent cb36f6f commit 80a1bab
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/wheat.js
Expand Up @@ -59,6 +59,7 @@ module.exports = function setup(repo) {
addRoute(/^\/()([a-z0-9_-]+)$/, Renderers.article);
addRoute(/^\/()(.+\.dot)$/, Renderers.dotFile);
addRoute(/^\/()(.+\.[a-z]{2,4})$/, Renderers.staticFile);
addRoute(/^\/()category\/([a-z0-9_-]+)$/, Renderers.categoryIndex);


return function handle(req, res, next) {
Expand Down
35 changes: 35 additions & 0 deletions lib/wheat/data.js
Expand Up @@ -274,6 +274,41 @@ var Data = module.exports = {
);
}),

categories: Git.safe(function articles(version, callback) {
Step(
function getListOfArticles() {
Git.readDir(version, "articles", this);
},
function readArticles(err, results) {
if (err) { callback(err); return; }
var group = this.group();
results.files.forEach(function onFile(filename) {
if (!(/\.markdown$/.test(filename))) {
return;
}
var name = filename.replace(/\.markdown$/, '');
Data.article(version, name, group());
});
},
function processCategories(err, articles) {
if (err) { callback(err); return; }
var categories = articles.reduce(function (start, element) {
var articleCategories = element.categories.split(',').map(function(element){
return element.trim();
});
articleCategories.forEach(function(category){
if(start.indexOf(category) == -1){
start = start.concat(category);
}
});
return start;
}, []);
return categories;
},
callback
)
}),

fullArticles: Git.safe(function fullArticles(version, callback) {
Step(
function getListOfArticles() {
Expand Down
40 changes: 38 additions & 2 deletions lib/wheat/renderers.js
Expand Up @@ -106,12 +106,14 @@ var Renderers = module.exports = {
if (err) { callback(err); return; }
Data.articles(version, this.parallel());
Git.readFile(head, "description.markdown", this.parallel());
Data.categories(version, this.parallel());
},
function applyTemplate(err, articles, description) {
function applyTemplate(err, articles, description, categories) {
if (err) { callback(err); return; }
Tools.render("index", {
articles: articles,
description: description
description: description,
categories: categories
}, this);
},
function callPostProcess(err, buffer) {
Expand Down Expand Up @@ -191,6 +193,40 @@ var Renderers = module.exports = {
);
}),

categoryIndex: Git.safe(function index(version, category, callback) {
Step(
function getHead() {
Git.getHead(this);
},
function loadData(err, head) {
if (err) { callback(err); return; }
Data.articles(version, this.parallel());
Git.readFile(head, "description.markdown", this.parallel());
Data.categories(version, this.parallel());
},
function applyTemplate(err, articles, description, categories) {
if (err) { callback(err); return; }

var articlesForCategory = articles.reduce(function (start, element){
return element.categories.indexOf(category) >= 0 ? start.concat(element) : start;
}, []);

Tools.render("index", {
articles: articlesForCategory,
description: description,
categories: categories
}, this);
},
function callPostProcess(err, buffer) {
if (err) { callback(err); return; }
postProcess({
"Cache-Control": "public, max-age=3600"
}, buffer, version, "index", this);
},
callback
);
}),

staticFile: Git.safe(function staticFile(version, path, callback) {
Step(
function loadPublicFiles() {
Expand Down

0 comments on commit 80a1bab

Please sign in to comment.