raw
Version:
Plain HTML templating with connect assets and env support
80 lines (69 loc) • 1.96 kB
JavaScript
// Generated by CoffeeScript 1.4.0
var Instance, css_replace, env_block, fs, js_replace, middleware, path, sub;
fs = require('fs');
path = require('path');
Instance = function() {
this.cache = {};
this.__express = middleware.bind(this);
};
middleware = function(filename, options, cb) {
var cache, render_file;
cache = this.cache;
render_file = function(locals, cb) {
var template;
template = cache[filename];
if (template != null) {
return cb(null, template);
}
return fs.readFile(filename, 'utf8', function(err, str) {
var final_str;
if (err != null) {
return cb(err);
}
final_str = env_block(str, options.settings.env);
final_str = js_replace(final_str);
final_str = css_replace(final_str);
if (options.cache) {
cache[filename] = final_str;
}
return cb(null, final_str);
});
};
return render_file(options, cb);
};
js_replace = function(str) {
return sub(str, /{{{js\s(.*)}}}/g, 'js');
};
css_replace = function(str) {
return sub(str, /{{{css\s(.*)}}}/g, 'css');
};
env_block = function(str, env) {
var final_str, matches, patt;
patt = /\{env\s(.*)\}((?:.|\n)*)?\{\/env\}/;
final_str = str;
matches = str.match(patt);
if (matches != null) {
if (env === matches[1]) {
final_str = final_str.replace(matches[0], matches[2]);
} else {
final_str = final_str.replace(matches[0], '');
}
}
return final_str;
};
sub = function(str, patt, type) {
var final_str, matched, matches, _i, _len;
final_str = str;
matches = str.match(patt);
if (matches != null) {
for (_i = 0, _len = matches.length; _i < _len; _i++) {
matched = matches[_i];
final_str = final_str.replace(matched, eval(type)(matched.split("{{{" + type + " ")[1].split('}}}')[0].trim()));
}
}
return final_str;
};
module.exports = new Instance();
module.exports.create = function() {
return new Instance();
};