Skip to content

Commit

Permalink
Added jade support
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Sep 10, 2011
1 parent 5efc89f commit 6622127
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
40 changes: 34 additions & 6 deletions bin/serve
Expand Up @@ -5,9 +5,13 @@
*/

var resolve = require('path').resolve
, join = require('path').join
, program = require('commander')
, connect = require('connect')
, stylus = require('stylus')
, connect = require('connect');
, jade = require('jade')
, url = require('url')
, fs = require('fs');

// CLI

Expand All @@ -17,15 +21,17 @@ program
.option('-F, --format <fmt>', 'specify the log format string', ':method :url - :response-time ms')
.option('-p, --port <port>', 'specify the port [3000]', Number, 3000)
.option('-H, --hidden', 'enable hidden file serving')
.option('-S, --no-stylus', 'disable stylus rendering')
.option('-J, --no-jade', 'disable jade rendering')
.option('-I, --no-icons', 'disable icons')
.option('-L, --no-logs', 'disable request logging')
.option('-D, --no-dirs', 'disable directory serving')
.parse(process.argv);

// path
var path = resolve(program.args.shift() || '.');
// setup the server

// setup the server
var server = connect();

// ignore favicon
Expand All @@ -35,10 +41,32 @@ server.use(connect.favicon());
if (program.logs) server.use(connect.logger(program.format));

// convert .styl to .css to trick stylus.middleware
server.use(function(req, res, next){
req.url = req.url.replace(/\.styl$/, '.css');
next();
});
if (program.stylus) {
server.use(function(req, res, next){
req.url = req.url.replace(/\.styl$/, '.css');
next();
});
}

// jade
if (program.jade) {
server.use(function(req, res, next){
if (!req.url.match(/\.jade$/)) return next();
var file = join(path, url.parse(req.url).pathname);
fs.readFile(file, 'utf8', function(err, str){
if (err) return next(err);
try {
var fn = jade.compile(str, { filename: file });
str = fn();
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Length', str.length);
res.end(str);
} catch (err) {
next(err);
}
});
});
}

// stylus
server.use(stylus.middleware({ src: path }));
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -7,6 +7,7 @@
, "dependencies": {
"connect": "1.7.x"
, "stylus": "0.15.x"
, "jade": "0.15.x"
, "commander": "0.0.4"
}
, "bin": { "serve": "./bin/serve" }
Expand Down

0 comments on commit 6622127

Please sign in to comment.