2013-12-12 20:48:23 +00:00
|
|
|
/**
|
|
|
|
* web/jade.js - Provides functionality for rendering/serving jade templates
|
|
|
|
*
|
|
|
|
* @author Calvin Montgomery <cyzon@cyzon.us>
|
|
|
|
*/
|
|
|
|
|
|
|
|
var jade = require('jade');
|
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
var templates = path.join(__dirname, '..', '..', 'templates');
|
|
|
|
|
|
|
|
var cache = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merges locals with globals for jade rendering
|
|
|
|
*
|
|
|
|
* @param {Object} locals - The locals to merge
|
|
|
|
* @return {Object} an object containing globals and locals
|
|
|
|
*/
|
|
|
|
function merge(locals) {
|
|
|
|
var _locals = {
|
|
|
|
siteTitle: 'CyTube Beta',
|
|
|
|
siteDescription: 'Free, open source synchtube',
|
|
|
|
siteAuthor: 'Calvin "calzoneman" "cyzon" Montgomery'
|
|
|
|
};
|
|
|
|
if (typeof locals !== 'object') {
|
|
|
|
return _locals;
|
|
|
|
}
|
|
|
|
for (var key in locals) {
|
|
|
|
_locals[key] = locals[key];
|
|
|
|
}
|
|
|
|
return _locals;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders and serves a jade template
|
|
|
|
*
|
|
|
|
* @param res - The HTTP response
|
|
|
|
* @param view - The view to render
|
|
|
|
* @param locals - The locals to pass to the renderer
|
|
|
|
*/
|
|
|
|
function sendJade(res, view, locals) {
|
|
|
|
if (!(view in cache) || process.env['DEBUG']) {
|
|
|
|
var file = path.join(templates, view + '.jade');
|
|
|
|
var fn = jade.compile(fs.readFileSync(file), {
|
2013-12-25 21:18:21 +00:00
|
|
|
filename: file,
|
|
|
|
pretty: true
|
2013-12-12 20:48:23 +00:00
|
|
|
});
|
|
|
|
cache[view] = fn;
|
|
|
|
}
|
2014-01-09 23:43:07 +00:00
|
|
|
var start = Date.now();
|
2013-12-12 20:48:23 +00:00
|
|
|
var html = cache[view](merge(locals));
|
2014-01-09 23:43:07 +00:00
|
|
|
var end = Date.now();
|
|
|
|
console.log(view + " " + (end - start) + "ms");
|
2013-12-12 20:48:23 +00:00
|
|
|
res.send(html);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
sendJade: sendJade
|
|
|
|
};
|