Skip to content

Commit

Permalink
added jade and stylus support
Browse files Browse the repository at this point in the history
  • Loading branch information
ianjorgensen committed Sep 10, 2011
1 parent 34edecd commit 9052762
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
.DS_Store
node_modules
82 changes: 46 additions & 36 deletions index.js
@@ -1,12 +1,10 @@
var common = require('common');
var aejs = require('async-ejs');

var mime = require('mime');
var fs = require('fs');
var path = require('path');
var rex = require('rex');
var markdown = require('github-flavored-markdown');
var less = require('less');
var jade = require('jade');
var stylus = require('stylus');

var configs = {};

Expand All @@ -18,17 +16,6 @@ var onfile = function(fn) {
};
};

var aejs = require('async-ejs')
.add('less', onfile(function(file, callback) {
less.render(file, callback);
}))
.add('markdown', onfile(function(file, callback) {
callback(null, markdown.parse(file));
}))
.create('rex', function(options) {
return rex.parser(common.join(options.rex, configs.rex));
});

var onsecure = function(fn) {
return function(request, response) {
if (/(^|\/)\.\.(\/|$)/.test(path.normalize(request.url))) {
Expand All @@ -46,34 +33,37 @@ var normalize = function(locals) {
return locals;
};

exports.renderTemplate = function(template, locals, callback) {
if (!callback) {
callback = locals;
locals = {};
}
aejs.renderFile(template, {locals:normalize(locals)}, callback);
};

exports.template = function(template, locals) {
var get = typeof locals === 'function' ? locals : function(request) {
return common.join(locals, request.params);
};

exports.stylus = function(location) {
return onsecure(function(request, response) {
common.step([
function(next) {
var params = get(request, response, next);

if (params && typeof params === 'object') {
next(null, params); // shortcut
}
fs.readFile(common.format(location, request.params), 'utf-8', next);
},
function(locals, next) {
aejs.renderFile(common.format(template, request.params), {locals:normalize(locals)}, next);
function(src, next) {
stylus.render(src, next);
},
function(src) {
function(str) {
response.writeHead(200, {
'content-type': 'text/html; charset=utf-8',
'content-length': Buffer.byteLength(str)
});
response.end(str);
}
], function(err) {
response.writeHead(500);
response.end(err.stack);
});
});
};
exports.rex = function(location) {
return onsecure(function(request, response) {
common.step([
function(next) {
rex.parse(common.format(location, request.params), next);
},
function(src) {
response.writeHead(200, {
'content-type': 'text/javascript; charset=utf-8',
'content-length': Buffer.byteLength(src)
});
response.end(src);
Expand All @@ -84,6 +74,26 @@ exports.template = function(template, locals) {
});
});
};
exports.jade = function(location, locals) {
return onsecure(function(request, response) {
common.step([
function(next) {
fs.readFile(common.format(location, request.params), 'utf-8', next);
},
function(src) {
var str = jade.compile(src, {self:true})(locals);
response.writeHead(200, {
'content-type': 'text/html; charset=utf-8',
'content-length': Buffer.byteLength(str)
});
response.end(str);
}
], function(err) {
response.writeHead(500);
response.end(err.stack);
});
});
};
exports.file = function(location, options) {
options = options || {};
options.status = options.status || 200;
Expand Down
11 changes: 5 additions & 6 deletions package.json
@@ -1,18 +1,17 @@
{
"name":"bark",
"version":"0.0.6",
"version":"0.1.0",
"description":"A collection of web rendering functions",
"keywords": ["framework", "web", "middleware", "render"],
"author": "Ian Jorgensen <jorgensen.ian@gmail.com>",
"repository": "git://github.com/ianjorgensen/bark.git",
"main":"./render.js",
"dependencies" : {
"async-ejs": ">=0.0.1",
"github-flavored-markdown": ">=1.0.0",
"less": ">=0.0.1",
"rex": ">=0.3.2",
"jade": ">=0.15.4",
"stylus": ">=0.15.2",
"mime": ">=0.0.1",
"common": ">=0.0.1",
"mud": ">=0.0.1"
"common": ">=0.2.0"
},
"devDependencies": {
"router": ">=0.0.1"
Expand Down

0 comments on commit 9052762

Please sign in to comment.