Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Adding categories/tags support to wheat #13

Merged
3 commits merged into from

4 participants

@acmarques

Hi Tim,

I've been looking into the sources of howtonode.org and there I found wheat. I loved the idea of a blog engine entirely based on gitfs and markdown and, so, I decided to try it out and build my own personal blog.

As a result, I've added support of categories/tags into wheat. To work, it is only necessary to add the article's categories in the header of the markdown file, as comma-separated values. You can see an example here:
http://github.com/acmarques/randomthoughts/raw/master/articles/deploying_node_with_nginx_and_god.markdown
There is also a new route, '/category/#{value}', used to index articles in the selected category.

You can see it working here, in my personal blog written using wheat: http://blog.acmarques.com .

Lastly, I would like to thank you by the many contributions you are doing in node.js, I'm learning a lot reading your code and howtonode.org!

Hope my changes are useful. Any doubts or problems, please let me know.

Tks,

Antonio

@TooTallNate

+1!!!!

I was thinking of implementing this someday, but this looks like exactly the way I would have done it. Great job!

@creationix
Owner

Awesome, this was on my todo list.

@mchelen

How do you add the "Categories" block?

@acmarques

mchelen,

Take a look at this example:
https://github.com/acmarques/randomthoughts/raw/master/articles/deploying_node_with_nginx_and_god.markdown

It's a post from my personal blog that uses wheat with the categories block.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 28, 2010
  1. @acmarques
Commits on Oct 29, 2010
  1. @acmarques
Commits on Oct 30, 2010
  1. @acmarques
This page is out of date. Refresh to see the latest.
Showing with 79 additions and 3 deletions.
  1. +1 −0  lib/wheat.js
  2. +40 −1 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
41 lib/wheat/data.js
@@ -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
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() {
Something went wrong with that request. Please try again.