Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add file array format + README

  • Loading branch information...
commit b93f4ada23be4474ef5b0a62074994f0fc569193 1 parent 8c82cdb
@dbushell authored
Showing with 67 additions and 17 deletions.
  1. +3 −1 Gruntfile.js
  2. +26 −0 README.md
  3. +33 −11 tasks/svg2png.js
  4. +5 −5 test/svg2png_test.js
View
4 Gruntfile.js
@@ -34,7 +34,9 @@ module.exports = function(grunt)
svg2png: {
all: {
- files: 'test/**/*.svg'
+ files: [
+ { src: ['test/**/*.svg'], dest: 'test/png/' }
+ ]
}
}
View
26 README.md
@@ -3,6 +3,32 @@ grunt-svg2png
Grunt plugin to rasterize SVG to PNG images using PhantomJS
+This plugin requires (Grunt ~0.4.1)[http://gruntjs.com/]
+
+## Example Usage
+
+````javascript
+// Gruntfile.js configuration
+
+grunt.loadNpmTasks('grunt-svg2png');
+
+grunt.initConfig({
+
+ svg2png: {
+ all: {
+ // specify files in array format with multiple src-dest mapping
+ files: [
+ // rasterize all SVG files in "img" and its subdirectories to "img/png"
+ { src: ['img/**/*.svg'], dest: 'img/png/' },
+ // rasterize SVG file to same directory
+ { src: ['img/logo.svg'] },
+ ]
+ }
+ }
+
+});
+````
+
* * *
## Authors
View
44 tasks/svg2png.js
@@ -8,7 +8,8 @@
'use strict';
-var phantomjs = require('phantomjs');
+var phantomjs = require('phantomjs'),
+ path = require('path');
module.exports = function(grunt)
{
@@ -16,12 +17,36 @@ module.exports = function(grunt)
{
var done = this.async(),
- files = grunt.file.expand(this.data.files),
- total = files.length,
start = new Date(),
processed = 0,
completed = 0,
- maxspawns = 10;
+ maxspawns = 10,
+ files = [],
+ total = 0;
+
+ this.data.files.forEach(function(fset)
+ {
+ var svg = grunt.file.expand(fset.src);
+
+ svg.forEach(function(svg)
+ {
+ var src = path.resolve(svg),
+ dest;
+
+ if (fset.dest) {
+ dest = path.resolve(fset.dest) + '/' + path.basename(svg);
+ } else {
+ dest = src;
+ }
+
+ files.push({
+ src: src,
+ dest: dest.replace(/\.svg$/i, '.png')
+ });
+ });
+
+ total = files.length;
+ });
grunt.log.subhead('Rasterizing SVG to PNG (' + files.length + ' files)...');
@@ -42,26 +67,23 @@ module.exports = function(grunt)
var next = function()
{
- var inputFile = files[processed++],
- outputFile = (inputFile || '').replace(/\.svg$/i, '.png');
-
- if (!inputFile) {
+ if (!files[processed]) {
return;
}
-
grunt.util.spawn(
{
cmd: phantomjs.path,
args: [
'tasks/lib/svg2png.js',
- inputFile,
- outputFile
+ files[processed].src,
+ files[processed].dest
]
},
function(error, result, code) {
onComplete();
}
);
+ processed++;
};
var styles = {
View
10 test/svg2png_test.js
@@ -16,7 +16,7 @@ exports.svg2png = {
test1: function(test)
{
- var all = fs.readdirSync('test/svg'),
+ var all = fs.readdirSync('test/png'),
png = [];
if (all instanceof Array) {
@@ -33,7 +33,7 @@ exports.svg2png = {
test2: function(test)
{
- fs.stat('test/svg/grunt-logo.png', function(err, stats)
+ fs.stat('test/png/grunt-logo.png', function(err, stats)
{
test.ok(err === null && stats.isFile(), 'PNG "grunt-logo.png" should exist');
test.done();
@@ -42,7 +42,7 @@ exports.svg2png = {
test3: function(test)
{
- fs.stat('test/svg/orange-square.png', function(err, stats)
+ fs.stat('test/png/orange-square.png', function(err, stats)
{
test.ok(err === null && stats.isFile(), 'PNG "orange-square.png" should exist');
test.done();
@@ -51,7 +51,7 @@ exports.svg2png = {
test4: function(test)
{
- fs.stat('test/svg/grunt-logo.png', function(err, stats)
+ fs.stat('test/png/grunt-logo.png', function(err, stats)
{
test.ok(err === null && stats.size > 44000 && stats.size < 56000, 'PNG "grunt-logo.png" should be ~45kb in size');
test.done();
@@ -61,7 +61,7 @@ exports.svg2png = {
test5: function(test)
{
var png = new pngjs.PNG({ filterType: 4 }),
- stream = fs.createReadStream('test/svg/orange-square.png').pipe(png);
+ stream = fs.createReadStream('test/png/orange-square.png').pipe(png);
stream.on('parsed', function()
{
Please sign in to comment.
Something went wrong with that request. Please try again.