2014-10-10 18:54:07 +04:00
|
|
|
var hbs = require('express-hbs'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2014-05-09 14:11:29 +04:00
|
|
|
errors = require('../errors'),
|
2014-10-10 18:54:07 +04:00
|
|
|
utils = require('./utils'),
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../i18n'),
|
2013-09-15 01:34:12 +04:00
|
|
|
coreHelpers = {},
|
2014-10-10 18:54:07 +04:00
|
|
|
registerHelpers;
|
2013-12-02 03:31:55 +04:00
|
|
|
|
2014-10-10 18:54:07 +04:00
|
|
|
if (!utils.isProduction) {
|
2014-02-05 02:01:53 +04:00
|
|
|
hbs.handlebars.logger.level = 0;
|
|
|
|
}
|
|
|
|
|
2014-10-10 18:54:07 +04:00
|
|
|
coreHelpers.asset = require('./asset');
|
|
|
|
coreHelpers.author = require('./author');
|
|
|
|
coreHelpers.body_class = require('./body_class');
|
|
|
|
coreHelpers.content = require('./content');
|
|
|
|
coreHelpers.date = require('./date');
|
|
|
|
coreHelpers.encode = require('./encode');
|
|
|
|
coreHelpers.excerpt = require('./excerpt');
|
|
|
|
coreHelpers.foreach = require('./foreach');
|
2015-06-27 18:42:10 +03:00
|
|
|
coreHelpers.get = require('./get');
|
2014-10-10 18:54:07 +04:00
|
|
|
coreHelpers.ghost_foot = require('./ghost_foot');
|
|
|
|
coreHelpers.ghost_head = require('./ghost_head');
|
2015-05-13 12:26:49 +03:00
|
|
|
coreHelpers.image = require('./image');
|
2014-10-10 18:54:07 +04:00
|
|
|
coreHelpers.is = require('./is');
|
|
|
|
coreHelpers.has = require('./has');
|
|
|
|
coreHelpers.meta_description = require('./meta_description');
|
|
|
|
coreHelpers.meta_title = require('./meta_title');
|
2015-01-21 10:00:38 +03:00
|
|
|
coreHelpers.navigation = require('./navigation');
|
2014-10-10 18:54:07 +04:00
|
|
|
coreHelpers.pagination = require('./pagination');
|
|
|
|
coreHelpers.plural = require('./plural');
|
|
|
|
coreHelpers.post_class = require('./post_class');
|
2015-05-13 12:26:49 +03:00
|
|
|
coreHelpers.prev_post = require('./prev_next');
|
|
|
|
coreHelpers.next_post = require('./prev_next');
|
2014-10-10 18:54:07 +04:00
|
|
|
coreHelpers.tags = require('./tags');
|
|
|
|
coreHelpers.title = require('./title');
|
|
|
|
coreHelpers.url = require('./url');
|
2015-05-13 12:26:49 +03:00
|
|
|
|
|
|
|
// Specialist helpers for certain templates
|
|
|
|
coreHelpers.input_password = require('./input_password');
|
|
|
|
coreHelpers.page_url = require('./page_url');
|
|
|
|
coreHelpers.pageUrl = require('./page_url').deprecated;
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2013-09-15 01:34:12 +04:00
|
|
|
coreHelpers.helperMissing = function (arg) {
|
|
|
|
if (arguments.length === 2) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2015-11-12 15:29:45 +03:00
|
|
|
errors.logError(i18n.t('warnings.helpers.index.missingHelper', {arg: arg}));
|
2013-09-15 01:34:12 +04:00
|
|
|
};
|
2013-09-14 06:15:30 +04:00
|
|
|
|
2013-12-02 03:31:55 +04:00
|
|
|
// Register an async handlebars helper for a given handlebars instance
|
|
|
|
function registerAsyncHelper(hbs, name, fn) {
|
2015-03-10 18:52:00 +03:00
|
|
|
hbs.registerAsyncHelper(name, function (context, options, cb) {
|
|
|
|
// Handle the case where we only get context and cb
|
|
|
|
if (!cb) {
|
|
|
|
cb = options;
|
|
|
|
options = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap the function passed in with a when.resolve so it can return either a promise or a value
|
|
|
|
Promise.resolve(fn.call(this, context, options)).then(function (result) {
|
2013-11-28 06:45:01 +04:00
|
|
|
cb(result);
|
2014-08-17 10:17:23 +04:00
|
|
|
}).catch(function (err) {
|
2014-07-20 20:41:59 +04:00
|
|
|
errors.logAndThrowError(err, 'registerAsyncThemeHelper: ' + name);
|
2013-11-28 06:45:01 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-12-02 03:31:55 +04:00
|
|
|
// Register a handlebars helper for themes
|
|
|
|
function registerThemeHelper(name, fn) {
|
|
|
|
hbs.registerHelper(name, fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register an async handlebars helper for themes
|
|
|
|
function registerAsyncThemeHelper(name, fn) {
|
|
|
|
registerAsyncHelper(hbs, name, fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a handlebars helper for admin
|
|
|
|
function registerAdminHelper(name, fn) {
|
|
|
|
coreHelpers.adminHbs.registerHelper(name, fn);
|
|
|
|
}
|
|
|
|
|
2014-10-10 18:54:07 +04:00
|
|
|
registerHelpers = function (adminHbs) {
|
2013-12-02 03:31:55 +04:00
|
|
|
// Expose hbs instance for admin
|
|
|
|
coreHelpers.adminHbs = adminHbs;
|
|
|
|
|
|
|
|
// Register theme helpers
|
2013-11-28 06:45:01 +04:00
|
|
|
registerThemeHelper('asset', coreHelpers.asset);
|
|
|
|
registerThemeHelper('author', coreHelpers.author);
|
2015-12-17 17:39:43 +03:00
|
|
|
registerThemeHelper('body_class', coreHelpers.body_class);
|
2013-11-28 06:45:01 +04:00
|
|
|
registerThemeHelper('content', coreHelpers.content);
|
|
|
|
registerThemeHelper('date', coreHelpers.date);
|
|
|
|
registerThemeHelper('encode', coreHelpers.encode);
|
|
|
|
registerThemeHelper('excerpt', coreHelpers.excerpt);
|
|
|
|
registerThemeHelper('foreach', coreHelpers.foreach);
|
2015-12-17 17:39:43 +03:00
|
|
|
registerThemeHelper('has', coreHelpers.has);
|
2014-09-04 20:07:12 +04:00
|
|
|
registerThemeHelper('is', coreHelpers.is);
|
2015-12-17 17:39:43 +03:00
|
|
|
registerThemeHelper('image', coreHelpers.image);
|
2015-05-13 12:26:49 +03:00
|
|
|
registerThemeHelper('input_password', coreHelpers.input_password);
|
2015-12-17 17:39:43 +03:00
|
|
|
registerThemeHelper('meta_description', coreHelpers.meta_description);
|
|
|
|
registerThemeHelper('meta_title', coreHelpers.meta_title);
|
2015-01-21 10:00:38 +03:00
|
|
|
registerThemeHelper('navigation', coreHelpers.navigation);
|
2014-02-20 00:17:43 +04:00
|
|
|
registerThemeHelper('page_url', coreHelpers.page_url);
|
2013-11-28 06:45:01 +04:00
|
|
|
registerThemeHelper('pageUrl', coreHelpers.pageUrl);
|
2013-12-02 03:31:55 +04:00
|
|
|
registerThemeHelper('pagination', coreHelpers.pagination);
|
2014-07-25 19:31:18 +04:00
|
|
|
registerThemeHelper('plural', coreHelpers.plural);
|
2015-12-17 17:39:43 +03:00
|
|
|
registerThemeHelper('post_class', coreHelpers.post_class);
|
|
|
|
registerThemeHelper('tags', coreHelpers.tags);
|
|
|
|
registerThemeHelper('title', coreHelpers.title);
|
2014-12-28 22:38:29 +03:00
|
|
|
registerThemeHelper('url', coreHelpers.url);
|
2014-07-25 19:31:18 +04:00
|
|
|
|
2014-10-10 18:54:07 +04:00
|
|
|
// Async theme helpers
|
2013-11-28 06:45:01 +04:00
|
|
|
registerAsyncThemeHelper('ghost_foot', coreHelpers.ghost_foot);
|
|
|
|
registerAsyncThemeHelper('ghost_head', coreHelpers.ghost_head);
|
2015-03-21 15:14:51 +03:00
|
|
|
registerAsyncThemeHelper('next_post', coreHelpers.next_post);
|
|
|
|
registerAsyncThemeHelper('prev_post', coreHelpers.prev_post);
|
2015-06-27 18:42:10 +03:00
|
|
|
registerAsyncThemeHelper('get', coreHelpers.get);
|
2013-12-06 12:51:35 +04:00
|
|
|
|
2013-12-02 03:31:55 +04:00
|
|
|
// Register admin helpers
|
2014-07-01 03:26:08 +04:00
|
|
|
registerAdminHelper('asset', coreHelpers.asset);
|
2015-05-13 12:26:49 +03:00
|
|
|
registerAdminHelper('input_password', coreHelpers.input_password);
|
2013-12-14 20:28:54 +04:00
|
|
|
};
|
2013-12-02 03:31:55 +04:00
|
|
|
|
2013-09-15 01:34:12 +04:00
|
|
|
module.exports = coreHelpers;
|
|
|
|
module.exports.loadCoreHelpers = registerHelpers;
|
2013-11-28 06:45:01 +04:00
|
|
|
module.exports.registerThemeHelper = registerThemeHelper;
|
|
|
|
module.exports.registerAsyncThemeHelper = registerAsyncThemeHelper;
|