Adding categories/tags support to wheat #13

Merged
3 commits merged into from Nov 2, 2010
View
@@ -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) {
View
@@ -2,7 +2,8 @@ var Git = require('git-fs'),
Path = require('path'),
Step = require('step'),
sys = require('sys'),
- Script = process.binding('evals').Script;
+ Script = process.binding('evals').Script,
+ QueryString = require('querystring');
function preProcessMarkdown(markdown) {
if (!(typeof markdown === 'string')) {
@@ -205,6 +206,12 @@ var Data = module.exports = {
props.version = version;
}
props.author = author;
+
+ if(props.categories != undefined){
+ props.categories = props.categories.split(',').map(function(element){
+ return QueryString.escape(element.trim());
+ });
+ }
return props;
},
callback
@@ -274,6 +281,38 @@ 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) {
+ element.categories.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() {
View
@@ -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) {
@@ -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() {