From 847b3f3697d735b5028d820a06168020d6f766af Mon Sep 17 00:00:00 2001 From: Brent Houghton Date: Mon, 9 Jun 2014 13:43:41 -0700 Subject: [PATCH 1/4] Update project config files to work with Grunt 0.4. --- grunt.js => Gruntfile.js | 27 ++++++++++++++++----------- package.json | 5 ++++- 2 files changed, 20 insertions(+), 12 deletions(-) rename grunt.js => Gruntfile.js (60%) diff --git a/grunt.js b/Gruntfile.js similarity index 60% rename from grunt.js rename to Gruntfile.js index 008da40..ccdf7bd 100644 --- a/grunt.js +++ b/Gruntfile.js @@ -1,12 +1,14 @@ +'use strict'; + module.exports = function(grunt) { + // Load grunt tasks automatically + require('load-grunt-tasks')(grunt); + // Project configuration. grunt.initConfig({ - test: { - files: ['test/**/*.js'] - }, - lint: { - files: ['grunt.js', 'tasks/**/*.js', 'test/**/*.js'] + nodeunit: { + all: ['test/**/_test.js'] }, watch: { files: '', @@ -25,16 +27,19 @@ module.exports = function(grunt) { boss: true, eqnull: true, node: true, - es5: true }, - globals: {} + all: [ + 'Gruntfile.js', + 'tasks/{,*/}*.js', + 'test/{,*/}*.js', + ] } }); - // Load local tasks. - grunt.loadTasks('tasks'); - // Default task. - grunt.registerTask('default', 'lint test'); + grunt.registerTask('default', [ + 'jshint', + 'nodeunit:all' + ]); }; diff --git a/package.json b/package.json index a677a37..ce0ae41 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,10 @@ "grunt": "~0.3.9" }, "devDependencies": { - "grunt": "~0.3.9" + "grunt": "~0.4.1", + "grunt-contrib-jshint": "^0.10.0", + "grunt-contrib-nodeunit": "^0.4.0", + "load-grunt-tasks": "^0.4.0" }, "keywords": [ "gruntplugin", From 7f9813423fbbff0fc9db522989c698bdfffc597e Mon Sep 17 00:00:00 2001 From: Brent Houghton Date: Mon, 9 Jun 2014 14:40:24 -0700 Subject: [PATCH 2/4] Update wkthmltopdf task to use Grunt 0.4 multi-task API. --- tasks/wkhtmltopdf.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tasks/wkhtmltopdf.js b/tasks/wkhtmltopdf.js index ee673be..c28506c 100644 --- a/tasks/wkhtmltopdf.js +++ b/tasks/wkhtmltopdf.js @@ -18,12 +18,12 @@ module.exports = function(grunt) { grunt.registerMultiTask('wkhtmltopdf', 'Your task description goes here.', function() { - var htmlFiles = grunt.file.expandFiles(this.file.src), - dest = (this.file.dest && this.file.dest !== '') ? this.file.dest + '/' : ''; + this.files.forEach(function(file) { + var srcpath = file.src.toString(), + dest = (file.dest && file.dest !== '') ? file.dest + '/' : ''; - grunt.log.writeln("pdf output is: " + dest); + grunt.log.writeln("pdf output is: " + dest); - htmlFiles.forEach(function(srcpath) { var dir = dest + srcpath.replace(/.*\/([^\/]+)\/[^\/]+\.html/, '$1'); // Create dest folder as wkhtmltopdf won't generate output if it doesn't exist From 469ff6bb422abed639d9281f2b68a8fb885e0d0d Mon Sep 17 00:00:00 2001 From: Brent Houghton Date: Thu, 12 Jun 2014 10:26:59 -0700 Subject: [PATCH 3/4] Update a lingering reference to Grunt 0.3. --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index ce0ae41..925f2a0 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "test": "grunt test" }, "dependencies": { - "grunt": "~0.3.9" + "grunt": "~0.4.1" }, "devDependencies": { "grunt": "~0.4.1", @@ -44,4 +44,4 @@ "convert", "html" ] -} \ No newline at end of file +} From dad2687587ad619a29dd4f34eafb1c0c59c4fd49 Mon Sep 17 00:00:00 2001 From: Brent Houghton Date: Thu, 12 Jun 2014 11:40:03 -0700 Subject: [PATCH 4/4] Improve predictability of the destination for created PDFs. Now generated PDFs are placed at the destination specified in the end-users's Gruntfile, rather than appending the source file's path to the destination path. The created PDFs have the same name as the source HTML file, excep the file extension is changed to .pdf. --- tasks/wkhtmltopdf.js | 65 +++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/tasks/wkhtmltopdf.js b/tasks/wkhtmltopdf.js index c28506c..6f6dc05 100644 --- a/tasks/wkhtmltopdf.js +++ b/tasks/wkhtmltopdf.js @@ -19,32 +19,53 @@ module.exports = function(grunt) { grunt.registerMultiTask('wkhtmltopdf', 'Your task description goes here.', function() { this.files.forEach(function(file) { - var srcpath = file.src.toString(), - dest = (file.dest && file.dest !== '') ? file.dest + '/' : ''; - grunt.log.writeln("pdf output is: " + dest); + var pathlib = require('path'); + // calculate the destination directory and ensure it exists, since + // wkhtmltopdf won't create the PDF if the destination directory doesn't + // exist + var destPath = file.dest; + if (grunt.file.isFile(file.dest)) { + destPath = pathlib.dirname(file.dest); + } + grunt.file.mkdir(destPath); - var dir = dest + srcpath.replace(/.*\/([^\/]+)\/[^\/]+\.html/, '$1'); + file.src.forEach(function(src) { - // Create dest folder as wkhtmltopdf won't generate output if it doesn't exist - grunt.file.mkdir(dir); - var destpath = dir + '/' + - srcpath.replace(/.*\/([^\/]+)\.html/, '$1.pdf'); - - // Launch PhantomJS. - helper.convert({ - code: 90, - args: [ - '--dpi', '96', // workarround to wkhtmltopdf letter-spacing bug (see http://code.google.com/p/wkhtmltopdf/issues/detail?id=72) - '--print-media-type', // Use @print media type - srcpath, - destpath - ], - done: function(err) { - if (err) { - grunt.log('>>>', err); - } + var dest = file.dest; + // wkhtmltopdf seems to require that the destination be a file + // location, not a directory, so if the given destination is a + // directory then append the name of the source file but with the + // extension changed to .pdf + if (grunt.file.isDir(dest)) { + var srcFileName = pathlib.basename(src); + var srcFileExtension = pathlib.extname(src); + var destFileName = srcFileName.replace( + new RegExp(srcFileExtension + "$"), // match only the end of the string + ".pdf" + ); + dest = pathlib.join(destPath + destFileName); } + + grunt.log.writeln( + "Converting " + src + " -> " + dest + ); + + // Launch PhantomJS. + helper.convert({ + code: 90, + args: [ + '--dpi', '96', // workarround to wkhtmltopdf letter-spacing bug (see http://code.google.com/p/wkhtmltopdf/issues/detail?id=72) + '--print-media-type', // Use @print media type + src, + dest + ], + done: function(err) { + if (err) { + grunt.log('>>>', err); + } + } + }); }); }); });