1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2025-07-12 13:11:08 +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

@ -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