@balena/doxx-handlebars-helper
Version:
Handlebars helper for smart partials import.
124 lines (107 loc) • 3.7 kB
JavaScript
// Generated by CoffeeScript 1.12.7
(function() {
var Handlebars, _, compileTemplate, createImportHelper, getBestPartial, getFullKey, getPartial, importDefaults, parseJsonPartial, registerHelper, stringifyPairs;
_ = require('lodash');
Handlebars = require('handlebars');
stringifyPairs = require('./util').stringifyPairs;
exports.Handlebars = Handlebars;
exports.compileTemplate = compileTemplate = _.memoize(function(tpl) {
return Handlebars.compile(tpl);
});
exports.render = function(template, context) {
var compiled;
if (!template) {
return '';
}
compiled = compileTemplate(template);
return compiled(context);
};
parseJsonPartial = _.memoize(function(key) {
var e, partial;
partial = Handlebars.partials[key];
if (partial == null) {
return;
}
try {
return JSON.parse(partial);
} catch (error1) {
e = error1;
throw new Error("Error parsing JSON partial " + key + ": " + e);
}
});
getPartial = function(hash, key) {
var partial;
partial = hash[key];
if (partial == null) {
return;
}
return compileTemplate(partial);
};
exports.getPartial = function(key) {
return getPartial(Handlebars.partials, key);
};
getFullKey = function(prefix, option, sep) {
return "" + prefix + sep + option;
};
exports.getBestPartial = getBestPartial = _.memoize(function(prefix, options, sep) {
var combinedPrefix, getKey, hash, i, len, option, partial;
if (sep == null) {
sep = '/';
}
hash = Handlebars.partials;
getKey = getFullKey;
combinedPrefix = prefix + ".json";
if (hash[combinedPrefix] != null) {
hash = parseJsonPartial(combinedPrefix);
getKey = function(prefix, option, sep) {
return option;
};
}
for (i = 0, len = options.length; i < len; i++) {
option = options[i];
partial = getPartial(hash, getKey(prefix, option, sep));
if (partial) {
return partial;
}
}
}, getFullKey);
importDefaults = {
importName: 'import',
beforeRun: null,
partialsSearchFieldName: '$partials_search',
defaultPartialName: '_default',
contextFieldName: '$variables'
};
exports.createImportHelper = createImportHelper = function(options) {
return function(prefix) {
var context, error, partial, partialsSearch, ref;
if ((ref = importDefaults.beforeRun) != null) {
ref.apply(this);
}
partialsSearch = this[options.partialsSearchFieldName];
if (!partialsSearch) {
console.warn(options.importName + ": no partials search specified, using \"" + options.defaultPartialName + "\".");
partialsSearch = [];
}
partial = getBestPartial(prefix, partialsSearch.concat(options.defaultPartialName));
if (partial) {
return new Handlebars.SafeString(partial(this));
}
error = [options.importName + ": Can't find any matching partial for \"" + prefix + "\"."];
if (options.contextFieldName) {
context = this[options.contextFieldName];
error.push("Context: " + (stringifyPairs(context)) + ".");
}
error.push("Partials search: " + (partialsSearch.join(', ')) + ".");
throw new Error(error.join('\n'));
};
};
exports.registerHelper = registerHelper = function(consolidate, options) {
options = _.assign({}, importDefaults, options);
return Handlebars.registerHelper(options.importName, createImportHelper(options));
};
exports.registerConsolidate = function(consolidate, options) {
registerHelper(consolidate, options);
return consolidate.requires.handlebars = Handlebars;
};
}).call(this);