feat: support custom theme helpers

This commit is contained in:
hacksalot 2018-01-31 21:11:21 -05:00
parent 7f656175f0
commit 069506e86d
No known key found for this signature in database
GPG Key ID: 2F343EC247CA4B06
8 changed files with 88 additions and 12 deletions

6
dist/cli/error.js vendored
View File

@ -61,7 +61,7 @@ Error-handling routines for HackMyResume.
stack = ex.stack || (ex.inner && ex.inner.stack);
stack && o(chalk.gray(stack));
}
if (shouldExit) {
if (shouldExit || ex.exit) {
if (this.debug) {
o(chalk.cyan('Exiting with error code ' + ex.fluenterror.toString()));
}
@ -262,6 +262,10 @@ Error-handling routines for HackMyResume.
case HMSTATUS.unknownSchema:
msg = M2C(this.msgs.unknownSchema.msg[0]);
etype = 'error';
break;
case HMSTATUS.themeHelperLoad:
msg = printf(M2C(this.msgs.themeHelperLoad.msg), ex.glob);
etype = 'error';
}
return {
msg: msg,

5
dist/cli/msg.yml vendored
View File

@ -132,3 +132,8 @@ errors:
"basics": {
"name": "John Doe"
}
themeHelperLoad:
msg: >-
An error occurred while attempting to load the '%s' theme helper. Is the
theme correctly installed?
dummy: dontcare

View File

@ -36,7 +36,8 @@ Status codes for HackMyResume.
validateError: 26,
invalidOptionsFile: 27,
optionsFileNotFound: 28,
unknownSchema: 29
unknownSchema: 29,
themeHelperLoad: 30
};
}).call(this);

View File

@ -6,7 +6,7 @@ Template helper definitions for Handlebars.
*/
(function() {
var HANDLEBARS, _, blockHelpers, helpers;
var HANDLEBARS, HMS, _, blockHelpers, helpers, path;
HANDLEBARS = require('handlebars');
@ -14,8 +14,12 @@ Template helper definitions for Handlebars.
helpers = require('./generic-helpers');
path = require('path');
blockHelpers = require('./block-helpers');
HMS = require('../core/status-codes');
/**
Register useful Handlebars helpers.
@ -23,7 +27,7 @@ Template helper definitions for Handlebars.
*/
module.exports = function(theme, opts) {
var i, len, ref, themeHelpers, wrappedHelpers;
var curGlob, ex, glob, wrappedHelpers;
helpers.theme = theme;
helpers.opts = opts;
helpers.type = 'handlebars';
@ -41,10 +45,33 @@ Template helper definitions for Handlebars.
}, this);
HANDLEBARS.registerHelper(wrappedHelpers);
HANDLEBARS.registerHelper(blockHelpers);
ref = theme.jsFiles;
for (i = 0, len = ref.length; i < len; i++) {
themeHelpers = ref[i];
HANDLEBARS.registerHelper(require(themeHelpers));
if (_.isString(theme.helpers)) {
theme.helpers = [theme.helpers];
}
if (_.isArray(theme.helpers)) {
glob = require('glob');
curGlob = null;
try {
_.each(theme.helpers, function(fGlob) {
curGlob = fGlob;
fGlob = path.join(theme.folder, fGlob);
glob(fGlob, {}, function(er, files) {
if (er === null && files.length > 0) {
_.each(files, function(f) {
HANDLEBARS.registerHelper(require(f));
});
}
});
});
} catch (_error) {
ex = _error;
throw {
fluenterror: HMS.themeHelperLoad,
inner: ex,
glob: curGlob,
exit: true
};
}
}
};

View File

@ -57,7 +57,7 @@ ErrorHandler = module.exports =
stack && o( chalk.gray( stack ) );
# Quit if necessary
if shouldExit
if shouldExit or ex.exit
if @debug
o chalk.cyan('Exiting with error code ' + ex.fluenterror.toString())
if @assert
@ -253,6 +253,11 @@ assembleError = ( ex ) ->
#msg += "\n" + M2C( @msgs.unknownSchema.msg[1], 'yellow' )
etype = 'error'
when HMSTATUS.themeHelperLoad
msg = printf M2C( @msgs.themeHelperLoad.msg ), ex.glob
etype = 'error'
msg: msg # The error message to display
withStack: withStack # Whether to include the stack
quit: quit

View File

@ -132,3 +132,8 @@ errors:
"basics": {
"name": "John Doe"
}
themeHelperLoad:
msg: >-
An error occurred while attempting to load the '%s' theme helper. Is the
theme correctly installed?
dummy: dontcare

View File

@ -36,3 +36,4 @@ module.exports =
invalidOptionsFile: 27
optionsFileNotFound: 28
unknownSchema: 29
themeHelperLoad: 30

View File

@ -8,7 +8,9 @@ Template helper definitions for Handlebars.
HANDLEBARS = require 'handlebars'
_ = require 'underscore'
helpers = require './generic-helpers'
path = require 'path'
blockHelpers = require './block-helpers'
HMS = require '../core/status-codes'
###*
Register useful Handlebars helpers.
@ -33,6 +35,32 @@ module.exports = ( theme, opts ) ->
HANDLEBARS.registerHelper wrappedHelpers
HANDLEBARS.registerHelper blockHelpers
for themeHelpers in theme.jsFiles
HANDLEBARS.registerHelper require themeHelpers
return
# Register any theme-provided custom helpers...
# Normalize "theme.helpers" (string or array) to an array
theme.helpers = [ theme.helpers ] if _.isString theme.helpers
if _.isArray theme.helpers
glob = require 'glob'
curGlob = null
try
_.each theme.helpers, (fGlob) -> # foreach theme.helpers entry
curGlob = fGlob # cache in case of exception
fGlob = path.join theme.folder, fGlob # make relative to theme
glob fGlob, { }, (er, files) -> # expand the glob to paths
if er is null and files.length > 0 # guard against the error
_.each files, (f) -> # loop over concrete paths
HANDLEBARS.registerHelper require f # register the path
return
# else if er # glob error occurred
# throw fluenterror: HMS.themeHelperLoad, inner: er, glob: fGlob
# else if files.length < 1 # glob returned no results
# throw fluenterror: HMS.themeHelperLoad
return
return
return
catch ex
# If a non-path is passed to glob() it will throw an error
throw fluenterror: HMS.themeHelperLoad, inner: ex, glob: curGlob, exit: true
return