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

Add baseline Handlebars support.

This commit is contained in:
devlinjd 2015-12-06 16:19:55 -05:00
parent fb783cdbc6
commit 3b8d100f39
4 changed files with 204 additions and 156 deletions

View File

@ -10,10 +10,11 @@ Abstract theme representation.
, validator = require('is-my-json-valid') , validator = require('is-my-json-valid')
, _ = require('underscore') , _ = require('underscore')
, PATH = require('path') , PATH = require('path')
, EXTEND = require('../utils/extend')
, moment = require('moment'); , moment = require('moment');
/** /**
The Theme class represents a specific presentation of a resume. The Theme class is a representation of a FluentCV theme asset.
@class Theme @class Theme
*/ */
function Theme() { function Theme() {
@ -31,8 +32,12 @@ Abstract theme representation.
return friendly[val] || val; return friendly[val] || val;
} }
// Remember the theme folder; might be custom // Open the theme.json file; should have the same name as folder
this.folder = themeFolder; this.folder = themeFolder;
var pathInfo = PATH.parse( themeFolder );
var themeFile = PATH.join( themeFolder, pathInfo.base + '.json' );
var themeInfo = JSON.parse( FS.readFileSync( themeFile, 'utf8' ) );
EXTEND( true, this, themeInfo );
// Iterate over all files in the theme folder, producing an array, fmts, // Iterate over all files in the theme folder, producing an array, fmts,
// containing info for each file. // containing info for each file.

View File

@ -0,0 +1,18 @@
/**
Handlebars template generate for FluentCV.
@license MIT. Copyright (c) 2015 James M. Devlin / FluentDesk.
*/
(function() {
var _ = require('underscore');
var HANDLEBARS = require('handlebars');
module.exports = function( json, jst, format, cssInfo, opts ) {
var template = HANDLEBARS.compile(jst);
return template( { r: json, filt: opts.filters, cssInfo: cssInfo, headFragment: opts.headFragment || '' } )
};
}());

View File

@ -0,0 +1,37 @@
/**
Underscore template generate for FluentCV.
@license MIT. Copyright (c) 2015 James M. Devlin / FluentDesk.
*/
(function() {
var _ = require('underscore');
module.exports = function( json, jst, format, cssInfo, opts ) {
// Tweak underscore's default template delimeters
_.templateSettings = opts.template;
// Convert {{ someVar }} to {% print(filt.out(someVar) %}
// Convert {{ someVar|someFilter }} to {% print(filt.someFilter(someVar) %}
jst = jst.replace( _.templateSettings.interpolate, function replace(m, p1) {
if( p1.indexOf('|') > -1 ) {
var terms = p1.split('|');
return '{% print( filt.' + terms[1] + '( ' + terms[0] + ' )) %}';
}
else {
return '{% print( filt.out(' + p1 + ') ) %}';
}
});
// Strip {# comments #}
jst = jst.replace( _.templateSettings.comment, '');
// Compile and run the template. TODO: avoid unnecessary recompiles.
jst = _.template(jst)({ r: json, filt: opts.filters, cssInfo: cssInfo, headFragment: opts.headFragment || '' });
return jst;
};
}());

View File

@ -1,8 +1,10 @@
/** /**
Template-based resume generator base for FluentCV. Template-based resume generator base for FluentCV.
@license Copyright (c) 2015 | James M. Devlin @license MIT. Copyright (c) 2015 James M. Devlin / FluentDesk.
*/ */
(function() {
var FS = require( 'fs' ) var FS = require( 'fs' )
, _ = require( 'underscore' ) , _ = require( 'underscore' )
, MD = require( 'marked' ) , MD = require( 'marked' )
@ -14,6 +16,7 @@ var FS = require( 'fs' )
// Default options. // Default options.
var _defaultOpts = { var _defaultOpts = {
engine: 'underscore',
keepBreaks: true, keepBreaks: true,
freezeBreaks: true, freezeBreaks: true,
nSym: '&newl;', // newline entity nSym: '&newl;', // newline entity
@ -43,9 +46,9 @@ var _defaultOpts = {
}; };
/** /**
TemplateGenerator performs resume generation via Underscore-style template TemplateGenerator performs resume generation via local Handlebar or Underscore
expansion and is appropriate for text-based formats like HTML, plain text, style template expansion and is appropriate for text-based formats like HTML,
and XML versions of Microsoft Word, Excel, and OpenOffice. plain text, and XML versions of Microsoft Word, Excel, and OpenOffice.
*/ */
var TemplateGenerator = module.exports = BaseGenerator.extend({ var TemplateGenerator = module.exports = BaseGenerator.extend({
@ -96,7 +99,7 @@ var TemplateGenerator = module.exports = BaseGenerator.extend({
var cssInfo = { file: curFmt.css ? curFmt.cssPath : null, data: curFmt.css || null }; var cssInfo = { file: curFmt.css ? curFmt.cssPath : null, data: curFmt.css || null };
// Compile and invoke the template! // Compile and invoke the template!
var mk = this.single( rez, curFmt.data, this.format, cssInfo, opts ); var mk = this.single( rez, curFmt.data, this.format, cssInfo, this.opts );
this.onBeforeSave && (mk = this.onBeforeSave( { mk: mk, theme: theme, outputFile: f } )); this.onBeforeSave && (mk = this.onBeforeSave( { mk: mk, theme: theme, outputFile: f } ));
FS.writeFileSync( f, mk, { encoding: 'utf8', flags: 'w' } ); FS.writeFileSync( f, mk, { encoding: 'utf8', flags: 'w' } );
@ -112,31 +115,14 @@ var TemplateGenerator = module.exports = BaseGenerator.extend({
// Freeze whitespace in the template. // Freeze whitespace in the template.
this.opts.freezeBreaks && ( jst = freeze(jst) ); this.opts.freezeBreaks && ( jst = freeze(jst) );
// Tweak underscore's default template delimeters // Apply the template.
_.templateSettings = this.opts.template; var eng = require( '../eng/' + opts.themeObj.engine + '-generator' );
var result = eng( json, jst, format, cssInfo, opts );
// Convert {{ someVar }} to {% print(filt.out(someVar) %} // Unfreeze whitespace.
// Convert {{ someVar|someFilter }} to {% print(filt.someFilter(someVar) %} this.opts.freezeBreaks && ( result = unfreeze(result) );
jst = jst.replace( _.templateSettings.interpolate, function replace(m, p1) {
if( p1.indexOf('|') > -1 ) {
var terms = p1.split('|');
return '{% print( filt.' + terms[1] + '( ' + terms[0] + ' )) %}';
}
else {
return '{% print( filt.out(' + p1 + ') ) %}';
}
});
// Strip {# comments #} return result;
jst = jst.replace( _.templateSettings.comment, '');
// Compile and run the template. TODO: avoid unnecessary recompiles.
jst = _.template(jst)({ r: json, filt: this.opts.filters, cssInfo: cssInfo, headFragment: this.opts.headFragment || '' });
// Unfreeze whitespace
this.opts.freezeBreaks && ( jst = unfreeze(jst) );
return jst;
} }
@ -174,3 +160,5 @@ var _reg = {
regSymN: new RegExp( _defaultOpts.nSym, 'g' ), regSymN: new RegExp( _defaultOpts.nSym, 'g' ),
regSymR: new RegExp( _defaultOpts.rSym, 'g' ) regSymR: new RegExp( _defaultOpts.rSym, 'g' )
}; };
}());