From 6b0ea0c7bddab028f149bfdaac6e407a26daa9b3 Mon Sep 17 00:00:00 2001 From: Ya Zhuang Date: Tue, 29 Dec 2015 03:29:13 +0800 Subject: [PATCH 1/2] add: png format --- package.json | 1 + src/core/default-formats.js | 2 ++ src/core/theme.js | 2 +- src/eng/handlebars-generator.js | 2 +- src/eng/underscore-generator.js | 2 +- src/gen/html-png-generator.js | 48 +++++++++++++++++++++++++++++++++ src/hackmyapi.js | 3 ++- 7 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 src/gen/html-png-generator.js diff --git a/package.json b/package.json index 025892b..ea6ef34 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,7 @@ "recursive-readdir-sync": "^1.0.6", "simple-html-tokenizer": "^0.2.0", "underscore": "^1.8.3", + "webshot": "^0.16.0", "wkhtmltopdf": "^0.1.5", "xml-escape": "^1.0.0", "yamljs": "^0.2.4" diff --git a/src/core/default-formats.js b/src/core/default-formats.js index a9620bf..ce344c3 100644 --- a/src/core/default-formats.js +++ b/src/core/default-formats.js @@ -10,10 +10,12 @@ { 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() }, + { name: 'png', ext: 'png', fmt: 'html', is: false, gen: new FLUENT.HtmlPngGenerator() }, { name: 'md', ext: 'md', fmt: 'txt', gen: new FLUENT.MarkdownGenerator() }, { name: 'json', ext: 'json', gen: new FLUENT.JsonGenerator() }, { name: 'yml', ext: 'yml', fmt: 'yml', gen: new FLUENT.JsonYamlGenerator() }, { name: 'latex', ext: 'tex', fmt: 'latex', gen: new FLUENT.LaTeXGenerator() } + ]; }()); diff --git a/src/core/theme.js b/src/core/theme.js index 3b5f729..d2ef909 100644 --- a/src/core/theme.js +++ b/src/core/theme.js @@ -102,7 +102,7 @@ Definition of the Theme class. var portion = pathInfo.dirname.replace(tplFolder,''); if( portion && portion.trim() ) { if( portion[1] === '_' ) return; - var reg = /^(?:\/|\\)(html|latex|doc|pdf|partials)(?:\/|\\)?/ig; + var reg = /^(?:\/|\\)(html|latex|doc|pdf|png|partials)(?:\/|\\)?/ig; var res = reg.exec( portion ); if( res ) { if( res[1] !== 'partials' ) { diff --git a/src/eng/handlebars-generator.js b/src/eng/handlebars-generator.js index b244a3f..71dbf58 100644 --- a/src/eng/handlebars-generator.js +++ b/src/eng/handlebars-generator.js @@ -36,7 +36,7 @@ Definition of the HandlebarsGenerator class. // Compile and run the Handlebars template. var template = HANDLEBARS.compile(jst); return template({ - r: format === 'html' || format === 'pdf' ? json.markdownify() : json, + r: format === 'html' || format === 'pdf' || format === 'png' ? json.markdownify() : json, RAW: json, filt: opts.filters, cssInfo: cssInfo, diff --git a/src/eng/underscore-generator.js b/src/eng/underscore-generator.js index fe2fb36..0a9be84 100644 --- a/src/eng/underscore-generator.js +++ b/src/eng/underscore-generator.js @@ -35,7 +35,7 @@ Definition of the UnderscoreGenerator class. // Compile and run the template. TODO: avoid unnecessary recompiles. var compiled = _.template(jst); var ret = compiled({ - r: format === 'html' || format === 'pdf' ? json.markdownify() : json, + r: format === 'html' || format === 'pdf' || format === 'png' ? json.markdownify() : json, filt: opts.filters, XML: require('xml-escape'), RAW: json, diff --git a/src/gen/html-png-generator.js b/src/gen/html-png-generator.js new file mode 100644 index 0000000..3265066 --- /dev/null +++ b/src/gen/html-png-generator.js @@ -0,0 +1,48 @@ +/** +Definition of the HtmlPngGenerator class. +@license MIT. Copyright (c) 2015 James Devlin / FluentDesk. +@module html-png-generator.js +*/ + +(function() { + + var TemplateGenerator = require('./template-generator') + , FS = require('fs-extra') + , HTML = require( 'html' ); + + /** + An HTML-based PDF resume generator for HackMyResume. + */ + var HtmlPngGenerator = module.exports = TemplateGenerator.extend({ + + init: function() { +console.log('png generator init'); + this._super( 'png', 'html' ); + }, + + /** + Generate the binary PDF. + */ + onBeforeSave: function( info ) { +console.log('png generator onBeforeSave'); + png( info.mk, info.outputFile ); + return null; // halt further processing + } + + }); + + /** + Generate a PDF from HTML. + */ + function png( markup, fOut ) { + + console.log('>> #png()'); + console.log(markup); + console.log(fOut); + + require('webshot')( markup , { encoding: 'binary', siteType: 'html' } ) + .pipe( FS.createWriteStream( fOut ) ); + + } + +}()); diff --git a/src/hackmyapi.js b/src/hackmyapi.js index 6d96368..f36c40a 100644 --- a/src/hackmyapi.js +++ b/src/hackmyapi.js @@ -18,5 +18,6 @@ module.exports = { JsonGenerator: require('./gen/json-generator'), YamlGenerator: require('./gen/yaml-generator'), JsonYamlGenerator: require('./gen/json-yaml-generator'), - LaTeXGenerator: require('./gen/latex-generator') + LaTeXGenerator: require('./gen/latex-generator'), + HtmlPngGenerator: require('./gen/html-png-generator') }; From 5dd3d1a3b44e19706a01faaed63434e7a9d3c622 Mon Sep 17 00:00:00 2001 From: Ya Zhuang Date: Tue, 29 Dec 2015 03:40:42 +0800 Subject: [PATCH 2/2] chore: remove debugging console logs --- src/gen/html-png-generator.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/gen/html-png-generator.js b/src/gen/html-png-generator.js index 3265066..4d9b5b9 100644 --- a/src/gen/html-png-generator.js +++ b/src/gen/html-png-generator.js @@ -16,7 +16,6 @@ Definition of the HtmlPngGenerator class. var HtmlPngGenerator = module.exports = TemplateGenerator.extend({ init: function() { -console.log('png generator init'); this._super( 'png', 'html' ); }, @@ -24,7 +23,6 @@ console.log('png generator init'); Generate the binary PDF. */ onBeforeSave: function( info ) { -console.log('png generator onBeforeSave'); png( info.mk, info.outputFile ); return null; // halt further processing } @@ -36,10 +34,6 @@ console.log('png generator onBeforeSave'); */ function png( markup, fOut ) { - console.log('>> #png()'); - console.log(markup); - console.log(fOut); - require('webshot')( markup , { encoding: 'binary', siteType: 'html' } ) .pipe( FS.createWriteStream( fOut ) );