Skip to content

Commit aa0ef4e

Browse files
committed
Adjust CSS file handling by generators.
1 parent 537bd4a commit aa0ef4e

3 files changed

Lines changed: 97 additions & 92 deletions

File tree

src/gen/html-generator.js

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
11
/**
22
HTML resume generator for FluentCV.
3-
@license Copyright (c) 2015 by James M. Devlin. All rights reserved.
3+
@license Copyright (c) 2015 James M. Devlin / FluentDesk
44
*/
55

6-
var TemplateGenerator = require('./template-generator');
7-
var FS = require('fs-extra');
8-
var HTML = require( 'html' );
9-
10-
var HtmlGenerator = module.exports = TemplateGenerator.extend({
11-
12-
init: function() {
13-
this._super( 'html' );
14-
},
15-
16-
/**
17-
Generate an HTML resume with optional pretty printing.
18-
*/
19-
onBeforeSave: function( mk, theme, outputFile ) {
20-
var themeFile = theme.getFormat('html').path;
21-
var cssSrc = themeFile.replace( /.html$/g, '.css' );
22-
var cssDst = outputFile.replace( /.html$/g, '.css' );
23-
var that = this;
24-
FS.copySync( cssSrc, cssDst, { clobber: true }, function( e ) {
25-
throw { fluenterror: that.codes.copyCss, data: [cssSrc,cssDst] };
26-
});
27-
28-
return this.opts.prettify ?
29-
HTML.prettyPrint( mk, this.opts.prettify ) : mk;
30-
}
31-
32-
});
6+
(function() {
7+
8+
var TemplateGenerator = require('./template-generator')
9+
, FS = require('fs-extra')
10+
, HTML = require( 'html' )
11+
, PATH = require('path');
12+
13+
var HtmlGenerator = module.exports = TemplateGenerator.extend({
14+
15+
init: function() {
16+
this._super( 'html' );
17+
},
18+
19+
/**
20+
Copy satellite CSS files to the destination and optionally pretty-print
21+
the HTML resume prior to saving.
22+
*/
23+
onBeforeSave: function( info ) {
24+
var cssSrc = PATH.join( info.theme.folder, 'templates', '*.css' )
25+
, outFolder = PATH.parse( info.outputFile ).dir, that = this;
26+
27+
info.theme.cssFiles.forEach( function( f ) {
28+
var fi = PATH.parse( f[1].path );
29+
FS.copySync( f[1].path, PATH.join( outFolder, fi.base ), { clobber: true }, function( e ) {
30+
throw { fluenterror: that.codes.copyCss, data: [cssSrc,cssDst] };
31+
});
32+
});
33+
34+
return this.opts.prettify ?
35+
HTML.prettyPrint( info.mk, this.opts.prettify ) : info.mk;
36+
}
37+
38+
});
39+
40+
}());

src/gen/html-pdf-generator.js

Lines changed: 58 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,71 @@
11
/**
2-
HTML-based PDF resume generator for FluentCV.
3-
@license Copyright (c) 2015 by James M. Devlin. All rights reserved.
2+
Definition of the HtmlPdfGenerator class.
3+
@license Copyright (c) 2015 James M. Devlin / FluentDesk
44
*/
55

6-
var TemplateGenerator = require('./template-generator');
7-
var FS = require('fs-extra');
8-
var HTML = require( 'html' );
6+
(function() {
97

10-
var HtmlPdfGenerator = module.exports = TemplateGenerator.extend({
11-
12-
init: function() {
13-
this._super( 'pdf', 'html' );
14-
},
8+
var TemplateGenerator = require('./template-generator')
9+
, FS = require('fs-extra')
10+
, HTML = require( 'html' );
1511

1612
/**
17-
Generate an HTML resume with optional pretty printing.
18-
TODO: Avoid copying the CSS file to dest if we don't need to.
13+
An HTML-based PDF resume generator for FluentCV.
1914
*/
20-
onBeforeSave: function( mk, themeFile, outputFile ) {
21-
// var cssSrc = themeFile.replace( /pdf\.html$/gi, 'html.css' );
22-
// var cssDst = outputFile.replace( /\.pdf$/gi, '.css' );
23-
// var that = this;
24-
// FS.copySync( cssSrc, cssDst, { clobber: true }, function( e ) {
25-
// if( e ) that.err( "Couldn't copy CSS file to destination: " + e);
26-
// });
27-
// return true ?
28-
// HTML.prettyPrint( mk, { indent_size: 2 } ) : mk;
15+
var HtmlPdfGenerator = module.exports = TemplateGenerator.extend({
2916

30-
pdf(mk, outputFile);
31-
return mk;
32-
}
17+
init: function() {
18+
this._super( 'pdf', 'html' );
19+
},
3320

34-
});
21+
/**
22+
Generate the binary PDF.
23+
*/
24+
onBeforeSave: function( info ) {
25+
pdf(info.mk, info.outputFile);
26+
return mk;
27+
}
3528

36-
/**
37-
Generate a PDF from HTML.
38-
*/
39-
function pdf( markup, fOut ) {
29+
});
4030

41-
var pdfCount = 0;
42-
if( false ) { //( _opts.pdf === 'phantom' || _opts.pdf == 'all' ) {
43-
pdfCount++;
44-
require('phantom').create( function( ph ) {
45-
ph.createPage( function( page ) {
46-
page.setContent( markup );
47-
page.set('paperSize', {
48-
format: 'A4',
49-
orientation: 'portrait',
50-
margin: '1cm'
51-
});
52-
page.set("viewportSize", {
53-
width: 1024, // TODO: option-ify
54-
height: 768 // TODO: Use "A" sizes
55-
});
56-
page.set('onLoadFinished', function(success) {
57-
page.render( fOut );
58-
pdfCount++;
59-
ph.exit();
60-
});
61-
},
62-
{ dnodeOpts: { weak: false } } );
63-
});
64-
}
65-
if( true ) { // _opts.pdf === 'wkhtmltopdf' || _opts.pdf == 'all' ) {
66-
var fOut2 = fOut;
67-
if( pdfCount == 1 ) {
68-
fOut2 = fOut2.replace(/\.pdf$/g, '.b.pdf');
69-
}
70-
require('wkhtmltopdf')( markup, { pageSize: 'letter' } )
71-
.pipe( FS.createWriteStream( fOut2 ) );
31+
/**
32+
Generate a PDF from HTML.
33+
*/
34+
function pdf( markup, fOut ) {
35+
36+
var pdfCount = 0;
37+
if( false ) { //( _opts.pdf === 'phantom' || _opts.pdf == 'all' ) {
7238
pdfCount++;
39+
require('phantom').create( function( ph ) {
40+
ph.createPage( function( page ) {
41+
page.setContent( markup );
42+
page.set('paperSize', {
43+
format: 'A4',
44+
orientation: 'portrait',
45+
margin: '1cm'
46+
});
47+
page.set("viewportSize", {
48+
width: 1024, // TODO: option-ify
49+
height: 768 // TODO: Use "A" sizes
50+
});
51+
page.set('onLoadFinished', function(success) {
52+
page.render( fOut );
53+
pdfCount++;
54+
ph.exit();
55+
});
56+
},
57+
{ dnodeOpts: { weak: false } } );
58+
});
59+
}
60+
if( true ) { // _opts.pdf === 'wkhtmltopdf' || _opts.pdf == 'all' ) {
61+
var fOut2 = fOut;
62+
if( pdfCount == 1 ) {
63+
fOut2 = fOut2.replace(/\.pdf$/g, '.b.pdf');
64+
}
65+
require('wkhtmltopdf')( markup, { pageSize: 'letter' } )
66+
.pipe( FS.createWriteStream( fOut2 ) );
67+
pdfCount++;
68+
}
7369
}
74-
}
70+
71+
}());

src/gen/template-generator.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ var TemplateGenerator = module.exports = BaseGenerator.extend({
8989
// Load theme and CSS data
9090
var tplFolder = PATH.join( tFolder, 'templates' );
9191
var curFmt = theme.getFormat( this.format );
92-
var ctx = { file: curFmt.css ? curFmt.cssPath : null, data: curFmt.css || null };
92+
var cssInfo = { file: curFmt.css ? curFmt.cssPath : null, data: curFmt.css || null };
9393

9494
// Compile and invoke the template!
95-
var mk = this.single( rez, curFmt.data, this.format, ctx, opts );
96-
this.onBeforeSave && (mk = this.onBeforeSave( mk, theme, f ));
95+
var mk = this.single( rez, curFmt.data, this.format, cssInfo, opts );
96+
this.onBeforeSave && (mk = this.onBeforeSave( { mk: mk, theme: theme, outputFile: f } ));
9797
FS.writeFileSync( f, mk, { encoding: 'utf8', flags: 'w' } );
9898

9999
},

0 commit comments

Comments
 (0)