From aa0ef4e8a48ca2611f8774f8ba38e5fc41e84cb0 Mon Sep 17 00:00:00 2001 From: devlinjd Date: Thu, 5 Nov 2015 00:57:57 -0500 Subject: [PATCH] Adjust CSS file handling by generators. --- src/gen/html-generator.js | 54 ++++++++------- src/gen/html-pdf-generator.js | 121 +++++++++++++++++----------------- src/gen/template-generator.js | 6 +- 3 files changed, 93 insertions(+), 88 deletions(-) diff --git a/src/gen/html-generator.js b/src/gen/html-generator.js index 1522b70..b0f2992 100644 --- a/src/gen/html-generator.js +++ b/src/gen/html-generator.js @@ -1,32 +1,40 @@ /** HTML resume generator for FluentCV. -@license Copyright (c) 2015 by James M. Devlin. All rights reserved. +@license Copyright (c) 2015 James M. Devlin / FluentDesk */ -var TemplateGenerator = require('./template-generator'); -var FS = require('fs-extra'); -var HTML = require( 'html' ); +(function() { -var HtmlGenerator = module.exports = TemplateGenerator.extend({ + var TemplateGenerator = require('./template-generator') + , FS = require('fs-extra') + , HTML = require( 'html' ) + , PATH = require('path'); - init: function() { - this._super( 'html' ); - }, + var HtmlGenerator = module.exports = TemplateGenerator.extend({ - /** - Generate an HTML resume with optional pretty printing. - */ - onBeforeSave: function( mk, theme, outputFile ) { - var themeFile = theme.getFormat('html').path; - var cssSrc = themeFile.replace( /.html$/g, '.css' ); - var cssDst = outputFile.replace( /.html$/g, '.css' ); - var that = this; - FS.copySync( cssSrc, cssDst, { clobber: true }, function( e ) { - throw { fluenterror: that.codes.copyCss, data: [cssSrc,cssDst] }; - }); + init: function() { + this._super( 'html' ); + }, - return this.opts.prettify ? - HTML.prettyPrint( mk, this.opts.prettify ) : mk; - } + /** + Copy satellite CSS files to the destination and optionally pretty-print + the HTML resume prior to saving. + */ + onBeforeSave: function( info ) { + var cssSrc = PATH.join( info.theme.folder, 'templates', '*.css' ) + , outFolder = PATH.parse( info.outputFile ).dir, that = this; -}); + info.theme.cssFiles.forEach( function( f ) { + var fi = PATH.parse( f[1].path ); + FS.copySync( f[1].path, PATH.join( outFolder, fi.base ), { clobber: true }, function( e ) { + throw { fluenterror: that.codes.copyCss, data: [cssSrc,cssDst] }; + }); + }); + + return this.opts.prettify ? + HTML.prettyPrint( info.mk, this.opts.prettify ) : info.mk; + } + + }); + +}()); diff --git a/src/gen/html-pdf-generator.js b/src/gen/html-pdf-generator.js index 549c787..3c43b1b 100644 --- a/src/gen/html-pdf-generator.js +++ b/src/gen/html-pdf-generator.js @@ -1,74 +1,71 @@ /** -HTML-based PDF resume generator for FluentCV. -@license Copyright (c) 2015 by James M. Devlin. All rights reserved. +Definition of the HtmlPdfGenerator class. +@license Copyright (c) 2015 James M. Devlin / FluentDesk */ -var TemplateGenerator = require('./template-generator'); -var FS = require('fs-extra'); -var HTML = require( 'html' ); +(function() { -var HtmlPdfGenerator = module.exports = TemplateGenerator.extend({ - - init: function() { - this._super( 'pdf', 'html' ); - }, + var TemplateGenerator = require('./template-generator') + , FS = require('fs-extra') + , HTML = require( 'html' ); /** - Generate an HTML resume with optional pretty printing. - TODO: Avoid copying the CSS file to dest if we don't need to. + An HTML-based PDF resume generator for FluentCV. */ - onBeforeSave: function( mk, themeFile, outputFile ) { - // var cssSrc = themeFile.replace( /pdf\.html$/gi, 'html.css' ); - // var cssDst = outputFile.replace( /\.pdf$/gi, '.css' ); - // var that = this; - // FS.copySync( cssSrc, cssDst, { clobber: true }, function( e ) { - // if( e ) that.err( "Couldn't copy CSS file to destination: " + e); - // }); - // return true ? - // HTML.prettyPrint( mk, { indent_size: 2 } ) : mk; + var HtmlPdfGenerator = module.exports = TemplateGenerator.extend({ - pdf(mk, outputFile); - return mk; - } + init: function() { + this._super( 'pdf', 'html' ); + }, -}); - -/** -Generate a PDF from HTML. -*/ -function pdf( markup, fOut ) { - - var pdfCount = 0; - if( false ) { //( _opts.pdf === 'phantom' || _opts.pdf == 'all' ) { - pdfCount++; - require('phantom').create( function( ph ) { - ph.createPage( function( page ) { - page.setContent( markup ); - page.set('paperSize', { - format: 'A4', - orientation: 'portrait', - margin: '1cm' - }); - page.set("viewportSize", { - width: 1024, // TODO: option-ify - height: 768 // TODO: Use "A" sizes - }); - page.set('onLoadFinished', function(success) { - page.render( fOut ); - pdfCount++; - ph.exit(); - }); - }, - { dnodeOpts: { weak: false } } ); - }); - } - if( true ) { // _opts.pdf === 'wkhtmltopdf' || _opts.pdf == 'all' ) { - var fOut2 = fOut; - if( pdfCount == 1 ) { - fOut2 = fOut2.replace(/\.pdf$/g, '.b.pdf'); + /** + Generate the binary PDF. + */ + onBeforeSave: function( info ) { + pdf(info.mk, info.outputFile); + return mk; } - require('wkhtmltopdf')( markup, { pageSize: 'letter' } ) - .pipe( FS.createWriteStream( fOut2 ) ); + + }); + + /** + Generate a PDF from HTML. + */ + function pdf( markup, fOut ) { + + var pdfCount = 0; + if( false ) { //( _opts.pdf === 'phantom' || _opts.pdf == 'all' ) { pdfCount++; + require('phantom').create( function( ph ) { + ph.createPage( function( page ) { + page.setContent( markup ); + page.set('paperSize', { + format: 'A4', + orientation: 'portrait', + margin: '1cm' + }); + page.set("viewportSize", { + width: 1024, // TODO: option-ify + height: 768 // TODO: Use "A" sizes + }); + page.set('onLoadFinished', function(success) { + page.render( fOut ); + pdfCount++; + ph.exit(); + }); + }, + { dnodeOpts: { weak: false } } ); + }); + } + if( true ) { // _opts.pdf === 'wkhtmltopdf' || _opts.pdf == 'all' ) { + var fOut2 = fOut; + if( pdfCount == 1 ) { + fOut2 = fOut2.replace(/\.pdf$/g, '.b.pdf'); + } + require('wkhtmltopdf')( markup, { pageSize: 'letter' } ) + .pipe( FS.createWriteStream( fOut2 ) ); + pdfCount++; + } } -} + +}()); diff --git a/src/gen/template-generator.js b/src/gen/template-generator.js index d9a7833..2e5e00f 100644 --- a/src/gen/template-generator.js +++ b/src/gen/template-generator.js @@ -89,11 +89,11 @@ var TemplateGenerator = module.exports = BaseGenerator.extend({ // Load theme and CSS data var tplFolder = PATH.join( tFolder, 'templates' ); var curFmt = theme.getFormat( this.format ); - var ctx = { 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! - var mk = this.single( rez, curFmt.data, this.format, ctx, opts ); - this.onBeforeSave && (mk = this.onBeforeSave( mk, theme, f )); + var mk = this.single( rez, curFmt.data, this.format, cssInfo, opts ); + this.onBeforeSave && (mk = this.onBeforeSave( { mk: mk, theme: theme, outputFile: f } )); FS.writeFileSync( f, mk, { encoding: 'utf8', flags: 'w' } ); },