1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-05 09:56:22 +00:00

feat: improve template helper wiring

This commit is contained in:
hacksalot 2018-02-07 05:49:02 -05:00
parent 2346562428
commit 38a073b09a
No known key found for this signature in database
GPG Key ID: 2F343EC247CA4B06
4 changed files with 27 additions and 14 deletions

View File

@ -26,7 +26,7 @@ Template helper definitions for Handlebars.
@method registerHelpers @method registerHelpers
*/ */
module.exports = function(theme, opts) { module.exports = function(theme, rez, opts) {
var curGlob, ex, glob, slash, wrappedHelpers; var curGlob, ex, glob, slash, wrappedHelpers;
helpers.theme = theme; helpers.theme = theme;
helpers.opts = opts; helpers.opts = opts;
@ -37,7 +37,7 @@ Template helper definitions for Handlebars.
var args; var args;
args = Array.prototype.slice.call(arguments); args = Array.prototype.slice.call(arguments);
args.shift(); args.shift();
args.pop(); args[args.length - 1] = rez;
return func.apply(this, args); return func.apply(this, args);
}); });
} }

View File

@ -51,8 +51,6 @@ Definition of the HandlebarsGenerator class.
}, },
generate: function(json, jst, format, curFmt, opts, theme) { generate: function(json, jst, format, curFmt, opts, theme) {
var ctx, encData; var ctx, encData;
registerPartials(format, theme);
registerHelpers(theme, opts);
encData = json; encData = json;
if (format === 'html' || format === 'pdf') { if (format === 'html' || format === 'pdf') {
encData = json.markdownify(); encData = json.markdownify();
@ -60,6 +58,8 @@ Definition of the HandlebarsGenerator class.
if (format === 'doc') { if (format === 'doc') {
encData = json.xmlify(); encData = json.xmlify();
} }
registerPartials(format, theme);
registerHelpers(theme, encData, opts);
ctx = { ctx = {
r: encData, r: encData,
RAW: json, RAW: json,

View File

@ -17,23 +17,28 @@ Register useful Handlebars helpers.
@method registerHelpers @method registerHelpers
### ###
module.exports = ( theme, opts ) -> module.exports = ( theme, rez, opts ) ->
helpers.theme = theme helpers.theme = theme
helpers.opts = opts helpers.opts = opts
helpers.type = 'handlebars' helpers.type = 'handlebars'
# Prepare generic helpers for use with Handlebars. We do this by wrapping them
# in a Handlebars-aware wrapper which calls the helper internally.
wrappedHelpers = _.mapObject helpers, ( hVal, hKey ) -> wrappedHelpers = _.mapObject helpers, ( hVal, hKey ) ->
if _.isFunction hVal if _.isFunction hVal
return _.wrap hVal, (func) -> return _.wrap hVal, (func) ->
args = Array.prototype.slice.call arguments args = Array.prototype.slice.call arguments
args.shift() # lose the 1st element (func) args.shift() # lose the 1st element (func) [^1]
args.pop() # lose the last element (the Handlebars options hash) #args.pop() # lose the last element (HB options hash)
func.apply @, args args[ args.length - 1 ] = rez # replace w/ resume object
func.apply @, args # call the generic helper
hVal hVal
, @ , @
HANDLEBARS.registerHelper wrappedHelpers HANDLEBARS.registerHelper wrappedHelpers
# Prepare Handlebars-specific helpers - "blockHelpers" is really a misnomer
# since any kind of Handlebars-specific helper can live here
HANDLEBARS.registerHelper blockHelpers HANDLEBARS.registerHelper blockHelpers
# Register any theme-provided custom helpers... # Register any theme-provided custom helpers...
@ -64,3 +69,9 @@ module.exports = ( theme, opts ) ->
inner: ex inner: ex
glob: curGlob, exit: true glob: curGlob, exit: true
return return
# [^1]: This little bit of acrobatics ensures that our generic helpers are
# called as generic helpers, not as Handlebars-specific helpers. This allows
# them to be used in other templating engines, like Underscore. If you need a
# Handlebars-specific helper with normal Handlebars context and options, put it
# in block-helpers.coffee.

View File

@ -30,7 +30,9 @@ HandlebarsGenerator = module.exports =
try try
# Compile and run the Handlebars template. # Compile and run the Handlebars template.
template = HANDLEBARS.compile tpl, template = HANDLEBARS.compile tpl,
strict: false, assumeObjects: false, noEscape: data.opts.noescape strict: false
assumeObjects: false
noEscape: data.opts.noescape
return template data return template data
catch catch
throw throw
@ -42,10 +44,6 @@ HandlebarsGenerator = module.exports =
generate: ( json, jst, format, curFmt, opts, theme ) -> generate: ( json, jst, format, curFmt, opts, theme ) ->
# Set up partials and helpers
registerPartials format, theme
registerHelpers theme, opts
# Preprocess text # Preprocess text
encData = json encData = json
if format == 'html' || format == 'pdf' if format == 'html' || format == 'pdf'
@ -53,6 +51,10 @@ HandlebarsGenerator = module.exports =
if( format == 'doc' ) if( format == 'doc' )
encData = json.xmlify() encData = json.xmlify()
# Set up partials and helpers
registerPartials format, theme
registerHelpers theme, encData, opts
# Set up the context # Set up the context
ctx = ctx =
r: encData r: encData