From 1441fe3ae5ffbe2d511313259fb2de02c13b93b3 Mon Sep 17 00:00:00 2001 From: hacksalot Date: Fri, 18 Dec 2015 13:17:07 -0500 Subject: [PATCH] Class-ify Underscore/Handlebars engine. --- src/eng/handlebars-generator.js | 43 +++++++++++++------------ src/eng/underscore-generator.js | 57 +++++++++++++++++++++------------ src/gen/template-generator.js | 2 +- 3 files changed, 60 insertions(+), 42 deletions(-) diff --git a/src/eng/handlebars-generator.js b/src/eng/handlebars-generator.js index 20964ca..8993811 100644 --- a/src/eng/handlebars-generator.js +++ b/src/eng/handlebars-generator.js @@ -17,31 +17,34 @@ Definition of the HandlebarsGenerator class. /** Perform template-based resume generation using Handlebars.js. - @method generate + @class HandlebarsGenerator */ - module.exports = function( json, jst, format, cssInfo, opts, theme ) { + var HandlebarsGenerator = module.exports = { - // Pre-compile any partials present in the theme. - _.each( theme.partials, function( el ) { - var tplData = FS.readFileSync( el.path, 'utf8' ); - var compiledTemplate = HANDLEBARS.compile( tplData ); - HANDLEBARS.registerPartial( el.name, compiledTemplate ); - }); + generate: function( json, jst, format, cssInfo, opts, theme ) { - // Register necessary helpers. - registerHelpers(); + // Pre-compile any partials present in the theme. + _.each( theme.partials, function( el ) { + var tplData = FS.readFileSync( el.path, 'utf8' ); + var compiledTemplate = HANDLEBARS.compile( tplData ); + HANDLEBARS.registerPartial( el.name, compiledTemplate ); + }); - // Compile and run the Handlebars template. - var template = HANDLEBARS.compile(jst); - return template({ - r: json, - filt: opts.filters, - cssInfo: cssInfo, - headFragment: opts.headFragment || '' - }); + // Register necessary helpers. + registerHelpers(); + + // Compile and run the Handlebars template. + var template = HANDLEBARS.compile(jst); + return template({ + r: format === 'html' || format === 'pdf' ? json.markdownify() : json, + RAW: json, + filt: opts.filters, + cssInfo: cssInfo, + headFragment: opts.headFragment || '' + }); + + } }; - - }()); diff --git a/src/eng/underscore-generator.js b/src/eng/underscore-generator.js index 755155d..fe2fb36 100644 --- a/src/eng/underscore-generator.js +++ b/src/eng/underscore-generator.js @@ -1,37 +1,52 @@ /** Definition of the UnderscoreGenerator class. @license MIT. Copyright (c) 2015 James Devlin / FluentDesk. +@module underscore-generator.js */ (function() { + + var _ = require('underscore'); - module.exports = function( json, jst, format, cssInfo, opts, theme ) { - // Tweak underscore's default template delimeters - var delims = (opts.themeObj && opts.themeObj.delimeters) || opts.template; - if( opts.themeObj && opts.themeObj.delimeters ) { - delims = _.mapObject( delims, function(val,key) { - return new RegExp( val, "ig"); + + /** + Perform template-based resume generation using Underscore.js. + @class UnderscoreGenerator + */ + var UnderscoreGenerator = module.exports = { + + generate: function( json, jst, format, cssInfo, opts, theme ) { + + // Tweak underscore's default template delimeters + var delims = (opts.themeObj && opts.themeObj.delimeters) || opts.template; + if( opts.themeObj && opts.themeObj.delimeters ) { + delims = _.mapObject( delims, function(val,key) { + return new RegExp( val, "ig"); + }); + } + _.templateSettings = delims; + + // Strip {# comments #} + jst = jst.replace( delims.comment, ''); + + // Compile and run the template. TODO: avoid unnecessary recompiles. + var compiled = _.template(jst); + var ret = compiled({ + r: format === 'html' || format === 'pdf' ? json.markdownify() : json, + filt: opts.filters, + XML: require('xml-escape'), + RAW: json, + cssInfo: cssInfo, + headFragment: opts.headFragment || '' }); + return ret; } - _.templateSettings = delims; - - // Strip {# comments #} - jst = jst.replace( delims.comment, ''); - // Compile and run the template. TODO: avoid unnecessary recompiles. - var compiled = _.template(jst); - var ret = compiled({ - r: format === 'html' || format === 'pdf' ? json.markdownify() : json, - filt: opts.filters, - XML: require('xml-escape'), - RAW: json, - cssInfo: cssInfo, - headFragment: opts.headFragment || '' - }); - return ret; }; + + }()); diff --git a/src/gen/template-generator.js b/src/gen/template-generator.js index b78d59d..fb6ee39 100644 --- a/src/gen/template-generator.js +++ b/src/gen/template-generator.js @@ -143,7 +143,7 @@ Definition of the TemplateGenerator class. single: function( json, jst, format, cssInfo, opts, theme ) { this.opts.freezeBreaks && ( jst = freeze(jst) ); var eng = require( '../eng/' + theme.engine + '-generator' ); - var result = eng( json, jst, format, cssInfo, opts, theme ); + var result = eng.generate( json, jst, format, cssInfo, opts, theme ); this.opts.freezeBreaks && ( result = unfreeze(result) ); return result; }