1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-07-05 09:30:04 +01:00

Refactor generator logic.

This commit is contained in:
hacksalot 2015-12-30 18:52:41 -05:00
parent d901047043
commit 558a321fe8
3 changed files with 77 additions and 40 deletions

View File

@ -47,7 +47,7 @@
"dependencies": { "dependencies": {
"colors": "^1.1.2", "colors": "^1.1.2",
"copy": "^0.1.3", "copy": "^0.1.3",
"fresh-themes": "~0.9.0-beta", "fresh-themes": "~0.9.2-beta",
"fresca": "~0.2.2", "fresca": "~0.2.2",
"fs-extra": "^0.24.0", "fs-extra": "^0.24.0",
"handlebars": "^4.0.5", "handlebars": "^4.0.5",

View File

@ -1,6 +1,6 @@
/** /**
Definition of the HtmlPngGenerator class. Definition of the HtmlPngGenerator class.
@license MIT. Copyright (c) 2015 James Devlin / FluentDesk. @license MIT. See LICENSE.MD for details.
@module html-png-generator.js @module html-png-generator.js
*/ */
@ -11,7 +11,7 @@ Definition of the HtmlPngGenerator class.
, HTML = require( 'html' ); , HTML = require( 'html' );
/** /**
An HTML-based PDF resume generator for HackMyResume. An HTML-based PNG resume generator for HackMyResume.
*/ */
var HtmlPngGenerator = module.exports = TemplateGenerator.extend({ var HtmlPngGenerator = module.exports = TemplateGenerator.extend({
@ -19,24 +19,29 @@ Definition of the HtmlPngGenerator class.
this._super( 'png', 'html' ); this._super( 'png', 'html' );
}, },
/** invoke: function( rez, themeMarkup, cssInfo, opts ) {
Generate the binary PDF. //return YAML.stringify( JSON.parse( rez.stringify() ), Infinity, 2 );
*/ },
onBeforeSave: function( info ) {
png( info.mk, info.outputFile ); generate: function( rez, f, opts ) {
return null; // halt further processing var htmlResults = opts.targets.filter(function(t){
return t.fmt.outFormat === 'html';
});
var htmlFile = htmlResults[0].final.files.filter(function(fl){
return fl.info.ext === 'html';
});
png(htmlFile[0].data, f);
} }
}); });
/** /**
Generate a PDF from HTML. Generate a PNG from HTML.
*/ */
function png( markup, fOut ) { function png( markup, fOut ) {
// require('webshot')( markup , { encoding: 'binary', siteType: 'html' } )
require('webshot')( markup , { encoding: 'binary', siteType: 'html' } ) // .pipe( FS.createWriteStream( fOut ) );
.pipe( FS.createWriteStream( fOut ) ); require('webshot')( markup , fOut, { siteType: 'html' }, function(err) { } );
} }
}()); }());

View File

@ -6,6 +6,8 @@ Implementation of the 'generate' verb for HackMyResume.
(function() { (function() {
var PATH = require('path') var PATH = require('path')
, FS = require('fs') , FS = require('fs')
, MD = require('marked') , MD = require('marked')
@ -70,22 +72,23 @@ Implementation of the 'generate' verb for HackMyResume.
var targets = expand( dst, theTheme ); var targets = expand( dst, theTheme );
// Run the transformation! // Run the transformation!
var finished = targets.map( function(t) { targets.forEach( function(t) {
return EXTEND(true, t, { markup: single(t, theTheme) }); t.final = single( t, theTheme, targets );
}); });
// Don't send the client back empty-handed // Don't send the client back empty-handed
return { sheet: rez, targets: targets, processed: finished }; return { sheet: rez, targets: targets, processed: targets };
} }
/** /**
Generate a single resume of a specific format. TODO: Refactor. Generate a single target resume such as "out/rez.html" or "out/rez.doc".
@param targInfo Information for the target resume. @param targInfo Information for the target resume.
@param theme A FRESHTheme or JRSTheme object. @param theme A FRESHTheme or JRSTheme object.
@returns
*/ */
function single( targInfo, theme ) { function single( targInfo, theme, finished ) {
function MDIN(txt) { // TODO: Move this function MDIN(txt) { // TODO: Move this
return MD(txt || '' ).replace(/^\s*<p>|<\/p>\s*$/gi, ''); return MD(txt || '' ).replace(/^\s*<p>|<\/p>\s*$/gi, '');
@ -107,7 +110,8 @@ Implementation of the 'generate' verb for HackMyResume.
theFormat = _fmts.filter( theFormat = _fmts.filter(
function(fmt) { return fmt.name === targInfo.fmt.outFormat; })[0]; function(fmt) { return fmt.name === targInfo.fmt.outFormat; })[0];
MKDIRP.sync( PATH.dirname( f ) ); // Ensure dest folder exists; MKDIRP.sync( PATH.dirname( f ) ); // Ensure dest folder exists;
theFormat.gen.generate( rez, f, _opts ); _opts.targets = finished;
return theFormat.gen.generate( rez, f, _opts );
} }
// Otherwise this is either a) a JSON Resume theme or b) an ad-hoc format // Otherwise this is either a) a JSON Resume theme or b) an ad-hoc format
@ -166,29 +170,67 @@ Implementation of the 'generate' verb for HackMyResume.
} }
/** /**
Expand output files. Expand output files. For example, "foo.all" should be expanded to
["foo.html", "foo.doc", "foo.pdf", "etc"].
@param dst An array of output files as specified by the user.
@param theTheme A FRESHTheme or JRSTheme object.
*/ */
function expand( dst, theTheme ) { function expand( dst, theTheme ) {
var targets = [];
// (can't use map() here).
( (dst && dst.length && dst) || ['resume.all'] ).forEach( function(t) {
var to = PATH.resolve(t), // Add freebie formats (JSON, YAML, PNG) every theme gets...
pa = parsePath(to), // Add HTML-driven PNG only if the theme has an HTML format.
fmat = pa.extname || '.all'; theTheme.formats.json = theTheme.formats.json || {
freebie: true, title: 'json', outFormat: 'json', pre: 'json',
ext: 'json', path: null, data: null
};
theTheme.formats.yml = theTheme.formats.yml || {
freebie: true, title: 'yaml', outFormat: 'yml', pre: 'yml',
ext: 'yml', path: null, data: null
};
theTheme.formats.png = theTheme.formats.png ||
( theTheme.formats.html && {
freebie: true, title: 'png', outFormat: 'png',
ext: 'yml', path: null, data: null
});
// Set up the destination collection. It's either the array of files passed
// by the user or 'out/resume.all' if no targets were specified.
var destColl = (dst && dst.length && dst) ||
[PATH.normalize('out/resume.all')]
// Assemble an array of expanded target files... (can't use map() here)
var targets = [];
destColl.forEach( function(t) {
var to = PATH.resolve(t), pa = parsePath(to),fmat = pa.extname || '.all';
var explicitFormats = _.omit( theTheme.formats, function(val) {
return !val.freebie;
});
var implicitFormats = _.omit( theTheme.formats, function(val) {
return val.freebie;
});
targets.push.apply( targets.push.apply(
targets, fmat === '.all' ? targets, fmat === '.all' ?
Object.keys( implicitFormats ).map( function( k ) {
Object.keys( theTheme.formats ).map(function(k){
var z = theTheme.formats[k]; var z = theTheme.formats[k];
return { file: to.replace(/all$/g,z.outFormat), fmt: z }; return { file: to.replace( /all$/g, z.outFormat ), fmt: z };
}) : }) :
[{ file: to, fmt: theTheme.getFormat( fmat.slice(1) ) }]);
targets.push.apply(
targets, fmat === '.all' ?
Object.keys( explicitFormats ).map( function( k ) {
var z = theTheme.formats[k];
return { file: to.replace( /all$/g, z.outFormat ), fmt: z };
}) :
[{ file: to, fmt: theTheme.getFormat( fmat.slice(1) ) }]); [{ file: to, fmt: theTheme.getFormat( fmat.slice(1) ) }]);
}); });
return targets; return targets;
} }
@ -223,16 +265,6 @@ Implementation of the 'generate' verb for HackMyResume.
var theTheme = _opts.theme.indexOf('jsonresume-theme-') > -1 ? var theTheme = _opts.theme.indexOf('jsonresume-theme-') > -1 ?
new JRSTheme().open(tFolder) : new FluentTheme().open( tFolder ); new JRSTheme().open(tFolder) : new FluentTheme().open( tFolder );
// Add freebie formats every theme gets
theTheme.formats.json = theTheme.formats.json || {
title: 'json', outFormat: 'json', pre: 'json',
ext: 'json', path: null, data: null
};
theTheme.formats.yml = theTheme.formats.yml || {
title: 'yaml', outFormat: 'yml', pre: 'yml',
ext: 'yml', path: null, data: null
};
// Cache the theme object // Cache the theme object
_opts.themeObj = theTheme; _opts.themeObj = theTheme;