Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding support to categories/tags on articles

  • Loading branch information...
commit 80a1bab6c04055d8b78648cb252c9bd56546052c 1 parent cb36f6f
@acmarques acmarques authored
Showing with 74 additions and 2 deletions.
  1. +1 −0  lib/wheat.js
  2. +35 −0 lib/wheat/data.js
  3. +38 −2 lib/wheat/renderers.js
View
1  lib/wheat.js
@@ -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
35 lib/wheat/data.js
@@ -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() {
View
40 lib/wheat/renderers.js
@@ -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() {
Please sign in to comment.
Something went wrong with that request. Please try again.