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

Refactor.

This commit is contained in:
devlinjd 2015-12-09 04:32:48 -05:00
parent 3dcf3c3974
commit 2abfe4426c

View File

@ -5,6 +5,8 @@ Template-based resume generator base for FluentCV.
(function() { (function() {
var FS = require( 'fs-extra' ) var FS = require( 'fs-extra' )
, _ = require( 'underscore' ) , _ = require( 'underscore' )
, MD = require( 'marked' ) , MD = require( 'marked' )
@ -15,6 +17,8 @@ Template-based resume generator base for FluentCV.
, EXTEND = require('../utils/extend') , EXTEND = require('../utils/extend')
, Theme = require('../core/theme'); , Theme = require('../core/theme');
// Default options. // Default options.
var _defaultOpts = { var _defaultOpts = {
engine: 'underscore', engine: 'underscore',
@ -46,38 +50,118 @@ Template-based resume generator base for FluentCV.
} }
}; };
/** /**
TemplateGenerator performs resume generation via local Handlebar or Underscore TemplateGenerator performs resume generation via local Handlebar or Underscore
style template expansion and is appropriate for text-based formats like HTML, style template expansion and is appropriate for text-based formats like HTML,
plain text, and XML versions of Microsoft Word, Excel, and OpenOffice. plain text, and XML versions of Microsoft Word, Excel, and OpenOffice.
@class TemplateGenerator
*/ */
var TemplateGenerator = module.exports = BaseGenerator.extend({ var TemplateGenerator = module.exports = BaseGenerator.extend({
/** outputFormat: html, txt, pdf, doc
templateFormat: html or txt
**/
init: function( outputFormat, templateFormat, cssFile ){ init: function( outputFormat, templateFormat, cssFile ){
this._super( outputFormat ); this._super( outputFormat );
this.tplFormat = templateFormat || outputFormat; this.tplFormat = templateFormat || outputFormat;
}, },
/** Default generation method for template-based generators. */
invoke: function( rez, themeMarkup, cssInfo, opts ) {
// Compile and invoke the template!
invoke: function( rez, themeMarkup, cssInfo, opts ) {
this.opts = EXTEND( true, {}, _defaultOpts, opts ); this.opts = EXTEND( true, {}, _defaultOpts, opts );
mk = this.single( rez, themeMarkup, this.format, cssInfo, { } ); mk = this.single( rez, themeMarkup, this.format, cssInfo, { } );
this.onBeforeSave && (mk = this.onBeforeSave( mk, themeFile, f )); this.onBeforeSave && (mk = this.onBeforeSave( mk, themeFile, f ));
return mk; return mk;
}, },
/** Default generation method for template-based generators. */
/**
Default generation method for template-based generators.
@method generate
@param rez A FreshResume object.
@param f Full path to the output resume file to generate.
@param opts Generator options.
*/
generate: function( rez, f, opts ) { generate: function( rez, f, opts ) {
// Carry over options // Carry over options
this.opts = EXTEND( true, { }, _defaultOpts, opts ); this.opts = EXTEND( true, { }, _defaultOpts, opts );
// Load the theme
var themeInfo = themeFromMoniker.call( this );
var theme = themeInfo.theme;
var tFolder = themeInfo.folder;
var tplFolder = PATH.join( tFolder, 'src' );
var outFolder = PATH.parse(f).dir;
var curFmt = theme.getFormat( this.format );
var that = this;
// "Generate": process individual files within the theme
curFmt.files.forEach(function(tplInfo){
if( tplInfo.action === 'transform' ) {
transform.call( that, rez, f, tplInfo, theme, outFolder );
}
else if( tplInfo.action === null ) {
var thisFilePath = PATH.join(outFolder, tplInfo.orgPath);
try {
MKDIRP.sync( PATH.dirname(thisFilePath) );
FS.copySync( tplInfo.path, thisFilePath );
}
catch(ex) {
console.log(ex);
}
}
});
// Some themes require a symlink structure. If so, create it.
if( curFmt.symLinks ) {
Object.keys( curFmt.symLinks ).forEach( function(loc) {
var absLoc = PATH.join(outFolder, loc);
var absTarg = PATH.join(PATH.dirname(absLoc), curFmt.symLinks[loc]);
var type = PATH.parse( absLoc ).ext ? 'file' : 'junction'; // 'file', 'dir', or 'junction' (Windows only)
FS.symlinkSync( absTarg, absLoc, type);
});
}
},
/**
Perform a single resume JSON-to-DEST resume transformation.
@param json A FRESH or JRS resume object.
@param jst The stringified template data
@param format The format name, such as "html" or "latex"
@param cssInfo Needs to be refactored.
@param opts Options and passthrough data.
*/
single: function( json, jst, format, cssInfo, opts ) {
this.opts.freezeBreaks && ( jst = freeze(jst) );
var eng = require( '../eng/' + opts.themeObj.engine + '-generator' );
var result = eng( json, jst, format, cssInfo, opts );
this.opts.freezeBreaks && ( result = unfreeze(result) );
return result;
}
});
/**
Export the TemplateGenerator function/ctor.
*/
module.exports = TemplateGenerator;
/**
Given a theme title, load the corresponding theme.
*/
function themeFromMoniker() {
// Verify the specified theme name/path // Verify the specified theme name/path
var tFolder = PATH.join( var tFolder = PATH.join(
PATH.parse( require.resolve('fluent-themes') ).dir, PATH.parse( require.resolve('fluent-themes') ).dir,
@ -91,22 +175,24 @@ Template-based resume generator base for FluentCV.
} }
} }
var outFolder = PATH.parse(f).dir; var t = this.opts.themeObj || new Theme().open( tFolder );
// Load the theme // Load the theme and format
var theme = opts.themeObj || new Theme().open( tFolder ); return {
theme: t,
folder: tFolder
};
}
// Load theme and CSS data
var tplFolder = PATH.join( tFolder, 'src' );
var curFmt = theme.getFormat( this.format );
var that = this;
curFmt.files.forEach(function(tplInfo){ /**
if( tplInfo.action === 'transform' || tplInfo.action === null ) { Transform a single subfile.
if( tplInfo.action === 'transform' ) { */
function transform( rez, f, tplInfo, theme, outFolder ) {
var cssInfo = { file: tplInfo.css ? tplInfo.cssPath : null, data: tplInfo.css || null }; var cssInfo = { file: tplInfo.css ? tplInfo.cssPath : null, data: tplInfo.css || null };
var mk = that.single( rez, tplInfo.data, that.format, cssInfo, that.opts ); var mk = this.single( rez, tplInfo.data, this.format, cssInfo, this.opts );
that.onBeforeSave && (mk = that.onBeforeSave( { mk: mk, theme: theme, outputFile: f } )); this.onBeforeSave && (mk = this.onBeforeSave( { mk: mk, theme: theme, outputFile: f } ));
var thisFilePath = PATH.join( outFolder, tplInfo.orgPath ); var thisFilePath = PATH.join( outFolder, tplInfo.orgPath );
try { try {
MKDIRP.sync( PATH.dirname(thisFilePath) ); MKDIRP.sync( PATH.dirname(thisFilePath) );
@ -116,61 +202,8 @@ Template-based resume generator base for FluentCV.
console.log(ex); console.log(ex);
} }
} }
else if( tplInfo.action === null ) {
var thisFilePath = PATH.join(outFolder, tplInfo.orgPath);
try {
MKDIRP.sync( PATH.dirname(thisFilePath) );
FS.copySync( tplInfo.path, thisFilePath );
}
catch(ex) {
console.log(ex);
}
}
}
});
// Create symlinks
if( curFmt.symLinks ) {
Object.keys( curFmt.symLinks ).forEach( function(loc) {
var absLoc = PATH.join(outFolder, loc);
var absTarg = PATH.join(PATH.dirname(absLoc), curFmt.symLinks[loc]);
var type = PATH.parse( absLoc ).ext ? 'file' : 'junction'; // 'file', 'dir', or 'junction' (Windows only)
FS.symlinkSync( absTarg, absLoc, type);
});
}
},
/**
Perform a single resume JSON-to-DEST resume transformation.
@param json A FRESH or JRS resume object.
@param jst The stringified template data
@param format The format name, such as "html" or "latex"
@param cssInfo Needs to be refactored.
@param opts Options and passthrough data.
*/
single: function( json, jst, format, cssInfo, opts ) {
// Freeze whitespace in the template.
this.opts.freezeBreaks && ( jst = freeze(jst) );
// Apply the template.
var eng = require( '../eng/' + opts.themeObj.engine + '-generator' );
var result = eng( json, jst, format, cssInfo, opts );
// Unfreeze whitespace.
this.opts.freezeBreaks && ( result = unfreeze(result) );
return result;
}
});
/**
Export the TemplateGenerator function/ctor.
*/
module.exports = TemplateGenerator;
/** /**
Freeze newlines for protection against errant JST parsers. Freeze newlines for protection against errant JST parsers.
@ -181,6 +214,8 @@ Template-based resume generator base for FluentCV.
.replace( _reg.regR, _defaultOpts.rSym ); .replace( _reg.regR, _defaultOpts.rSym );
} }
/** /**
Unfreeze newlines when the coast is clear. Unfreeze newlines when the coast is clear.
*/ */
@ -190,6 +225,8 @@ Template-based resume generator base for FluentCV.
.replace( _reg.regSymN, '\n' ); .replace( _reg.regSymN, '\n' );
} }
/** /**
Regexes for linebreak preservation. Regexes for linebreak preservation.
*/ */
@ -200,4 +237,6 @@ Template-based resume generator base for FluentCV.
regSymR: new RegExp( _defaultOpts.rSym, 'g' ) regSymR: new RegExp( _defaultOpts.rSym, 'g' )
}; };
}()); }());