-
-
Save carlos8f/285c2a59b6cae3a42e61 to your computer and use it in GitHub Desktop.
downer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node_modules | |
example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Templ = require('templ').Templ | |
, fm = require('front-matter') | |
, marked = require('marked') | |
, linkify = require('gfm-linkify') | |
, inherits = require('util').inherits | |
function Downer (specs, options) { | |
if (specs.constructor === Object) { | |
options = specs; | |
specs = null; | |
} | |
options || (options = {}); | |
if (typeof options.sanitize === 'undefined') options.sanitize = false; | |
if (typeof options.smartypants === 'undefined') options.smartypants = true; | |
Templ.call(this, specs, options); | |
} | |
inherits(Downer, Templ); | |
Downer.prototype.compile = function (file) { | |
if (file.name.match(/\.(markdown|md)$/i)) { | |
var self = this, ctx = {}; | |
var data = file.data({encoding: 'utf8'}); | |
var content = fm(data); | |
Object.keys(this.options).forEach(function (k) { | |
ctx[k] = self.options[k]; | |
}); | |
Object.keys(content.attributes).forEach(function (k) { | |
ctx[k] = content.attributes[k]; | |
}); | |
var linked = linkify(content.body, ctx.repository || ctx.repo); | |
marked.setOptions(ctx); | |
var markedDown = marked(linked); | |
var template = this.handlebars.compile(markedDown); | |
template.ctx = ctx; | |
return template; | |
} | |
else if (file.name.match(/\.(handlebars|hbs)$/i)) { | |
return Templ.prototype.compile.call(this, file); | |
} | |
}; | |
Downer.prototype.middleware = function (options) { | |
var self = this; | |
var mw = Templ.prototype.middleware.call(this, options); | |
return function (req, res, next) { | |
function render () { | |
var p = req.url.replace(/^\//, ''); | |
var file = self.getPlugin(p); | |
if (typeof file === 'undefined' || file.name.match(/\.(handlebars|hbs)$/i)) { | |
return next(); | |
} | |
var template = file.plugin; | |
var ctx = {}; | |
Object.keys(template.ctx || {}).forEach(function (k) { | |
ctx[k] = template.ctx[k]; | |
}); | |
Object.keys(res.vars || {}).forEach(function (k) { | |
ctx[k] = res.vars[k]; | |
}); | |
res.render(p, ctx, ctx); | |
} | |
mw(req, res); | |
if (self.ready) render(); | |
else self.once('ready', render); | |
}; | |
}; | |
module.exports = function (root, options) { | |
return new Downer([{globs: ['**/*.md', '**/*.markdown', '**/*.hbs', '**/*.handlebars'], cwd: root}], options).middleware(options); | |
}; | |
module.exports.Downer = Downer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "downer", | |
"version": "0.0.2", | |
"description": "a hybrid markdown/handlebars rendering middleware", | |
"main": "index.js", | |
"scripts": { | |
"test": "tar -xf example.tar.gz && ./node_modules/.bin/mocha --reporter spec --timeout 10s --bail" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "git@gist.github.com:/285c2a59b6cae3a42e61.git" | |
}, | |
"dependencies": { | |
"front-matter": "^0.2.0", | |
"templ": "^1.4.2", | |
"gfm-linkify": "^0.1.0", | |
"marked": "^0.3.2" | |
}, | |
"devDependencies": { | |
"handlebars": "^2.0.0-alpha.4", | |
"idgen": "^2.0.2", | |
"middler": "^0.7.1", | |
"mocha": "^1.20.1", | |
"ncp": "^0.5.1", | |
"request": "^2.36.0", | |
"rimraf": "^2.2.8" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
assert = require('assert'); | |
util = require('util'); | |
templ = require('./'); | |
spawn = require('child_process').spawn; | |
path = require('path'); | |
request = require('request'); | |
idgen = require('idgen'); | |
rimraf = require('rimraf'); | |
ncp = require('ncp').ncp; | |
fs = require('fs'); | |
describe('basic test', function () { | |
var server, port, root; | |
before(function (done) { | |
root = '/tmp/templ-test-' + idgen(); | |
ncp(path.join(__dirname, 'example', 'views'), root, done); | |
}); | |
after(function (done) { | |
rimraf(root, done); | |
}); | |
before(function (done) { | |
server = spawn('node', [path.resolve(__dirname, 'example', 'middleware.js'), root, '0']); | |
process.once('exit', function () { | |
server.kill(); | |
}); | |
server.stdout.on('data', function (data) { | |
data = String(data); | |
var match = data.match(/localhost:(.*)\//); | |
assert(match); | |
port = Number(match[1]); | |
assert(port); | |
done(); | |
}); | |
server.stderr.pipe(process.stderr); | |
}); | |
it('hits example', function (done) { | |
request('http://localhost:' + port + '/', function (err, resp, body) { | |
assert.ifError(err); | |
assert(body.match(/<title>templ example<\/title>/)); | |
assert(body.match(/<h1>templ example<\/h1>/)); | |
assert(body.match(/your number is: \d\.\d+/)); | |
assert(body.match(/<div class="hidden">this is a partial<\/div>/)); | |
done(); | |
}); | |
}); | |
it('hits hidden page', function (done) { | |
request('http://localhost:' + port + '/hidden', function (err, resp, body) { | |
assert.ifError(err); | |
assert(body.match(/<title><\/title>/)); | |
assert(body.match(/<h1><\/h1>/)); | |
assert(body.match(/I am a hidden page!/)); | |
done(); | |
}); | |
}); | |
it('serves xml file', function (done) { | |
request('http://localhost:' + port + '/feed.xml', function (err, resp, body) { | |
assert.ifError(err); | |
assert.equal(resp.statusCode, 200); | |
assert.equal(resp.headers['content-type'], 'text/xml'); | |
assert(body.trim().match(/^<xml>this is a fake feed<\/xml>$/)); | |
done(); | |
}); | |
}); | |
it('update template', function (done) { | |
setTimeout(function () { | |
fs.writeFile(path.join(root, 'subdir', 'partial.hbs'), 'fo shizzle!', function (err) { | |
assert.ifError(err); | |
setTimeout(done, 1000); | |
}); | |
}, 1000); | |
}); | |
it('serves updated template', function (done) { | |
request('http://localhost:' + port + '/', function (err, resp, body) { | |
assert.ifError(err); | |
assert(body.match(/<div class="hidden">fo shizzle!<\/div>/)); | |
done(); | |
}); | |
}); | |
it('serves raw template', function (done) { | |
request('http://localhost:' + port + '/hidden/raw', function (err, resp, body) { | |
assert.ifError(err); | |
assert(body.trim().match(/^I am a hidden page!$/)); | |
done(); | |
}); | |
}); | |
it('precompiled layout override', function (done) { | |
request('http://localhost:' + port + '/layout-override', function (err, resp, body) { | |
assert.ifError(err); | |
assert(body.trim().match(/^<cool>Hi, my name is carlos.<\/cool>$/)); | |
done(); | |
}); | |
}); | |
it('serves 404', function (done) { | |
request('http://localhost:' + port + '/pony', function (err, resp, body) { | |
assert.ifError(err); | |
assert.equal(resp.statusCode, 404); | |
assert(body.match(/<title>error 404<\/title>/)); | |
assert(body.match(/<h1>error 404<\/h1>/)); | |
assert(body.match(/page not found!/)); | |
done(); | |
}); | |
}); | |
it('serves 403', function (done) { | |
request('http://localhost:' + port + '/admin', function (err, resp, body) { | |
assert.ifError(err); | |
assert.equal(resp.statusCode, 403); | |
assert.equal(body, ''); | |
done(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment