2016-07-30 17:18:38 +02:00
|
|
|
var render = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
var fs = require('fs');
|
2017-01-08 09:36:18 +01:00
|
|
|
var _ = require('node_modules/underscore/underscore.js');
|
2016-07-30 17:18:38 +02:00
|
|
|
var Handlebars = require('handlebars');
|
|
|
|
|
|
|
|
function template_dir() {
|
|
|
|
return __dirname + '/../../static/templates/';
|
|
|
|
}
|
|
|
|
|
2016-08-25 03:58:13 +02:00
|
|
|
exports.init = function () {
|
|
|
|
Handlebars.templates = {};
|
|
|
|
};
|
|
|
|
|
2016-11-04 16:34:27 +01:00
|
|
|
exports.render_template = function (name, args) {
|
|
|
|
exports.compile_template(name);
|
|
|
|
return global.templates.render(name, args);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.compile_template = function (name) {
|
|
|
|
var included_fns = exports.find_included_partials(name);
|
|
|
|
|
|
|
|
_.each(included_fns, function (fn) {
|
|
|
|
exports.compile_template(fn);
|
|
|
|
});
|
|
|
|
|
2016-07-30 17:18:38 +02:00
|
|
|
if (Handlebars.templates === undefined) {
|
|
|
|
Handlebars.templates = {};
|
|
|
|
}
|
2016-10-05 02:03:00 +02:00
|
|
|
|
2016-11-04 16:14:48 +01:00
|
|
|
if (_.has(Handlebars.template, name)) {
|
|
|
|
// we already compile the template
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-04 19:39:58 +01:00
|
|
|
var file = exports.template_finder.get(name);
|
|
|
|
var data = fs.readFileSync(file.url).toString();
|
2016-07-30 17:18:38 +02:00
|
|
|
Handlebars.templates[name] = Handlebars.compile(data);
|
|
|
|
};
|
|
|
|
|
2016-11-02 06:45:06 +01:00
|
|
|
// list all files in a directory and it's subdirectories in a recursive sync way.
|
|
|
|
exports.walk = function (dir, filelist) {
|
|
|
|
filelist = filelist || [];
|
|
|
|
|
|
|
|
// grab files one level deep.
|
|
|
|
var files = fs.readdirSync(dir);
|
|
|
|
|
|
|
|
// for each file, check if it's a directory. If so, continue recursion.
|
|
|
|
// if not add to the file list.
|
|
|
|
files.forEach(function (file) {
|
|
|
|
if (fs.statSync(dir + "/" + file).isDirectory()) {
|
|
|
|
filelist = exports.walk(dir + "/" + file, filelist);
|
|
|
|
} else {
|
|
|
|
filelist.push({
|
|
|
|
url: dir + "/" + file,
|
2016-12-03 23:18:44 +01:00
|
|
|
name: file,
|
2016-11-02 06:45:06 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// return all recursively found files.
|
|
|
|
return filelist;
|
|
|
|
};
|
|
|
|
|
2016-11-04 15:51:28 +01:00
|
|
|
exports.template_finder = (function () {
|
|
|
|
// This class lets you find template files in our file system.
|
|
|
|
// It may be slightly overkill for our flat directory system;
|
|
|
|
// it might make more sense to just do something more like
|
|
|
|
// this: get_template_dir() + name + '.handlebars'
|
|
|
|
|
|
|
|
var self = {};
|
2016-11-02 06:45:06 +01:00
|
|
|
|
|
|
|
// get all files and then map them into friendlier names.
|
2016-11-04 19:56:00 +01:00
|
|
|
var all_files = exports.walk(template_dir());
|
|
|
|
var files = all_files
|
2018-05-07 01:50:14 +02:00
|
|
|
.filter(function (file) {
|
|
|
|
return (/\.handlebars$/).test(file.name);
|
|
|
|
})
|
|
|
|
.map(function (file) {
|
|
|
|
return {
|
|
|
|
url: file.url,
|
|
|
|
name: file.name.replace(/\.handlebars$/, ""),
|
|
|
|
};
|
|
|
|
});
|
2016-11-02 06:45:06 +01:00
|
|
|
|
2016-11-04 15:51:28 +01:00
|
|
|
self.get = function (name) {
|
|
|
|
var file = files.find(function (file) {
|
|
|
|
return file.name === name;
|
|
|
|
});
|
|
|
|
assert(file);
|
|
|
|
|
|
|
|
return file;
|
|
|
|
};
|
|
|
|
|
2016-11-04 19:56:00 +01:00
|
|
|
self.get_all = function () {
|
|
|
|
return files;
|
|
|
|
};
|
|
|
|
|
2016-11-04 15:51:28 +01:00
|
|
|
return self;
|
|
|
|
}());
|
|
|
|
|
2016-11-04 16:07:00 +01:00
|
|
|
exports.find_included_partials = function (name) {
|
|
|
|
|
|
|
|
var file = exports.template_finder.get(name);
|
|
|
|
|
|
|
|
assert(file);
|
|
|
|
|
|
|
|
var template = fs.readFileSync(file.url, "utf8");
|
|
|
|
|
|
|
|
var lst = [];
|
|
|
|
|
|
|
|
// match partial tags.
|
|
|
|
// this uses String.prototype.replace which is kind of hacky but
|
|
|
|
// it is the only JS function IIRC that allows you to match all
|
|
|
|
// instances of a pattern AND return capture groups.
|
|
|
|
template.replace(/\{\{\s*partial\s*"(.+?)"/ig, function (match, $1) {
|
|
|
|
lst.push($1);
|
|
|
|
});
|
|
|
|
|
|
|
|
return lst;
|
|
|
|
};
|
|
|
|
|
2016-07-30 17:18:38 +02:00
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
module.exports = render;
|