1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2025-05-03 04:47:07 +01:00

feat: support custom theme helpers

This commit is contained in:
hacksalot
2018-01-31 21:11:21 -05:00
parent 7f656175f0
commit 069506e86d
8 changed files with 88 additions and 12 deletions

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