2015-09-24 21:09:48 +01:00
|
|
|
/**
|
2015-10-27 07:54:50 +00:00
|
|
|
Internal resume generation logic for FluentCV.
|
2015-11-19 06:57:15 +00:00
|
|
|
@license MIT. Copyright (c) 2015 James M. Devlin / FluentDesk
|
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-11-19 14:46:02 +00:00
|
|
|
, FS = require('fs')
|
2015-10-07 08:53:38 +01:00
|
|
|
, _ = require('underscore')
|
2015-10-26 16:30:00 +00:00
|
|
|
, FLUENT = require('./fluentlib')
|
2015-10-26 12:01:01 +00:00
|
|
|
, PATH = require('path')
|
|
|
|
, MKDIRP = require('mkdirp')
|
2015-10-07 14:29:41 +01:00
|
|
|
, rez, _log, _err;
|
2015-09-24 21:09:48 +01:00
|
|
|
|
|
|
|
/**
|
2015-10-07 08:53:38 +01:00
|
|
|
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-07 08:53:38 +01:00
|
|
|
@param dst An array of paths to the target resume file(s).
|
2015-10-10 20:39:13 +01:00
|
|
|
@param theme Friendly name of the resume theme. Defaults to "modern".
|
2015-10-06 17:21:22 +01:00
|
|
|
@param logger Optional logging override.
|
2015-09-24 21:09:48 +01:00
|
|
|
*/
|
2015-10-25 07:04:51 +00:00
|
|
|
function gen( src, dst, opts, logger, errHandler ) {
|
2015-09-24 21:09:48 +01:00
|
|
|
|
2015-09-29 09:15:04 +01:00
|
|
|
_log = logger || console.log;
|
2015-10-07 14:29:41 +01:00
|
|
|
_err = errHandler || error;
|
2015-10-26 06:45:37 +00:00
|
|
|
|
2015-10-25 07:04:51 +00:00
|
|
|
//_opts = extend( true, _opts, opts );
|
|
|
|
_opts.theme = (opts.theme && opts.theme.toLowerCase().trim()) || 'modern';
|
|
|
|
_opts.prettify = opts.prettify === true ? _opts.prettify : false;
|
2015-09-24 21:09:48 +01:00
|
|
|
|
2015-10-07 08:53:38 +01:00
|
|
|
// Load input resumes...
|
2015-10-07 14:29:41 +01:00
|
|
|
if(!src || !src.length) { throw { fluenterror: 3 }; }
|
2015-09-24 21:09:48 +01:00
|
|
|
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 );
|
|
|
|
});
|
|
|
|
|
2015-10-07 08:53:38 +01:00
|
|
|
// Merge input resumes...
|
2015-10-07 14:29:41 +01:00
|
|
|
var msg = '';
|
2015-10-07 08:53:38 +01:00
|
|
|
rez = _.reduceRight( sheets, function( a, b, idx ) {
|
|
|
|
msg += ((idx == sheets.length - 2) ? 'Merging ' + a.meta.fileName : '')
|
|
|
|
+ ' onto ' + b.meta.fileName;
|
|
|
|
return extend( true, b, a );
|
2015-09-24 21:09:48 +01:00
|
|
|
});
|
2015-10-07 08:53:38 +01:00
|
|
|
msg && _log(msg);
|
2015-10-06 21:09:40 +01:00
|
|
|
|
2015-10-26 12:01:01 +00:00
|
|
|
// Load the active theme
|
|
|
|
// Verify the specified theme name/path
|
2015-10-26 17:48:00 +00:00
|
|
|
var tFolder = PATH.resolve( __dirname, '../node_modules/fluent-themes/themes', _opts.theme );
|
2015-10-26 12:01:01 +00:00
|
|
|
var exists = require('./utils/file-exists');
|
|
|
|
if (!exists( tFolder )) {
|
|
|
|
tFolder = PATH.resolve( _opts.theme );
|
|
|
|
if (!exists( tFolder )) {
|
|
|
|
throw { fluenterror: 1, data: _opts.theme };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var theTheme = new FLUENT.Theme().open( tFolder );
|
|
|
|
_opts.themeObj = theTheme;
|
|
|
|
_log( 'Applying ' + theTheme.name.toUpperCase() + ' theme (' + Object.keys(theTheme.formats).length + ' formats)' );
|
|
|
|
|
2015-10-07 08:53:38 +01:00
|
|
|
// Expand output resumes... (can't use map() here)
|
|
|
|
var targets = [];
|
2015-10-26 12:01:01 +00:00
|
|
|
var that = this;
|
2015-10-07 08:53:38 +01:00
|
|
|
( (dst && dst.length && dst) || ['resume.all'] ).forEach( function(t) {
|
|
|
|
var to = path.resolve(t), pa = path.parse(to), fmat = pa.ext || '.all';
|
|
|
|
targets.push.apply(targets, fmat === '.all' ?
|
2015-10-26 12:01:01 +00:00
|
|
|
Object.keys( theTheme.formats ).map(function(k){ var z = theTheme.formats[k]; return { file: to.replace(/all$/g,z.pre), fmt: z } })
|
|
|
|
: [{ file: to, fmt: theTheme.getFormat( fmat.slice(1) ) }]);
|
2015-10-07 08:53:38 +01:00
|
|
|
});
|
2015-09-24 21:09:48 +01:00
|
|
|
|
|
|
|
// Run the transformation!
|
2015-10-26 12:01:01 +00:00
|
|
|
var finished = targets.map( function(t) { return single(t, theTheme); } );
|
2015-09-24 21:09:48 +01:00
|
|
|
|
2015-10-07 08:53:38 +01:00
|
|
|
// Don't send the client back empty-handed
|
|
|
|
return { sheet: rez, targets: targets, processed: finished };
|
2015-09-24 21:09:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
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-26 12:01:01 +00:00
|
|
|
function single( fi, theme ) {
|
2015-09-24 21:09:48 +01:00
|
|
|
try {
|
2015-10-10 22:49:29 +01:00
|
|
|
var f = fi.file, fType = fi.fmt.ext, fName = path.basename( f, '.' + fType );
|
2015-10-26 12:01:01 +00:00
|
|
|
var fObj = _.property( fi.fmt.pre )( theme.formats );
|
|
|
|
var fOut = path.join( f.substring( 0, f.lastIndexOf('.') + 1 ) + fObj.pre );
|
|
|
|
_log( 'Generating ' + fi.fmt.title.toUpperCase() + ' resume: ' + path.relative(process.cwd(), f ) );
|
|
|
|
var theFormat = _fmts.filter( function( fmt ) {
|
|
|
|
return fmt.name === fi.fmt.pre;
|
|
|
|
})[0];
|
|
|
|
MKDIRP( path.dirname(fOut) ); // Ensure dest folder exists; don't bug user
|
|
|
|
theFormat.gen.generate( rez, fOut, _opts );
|
2015-09-24 21:09:48 +01:00
|
|
|
}
|
|
|
|
catch( ex ) {
|
2015-10-07 14:29:41 +01:00
|
|
|
_err( ex );
|
2015-09-24 21:09:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Handle an exception.
|
|
|
|
*/
|
2015-10-07 14:29:41 +01:00
|
|
|
function error( ex ) {
|
|
|
|
throw ex;
|
2015-09-24 21:09:48 +01:00
|
|
|
}
|
|
|
|
|
2015-11-19 14:46:02 +00:00
|
|
|
/**
|
|
|
|
Validate 1 to N resumes as vanilla JSON.
|
|
|
|
*/
|
|
|
|
// function validateAsJSON( src, logger ) {
|
|
|
|
// _log = logger || console.log;
|
|
|
|
// if( !src || !src.length ) { throw { fluenterror: 3 }; }
|
|
|
|
// var isValid = true;
|
|
|
|
// var sheets = src.map( function( res ) {
|
|
|
|
// try {
|
|
|
|
// var rawJson = FS.readFileSync( res, 'utf8' );
|
|
|
|
// var testObj = JSON.parse( rawJson );
|
|
|
|
// }
|
|
|
|
// catch(ex) {
|
|
|
|
// if (!(ex instanceof SyntaxError)) { throw ex; } // [1]
|
|
|
|
// isValid = false;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// _log( 'Validating JSON resume: ' + res + (isValid ? ' (VALID)' : ' (INVALID)'));
|
|
|
|
// return isValid;
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
|
|
|
|
/**
|
|
|
|
Validate 1 to N resumes in either FRESH or JSON Resume format.
|
|
|
|
*/
|
|
|
|
function validate( src, unused, opts, logger ) {
|
|
|
|
_log = logger || console.log;
|
|
|
|
if( !src || !src.length ) { throw { fluenterror: 3 }; }
|
|
|
|
var isValid = true;
|
|
|
|
var sheets = src.map( function( res ) {
|
|
|
|
var sheet = (new FLUENT.Sheet()).open( res );
|
|
|
|
var valid = sheet.isValid();
|
|
|
|
_log( 'Validating JSON resume: ' + res +
|
|
|
|
(valid ? ' (VALID)' : ' (INVALID)'));
|
|
|
|
if( !valid ) {
|
|
|
|
_log( sheet.meta.validationErrors );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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() },
|
2015-10-10 22:49:29 +01:00
|
|
|
{ name: 'pdf', ext: 'pdf', fmt: 'html', is: false, gen: new FLUENT.HtmlPdfGenerator() },
|
2015-10-26 12:01:01 +00:00
|
|
|
{ name: 'md', ext: 'md', fmt: 'txt', gen: new FLUENT.MarkdownGenerator() },
|
2015-10-26 06:45:37 +00:00
|
|
|
{ name: 'json', ext: 'json', gen: new FLUENT.JsonGenerator() },
|
2015-10-26 12:01:01 +00:00
|
|
|
{ name: 'yml', ext: 'yml', fmt: 'yml', gen: new FLUENT.JsonYamlGenerator() }
|
2015-09-24 21:09:48 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2015-11-05 05:56:41 +00:00
|
|
|
Default FluentCV options.
|
2015-09-24 21:09:48 +01:00
|
|
|
*/
|
|
|
|
var _opts = {
|
2015-10-10 20:39:13 +01:00
|
|
|
theme: 'modern',
|
2015-10-25 07:04:51 +00:00
|
|
|
prettify: { // ← See https://github.com/beautify-web/js-beautify#options
|
|
|
|
indent_size: 2,
|
|
|
|
unformatted: ['em','strong'],
|
|
|
|
max_char: 80, // ← See lib/html.js in above-linked repo
|
|
|
|
//wrap_line_length: 120, ← Don't use this
|
|
|
|
}
|
|
|
|
};
|
2015-09-24 21:09:48 +01:00
|
|
|
|
|
|
|
/**
|
2015-10-07 08:53:38 +01:00
|
|
|
Internal module interface. Used by FCV Desktop and HMR.
|
2015-09-24 21:09:48 +01:00
|
|
|
*/
|
|
|
|
return {
|
2015-11-19 14:46:02 +00:00
|
|
|
verbs: {
|
|
|
|
generate: gen,
|
|
|
|
validate: validate,
|
|
|
|
convert: convert
|
|
|
|
},
|
2015-10-26 16:54:27 +00:00
|
|
|
lib: require('./fluentlib'),
|
2015-09-24 21:09:48 +01:00
|
|
|
options: _opts,
|
|
|
|
formats: _fmts
|
|
|
|
};
|
|
|
|
|
|
|
|
}();
|
2015-11-19 14:46:02 +00:00
|
|
|
|
|
|
|
// [1]: JSON.parse throws SyntaxError on invalid JSON. See:
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
|