From f02cffd8a5b6417bbee983056d1708caa8e16873 Mon Sep 17 00:00:00 2001 From: Olivier Audard Date: Sun, 14 Oct 2012 02:13:02 +0200 Subject: [PATCH] Fixed compatibility with grunt 0.4 --- package.json | 5 +++- tasks/lib/wkhtmltopdf-lib.js | 49 ++++++++++++++++++++++++++++++++++++ tasks/wkhtmltopdf.js | 42 +++---------------------------- 3 files changed, 56 insertions(+), 40 deletions(-) create mode 100644 tasks/lib/wkhtmltopdf-lib.js diff --git a/package.json b/package.json index 8c9106d..3a8b1c2 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,9 @@ "grunt": "~0.3.9" }, "keywords": [ - "gruntplugin" + "gruntplugin", + "pdf", + "convert", + "html" ] } \ No newline at end of file diff --git a/tasks/lib/wkhtmltopdf-lib.js b/tasks/lib/wkhtmltopdf-lib.js new file mode 100644 index 0000000..a15ef98 --- /dev/null +++ b/tasks/lib/wkhtmltopdf-lib.js @@ -0,0 +1,49 @@ +/* +* grunt-wkhtmltopdf +* http://www.dhar.fr/grunt-wkhtmltopdf/ +* +* Copyright (c) 2012 Olivier Audard, contributors +* Licensed under the MIT license. +*/ +/*globals exports:false*/ + +exports.init = function(grunt) { + 'use strict'; + + return { + convert: function(options) { + + if (!options || !options.args) { + grunt.warn("You need to specify atleast one input file, and exactly one output file"); + return null; + } + + // TODO: ditch this when grunt v0.4 is released + grunt.util = grunt.util || grunt.utils; + + return grunt.util.spawn({ + cmd: 'wkhtmltopdf', + args: options.args + }, function(err, result, code) { + grunt.log.writeln('wkhtmltopdf done'); + if (!err) { return options.done(null); } + // Something went horribly wrong. + grunt.verbose.or.writeln(); + grunt.log.write('Running wkhtmltopdf...').error(); + if (code === 127) { + grunt.log.errorlns( + 'In order for this task to work properly, wkhtmltopdf must be ' + + 'installed and in the system PATH (if you can run "wkhtmltopdf" at' + + ' the command line, this task should work). Unfortunately, ' + + 'wkhtmltopdf cannot be installed automatically via npm or grunt. ' + ); + grunt.warn('wkhtmltopdf not found.', options.code); + } else { + result.split('\n').forEach(grunt.log.error, grunt.log); + grunt.warn('wkhtmltopdf exited unexpectedly with exit code ' + code + '.', options.code); + } + options.done(code); + }); + } + }; +}; \ No newline at end of file diff --git a/tasks/wkhtmltopdf.js b/tasks/wkhtmltopdf.js index c44be6b..ec0e2ec 100644 --- a/tasks/wkhtmltopdf.js +++ b/tasks/wkhtmltopdf.js @@ -5,7 +5,7 @@ * Copyright (c) 2012 Olivier Audard * Licensed under the MIT license. */ -/*globals module:false*/ +/*globals module:false, require:false*/ module.exports = function(grunt) { // Please see the grunt documentation for more information regarding task and @@ -14,6 +14,7 @@ module.exports = function(grunt) { // ========================================================================== // TASKS // ========================================================================== + var helper = require('./lib/wkhtmltopdf-lib').init(grunt); grunt.registerTask('wkhtmltopdf', 'Your task description goes here.', function() { grunt.config.requires('wkhtmltopdf.src'); @@ -35,7 +36,7 @@ module.exports = function(grunt) { srcpath.replace(/.*\/([^\/]+)\.html/, '$1.pdf'); // Launch PhantomJS. - grunt.helper('wkhtmltopdf', { + helper.convert({ code: 90, args: [ '--dpi', '96', // workarround to wkhtmltopdf letter-spacing bug (see http://code.google.com/p/wkhtmltopdf/issues/detail?id=72) @@ -51,41 +52,4 @@ module.exports = function(grunt) { }); }); }); - - // ========================================================================== - // HELPERS - // ========================================================================== - - grunt.registerHelper('wkhtmltopdf', function(options) { - - if (!options || !options.args) { - grunt.warn("You need to specify atleast one input file, and exactly one output file"); - return null; - } - - return grunt.utils.spawn({ - cmd: 'wkhtmltopdf', - args: options.args - }, function(err, result, code) { - grunt.log.writeln('wkhtmltopdf done'); - if (!err) { return options.done(null); } - // Something went horribly wrong. - grunt.verbose.or.writeln(); - grunt.log.write('Running wkhtmltopdf...').error(); - if (code === 127) { - grunt.log.errorlns( - 'In order for this task to work properly, wkhtmltopdf must be ' + - 'installed and in the system PATH (if you can run "wkhtmltopdf" at' + - ' the command line, this task should work). Unfortunately, ' + - 'wkhtmltopdf cannot be installed automatically via npm or grunt. ' - ); - grunt.warn('wkhtmltopdf not found.', options.code); - } else { - result.split('\n').forEach(grunt.log.error, grunt.log); - grunt.warn('wkhtmltopdf exited unexpectedly with exit code ' + code + '.', options.code); - } - options.done(code); - }); - }); - };