Merge pull request #8 from starim/grunt-0.4
Update grunt-wkhtmltopdf with work with Grunt 0.4
This commit is contained in:
commit
7344889930
3 changed files with 66 additions and 37 deletions
|
@ -1,12 +1,14 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
module.exports = function(grunt) {
|
module.exports = function(grunt) {
|
||||||
|
|
||||||
|
// Load grunt tasks automatically
|
||||||
|
require('load-grunt-tasks')(grunt);
|
||||||
|
|
||||||
// Project configuration.
|
// Project configuration.
|
||||||
grunt.initConfig({
|
grunt.initConfig({
|
||||||
test: {
|
nodeunit: {
|
||||||
files: ['test/**/*.js']
|
all: ['test/**/_test.js']
|
||||||
},
|
|
||||||
lint: {
|
|
||||||
files: ['grunt.js', 'tasks/**/*.js', 'test/**/*.js']
|
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
files: '<config:lint.files>',
|
files: '<config:lint.files>',
|
||||||
|
@ -25,16 +27,19 @@ module.exports = function(grunt) {
|
||||||
boss: true,
|
boss: true,
|
||||||
eqnull: true,
|
eqnull: true,
|
||||||
node: true,
|
node: true,
|
||||||
es5: true
|
|
||||||
},
|
},
|
||||||
globals: {}
|
all: [
|
||||||
|
'Gruntfile.js',
|
||||||
|
'tasks/{,*/}*.js',
|
||||||
|
'test/{,*/}*.js',
|
||||||
|
]
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Load local tasks.
|
|
||||||
grunt.loadTasks('tasks');
|
|
||||||
|
|
||||||
// Default task.
|
// Default task.
|
||||||
grunt.registerTask('default', 'lint test');
|
grunt.registerTask('default', [
|
||||||
|
'jshint',
|
||||||
|
'nodeunit:all'
|
||||||
|
]);
|
||||||
|
|
||||||
};
|
};
|
|
@ -30,10 +30,13 @@
|
||||||
"test": "grunt test"
|
"test": "grunt test"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"grunt": "~0.3.9"
|
"grunt": "~0.4.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"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": [
|
"keywords": [
|
||||||
"gruntplugin",
|
"gruntplugin",
|
||||||
|
|
|
@ -18,18 +18,38 @@ module.exports = function(grunt) {
|
||||||
|
|
||||||
grunt.registerMultiTask('wkhtmltopdf', 'Your task description goes here.', function() {
|
grunt.registerMultiTask('wkhtmltopdf', 'Your task description goes here.', function() {
|
||||||
|
|
||||||
var htmlFiles = grunt.file.expandFiles(this.file.src),
|
this.files.forEach(function(file) {
|
||||||
dest = (this.file.dest && this.file.dest !== '') ? this.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);
|
||||||
|
|
||||||
htmlFiles.forEach(function(srcpath) {
|
file.src.forEach(function(src) {
|
||||||
var dir = dest + srcpath.replace(/.*\/([^\/]+)\/[^\/]+\.html/, '$1');
|
|
||||||
|
|
||||||
// Create dest folder as wkhtmltopdf won't generate output if it doesn't exist
|
var dest = file.dest;
|
||||||
grunt.file.mkdir(dir);
|
// wkhtmltopdf seems to require that the destination be a file
|
||||||
var destpath = dir + '/' +
|
// location, not a directory, so if the given destination is a
|
||||||
srcpath.replace(/.*\/([^\/]+)\.html/, '$1.pdf');
|
// 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.
|
// Launch PhantomJS.
|
||||||
helper.convert({
|
helper.convert({
|
||||||
|
@ -37,8 +57,8 @@ module.exports = function(grunt) {
|
||||||
args: [
|
args: [
|
||||||
'--dpi', '96', // workarround to wkhtmltopdf letter-spacing bug (see http://code.google.com/p/wkhtmltopdf/issues/detail?id=72)
|
'--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
|
'--print-media-type', // Use @print media type
|
||||||
srcpath,
|
src,
|
||||||
destpath
|
dest
|
||||||
],
|
],
|
||||||
done: function(err) {
|
done: function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -48,4 +68,5 @@ module.exports = function(grunt) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue