2015-09-24 21:09:48 +01:00
|
|
|
/**
|
2015-10-06 17:21:22 +01:00
|
|
|
Internal resume generation logic for FluentCMD.
|
|
|
|
@license Copyright (c) 2015 | James M. Devlin
|
2015-09-24 21:09:48 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = function () {
|
|
|
|
|
2015-10-06 17:21:22 +01:00
|
|
|
// We don't mind pseudo-globals here
|
2015-10-06 17:24:29 +01:00
|
|
|
var path = require( 'path' )
|
2015-09-24 21:09:48 +01:00
|
|
|
, extend = require( './utils/extend' )
|
2015-09-26 20:05:37 +01:00
|
|
|
, unused = require('./utils/string')
|
2015-10-06 17:21:22 +01:00
|
|
|
, FLUENT = require('fluentlib')
|
2015-10-06 17:24:29 +01:00
|
|
|
, rez, _log;
|
2015-09-24 21:09:48 +01:00
|
|
|
|
|
|
|
/**
|
2015-10-06 17:21:22 +01:00
|
|
|
Core workhorse method for FluentCMD. Given a source JSON resume, a destination
|
|
|
|
resume path, and a theme file, generate 0..N resumes in the desired formats.
|
2015-09-24 21:09:48 +01:00
|
|
|
@param src Path to the source JSON resume file: "rez/resume.json".
|
2015-10-06 17:21:22 +01:00
|
|
|
@param dst An array of paths to the destination resume file(s): "rez/resume.all".
|
2015-09-24 21:09:48 +01:00
|
|
|
@param theme Friendly name of the resume theme. Defaults to "default".
|
2015-10-06 17:21:22 +01:00
|
|
|
@param logger Optional logging override.
|
2015-09-24 21:09:48 +01:00
|
|
|
*/
|
2015-10-06 17:21:22 +01:00
|
|
|
function gen( src, dst, theme, logger ) {
|
2015-09-24 21:09:48 +01:00
|
|
|
|
2015-09-29 09:15:04 +01:00
|
|
|
_log = logger || console.log;
|
2015-09-24 21:09:48 +01:00
|
|
|
_opts.theme = theme;
|
|
|
|
dst = (dst && dst.length && dst) || ['resume.all'];
|
|
|
|
|
|
|
|
// Assemble output resume targets
|
|
|
|
var targets = [];
|
|
|
|
dst.forEach( function(t) {
|
|
|
|
t = path.resolve(t);
|
|
|
|
var dot = t.lastIndexOf('.');
|
|
|
|
var format = ( dot === -1 ) ? 'all' : t.substring( dot + 1 );
|
|
|
|
var temp = ( format === 'all' ) ?
|
|
|
|
_fmts.map( function( fmt ) { return t.replace( /all$/g, fmt.name ); }) :
|
|
|
|
( format === 'doc' ? [ 'doc' ] : [ t ] ); // interim code
|
|
|
|
targets.push.apply(targets, temp);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Assemble input resumes
|
|
|
|
var sheets = src.map( function( res ) {
|
2015-09-29 09:15:04 +01:00
|
|
|
_log( 'Reading JSON resume: ' + res );
|
2015-09-24 21:09:48 +01:00
|
|
|
return (new FLUENT.Sheet()).open( res );
|
|
|
|
});
|
|
|
|
|
|
|
|
// Merge input resumes
|
2015-10-06 21:09:40 +01:00
|
|
|
var msg = '';
|
|
|
|
sheets.length > 1 && (msg += ('Merging ' + sheets[ sheets.length - 1].meta.fileName));
|
2015-09-24 21:09:48 +01:00
|
|
|
rez = sheets.reduce( function( acc, elem ) {
|
2015-10-06 21:09:40 +01:00
|
|
|
msg += (' onto ' + acc.meta.fileName);
|
|
|
|
return extend( true, acc, elem );
|
2015-09-24 21:09:48 +01:00
|
|
|
});
|
2015-10-06 21:09:40 +01:00
|
|
|
sheets.length > 1 && _log( msg );
|
|
|
|
|
2015-09-24 21:09:48 +01:00
|
|
|
|
|
|
|
// Run the transformation!
|
2015-10-06 17:21:22 +01:00
|
|
|
var finished = targets.map( single );
|
2015-09-24 21:09:48 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
sheet: rez,//.rep,
|
|
|
|
targets: targets,
|
|
|
|
processed: finished
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Generate a single resume of a specific format.
|
|
|
|
@param f Full path to the destination resume to generate, for example,
|
|
|
|
"/foo/bar/resume.pdf" or "c:\foo\bar\resume.txt".
|
|
|
|
*/
|
2015-10-06 17:21:22 +01:00
|
|
|
function single( f ) {
|
2015-09-24 21:09:48 +01:00
|
|
|
try {
|
|
|
|
|
|
|
|
// Get the output file type (pdf, html, txt, etc)
|
|
|
|
var fType = path.extname( f ).trim().toLowerCase().substr(1);
|
|
|
|
var fName = path.basename( f, '.' + fType );
|
|
|
|
|
|
|
|
// Get the format object (if any) corresponding to that type, and assemble
|
|
|
|
// the final output file path for the generated resume.
|
|
|
|
var fObj = _fmts.filter( function(_f) { return _f.name === fType; } )[0];
|
|
|
|
var fOut = path.join( f.substring( 0, f.lastIndexOf('.') + 1 ) + fObj.ext );
|
|
|
|
|
|
|
|
// Generate!
|
2015-10-04 20:05:44 +01:00
|
|
|
_log( 'Generating ' + fType.toUpperCase() + ' resume: ' + path.relative(process.cwd(), f) );
|
2015-09-24 21:09:48 +01:00
|
|
|
return fObj.gen.generate( rez, fOut, _opts.theme );
|
|
|
|
}
|
|
|
|
catch( ex ) {
|
|
|
|
err( ex );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Handle an exception.
|
|
|
|
*/
|
|
|
|
function err( ex ) {
|
|
|
|
var msg = ex.toString();
|
|
|
|
var idx = msg.indexOf('Error: ');
|
|
|
|
var trimmed = idx === -1 ? msg : msg.substring( idx + 7 );
|
2015-09-29 09:15:04 +01:00
|
|
|
_log( 'ERROR: ' + trimmed.toString() );
|
2015-09-24 21:09:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Supported resume formats.
|
|
|
|
*/
|
|
|
|
var _fmts = [
|
|
|
|
{ name: 'html', ext: 'html', gen: new FLUENT.HtmlGenerator() },
|
|
|
|
{ name: 'txt', ext: 'txt', gen: new FLUENT.TextGenerator() },
|
|
|
|
{ name: 'doc', ext: 'doc', fmt: 'xml', gen: new FLUENT.WordGenerator() },
|
|
|
|
{ name: 'pdf', ext: 'pdf', fmt: 'html', is: false, gen: new FLUENT.HtmlPdfGenerator() }
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2015-10-06 17:24:29 +01:00
|
|
|
Default options.
|
2015-09-24 21:09:48 +01:00
|
|
|
*/
|
|
|
|
var _opts = {
|
|
|
|
theme: 'default',
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-06 17:21:22 +01:00
|
|
|
Module public interface. Used by FCV Desktop.
|
2015-09-24 21:09:48 +01:00
|
|
|
*/
|
|
|
|
return {
|
2015-10-06 17:21:22 +01:00
|
|
|
generate: gen,
|
2015-09-24 21:09:48 +01:00
|
|
|
options: _opts,
|
|
|
|
formats: _fmts
|
|
|
|
};
|
|
|
|
|
|
|
|
}();
|