1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-05 09:56:22 +00:00
HackMyResume/dist/helpers/handlebars-helpers.js

98 lines
3.2 KiB
JavaScript
Raw Permalink Normal View History

2016-01-27 10:29:26 +00:00
(function() {
2018-02-12 05:05:29 +00:00
/**
Template helper definitions for Handlebars.
@license MIT. See LICENSE.md for details.
@module handlebars-helpers.js
*/
2018-02-01 02:11:21 +00:00
var HANDLEBARS, HMS, _, blockHelpers, helpers, path;
2016-01-27 10:29:26 +00:00
HANDLEBARS = require('handlebars');
_ = require('underscore');
helpers = require('./generic-helpers');
2018-02-01 02:11:21 +00:00
path = require('path');
blockHelpers = require('./block-helpers');
2018-02-01 02:11:21 +00:00
HMS = require('../core/status-codes');
2016-01-27 10:29:26 +00:00
/**
Register useful Handlebars helpers.
@method registerHelpers
2018-02-12 05:05:29 +00:00
*/
2018-02-07 10:49:02 +00:00
module.exports = function(theme, rez, opts) {
var curGlob, ex, glob, slash, wrappedHelpers;
2016-01-27 10:29:26 +00:00
helpers.theme = theme;
helpers.opts = opts;
helpers.type = 'handlebars';
2018-02-12 05:05:29 +00:00
// Prepare generic helpers for use with Handlebars. We do this by wrapping them
// in a Handlebars-aware wrapper which calls the helper internally.
wrappedHelpers = _.mapObject(helpers, function(hVal, hKey) {
if (_.isFunction(hVal)) {
return _.wrap(hVal, function(func) {
var args;
args = Array.prototype.slice.call(arguments);
2018-02-12 05:05:29 +00:00
args.shift(); // lose the 1st element (func) [^1]
//args.pop() # lose the last element (HB options hash)
args[args.length - 1] = rez; // replace w/ resume object
return func.apply(this, args); // call the generic helper
});
}
return hVal;
}, this);
HANDLEBARS.registerHelper(wrappedHelpers);
2018-02-12 05:05:29 +00:00
// Prepare Handlebars-specific helpers - "blockHelpers" is really a misnomer
// since any kind of Handlebars-specific helper can live here
HANDLEBARS.registerHelper(blockHelpers);
2018-02-01 02:11:21 +00:00
if (_.isString(theme.helpers)) {
2018-02-12 05:05:29 +00:00
// Register any theme-provided custom helpers...
// Normalize "theme.helpers" (string or array) to an array
2018-02-01 02:11:21 +00:00
theme.helpers = [theme.helpers];
}
if (_.isArray(theme.helpers)) {
glob = require('glob');
slash = require('slash');
2018-02-01 02:11:21 +00:00
curGlob = null;
try {
2018-02-12 05:05:29 +00:00
_.each(theme.helpers, function(fGlob) { // foreach theme.helpers entry
var files;
2018-02-12 05:05:29 +00:00
curGlob = fGlob; // ..cache in case of exception
fGlob = path.join(theme.folder, fGlob); // ..make relative to theme
files = glob.sync(slash(fGlob)); // ..expand the glob
if (files.length > 0) { // ..guard against empty glob
_.each(files, function(f) { // ..loop over concrete paths
HANDLEBARS.registerHelper(require(f)); // ..register the path
});
} else {
throw {
fluenterror: HMS.themeHelperLoad,
inner: er,
glob: fGlob
};
}
2018-02-01 02:11:21 +00:00
});
2018-02-12 05:05:29 +00:00
} catch (error) {
ex = error;
2018-02-01 02:11:21 +00:00
throw {
fluenterror: HMS.themeHelperLoad,
inner: ex,
glob: curGlob,
exit: true
};
}
}
2016-01-27 10:29:26 +00:00
};
2018-02-12 05:05:29 +00:00
// [^1]: This little bit of acrobatics ensures that our generic helpers are
// called as generic helpers, not as Handlebars-specific helpers. This allows
// them to be used in other templating engines, like Underscore. If you need a
// Handlebars-specific helper with normal Handlebars context and options, put it
// in block-helpers.coffee.
2016-01-27 10:29:26 +00:00
}).call(this);
2016-02-02 02:14:36 +00:00
//# sourceMappingURL=handlebars-helpers.js.map