gulp-edit-xml
Advanced tools
Comparing version 2.0.0 to 3.0.0
56
index.js
@@ -1,12 +0,11 @@ | ||
var Stream = require('stream'), | ||
xml2js = require('xml2js'), | ||
isFunction = require('lodash.isfunction'), | ||
isObject = require('lodash.isobject'), | ||
assign = require("object-assign"), | ||
gutil = require('gulp-util'); | ||
const Stream = require('stream'); | ||
const xml2js = require('xml2js'); | ||
const isFunction = require('lodash.isfunction'); | ||
const isObject = require('lodash.isobject'); | ||
const assign = require('object-assign'); | ||
const PluginError = require('plugin-error'); | ||
var xmlEdit = function(transform, options){ | ||
if(!isFunction(transform)){ | ||
transform = function(data){ | ||
const xmlEdit = function(transform, options) { | ||
if (!isFunction(transform)) { | ||
transform = function(data) { | ||
return data; | ||
@@ -16,20 +15,19 @@ }; | ||
var defaults = { | ||
const defaults = { | ||
parserOptions: {}, | ||
builderOptions: { | ||
headless:true, | ||
renderOpts:{ | ||
headless: true, | ||
renderOpts: { | ||
pretty: false | ||
} | ||
} | ||
} | ||
}; | ||
var settings = assign(defaults, options); | ||
const settings = assign(defaults, options); | ||
var stream = new Stream.Transform({ objectMode: true }); | ||
const stream = new Stream.Transform({ objectMode: true }); | ||
stream._transform = function(file, unused, done){ | ||
stream._transform = function(file, unused, done) { | ||
const that = this; | ||
var that = this; | ||
if (file.isNull()) { | ||
@@ -40,18 +38,20 @@ return done(null, file); | ||
if (file.isStream()) { | ||
return done(new gutil.PluginError('gulp-xml-edit', 'Streaming not supported')); | ||
return done(new PluginError('gulp-xml-edit', 'Streaming not supported')); | ||
} | ||
const content = file.contents.toString('utf-8'); | ||
const parser = new xml2js.Parser(settings.parserOptions); | ||
const builder = new xml2js.Builder(settings.builderOptions); | ||
var content = file.contents.toString('utf-8'), | ||
parser = new xml2js.Parser(settings.parserOptions), | ||
builder = new xml2js.Builder(settings.builderOptions); | ||
parser.parseString(content, function(err, data) { | ||
let content = transform.call(null, data); | ||
parser.parseString(content, function(err,data){ | ||
var content = transform.call(null, data); | ||
if(!isObject(content)){ | ||
done(new gutil.PluginError('gulp-xml-edit', 'transformation does not returns an object')); | ||
if (!isObject(content)) { | ||
done(new PluginError('gulp-xml-edit', 'transformation does not returns an object')); | ||
return; | ||
} | ||
content = builder.buildObject(content); | ||
file.contents = new Buffer(content); | ||
return done(null, file); | ||
@@ -62,4 +62,4 @@ }); | ||
return stream; | ||
} | ||
}; | ||
module.exports = xmlEdit; |
{ | ||
"name": "gulp-edit-xml", | ||
"version": "2.0.0", | ||
"description": "Gulp plugin for editing xml files", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jasmine-node test/edit-xml-spec.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/vkbansal/gulp-edit-xml.git" | ||
}, | ||
"keywords": [ | ||
"gulp", | ||
"plugin", | ||
"gulpplugin", | ||
"xml", | ||
"svg" | ||
], | ||
"author": "Vivek Kumar Bansal <contact@vkbansal.me>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/vkbansal/gulp-edit-xml/issues" | ||
}, | ||
"homepage": "https://github.com/vkbansal/gulp-edit-xml", | ||
"dependencies": { | ||
"gulp-util": "^3.0.4", | ||
"lodash.isfunction": "^3.0.6", | ||
"lodash.isobject": "^3.0.2", | ||
"object-assign": "^4.0.1", | ||
"xml2js": "^0.4.8" | ||
}, | ||
"devDependencies": { | ||
"event-stream": "^3.3.1", | ||
"jasmine-node": "^1.14.5" | ||
} | ||
"name": "gulp-edit-xml", | ||
"version": "3.0.0", | ||
"description": "Gulp plugin for editing xml files", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jasmine-node test/edit-xml-spec.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/vkbansal/gulp-edit-xml.git" | ||
}, | ||
"keywords": [ | ||
"gulp", | ||
"plugin", | ||
"gulpplugin", | ||
"xml", | ||
"svg" | ||
], | ||
"author": "Vivek Kumar Bansal <contact@vkbansal.me>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/vkbansal/gulp-edit-xml/issues" | ||
}, | ||
"homepage": "https://github.com/vkbansal/gulp-edit-xml", | ||
"dependencies": { | ||
"lodash.isfunction": "^3.0.6", | ||
"lodash.isobject": "^3.0.2", | ||
"object-assign": "^4.0.1", | ||
"plugin-error": "^0.1.2", | ||
"xml2js": "^0.4.8" | ||
}, | ||
"devDependencies": { | ||
"event-stream": "^3.3.1", | ||
"jasmine-node": "^1.14.5", | ||
"vinyl": "^2.1.0" | ||
} | ||
} |
[![NPM version](https://badge.fury.io/js/gulp-edit-xml.svg)](http://badge.fury.io/js/gulp-edit-xml) [![Build Status](https://travis-ci.org/vkbansal/gulp-edit-xml.svg?branch=master)](https://travis-ci.org/vkbansal/gulp-edit-xml) [![Dependencies](https://david-dm.org/vkbansal/gulp-edit-xml.png)](https://david-dm.org/vkbansal/gulp-edit-xml) [![devDependency Status](https://david-dm.org/vkbansal/gulp-edit-xml/dev-status.svg)](https://david-dm.org/vkbansal/gulp-edit-xml#info=devDependencies) | ||
#gulp-edit-xml | ||
# gulp-edit-xml | ||
A gulp plugin for editing xml files, specially svg files. It makes use of [xml2js](https://www.npmjs.org/package/xml2js). It helps in further manual optimization of a svg file even after using [svgo](https://www.npmjs.org/package/svgo). | ||
##Install | ||
## Install | ||
```bash | ||
npm install gulp-edit-xml | ||
``` | ||
##Why I made this | ||
## Why I made this | ||
Recieved a `~400KB` svg file from a client. After editing it in Inkscape and processing it with `svgo`, the file size was reduced to `~140KB`. The file contained `~1600` circle elements which had `transform` and `fill` attributes on each of it. | ||
@@ -20,3 +25,4 @@ | ||
##Usage | ||
## Usage | ||
```js | ||
@@ -27,38 +33,45 @@ var gulp = require('gulp'), | ||
gulp.task('svg', function(){ | ||
gulp.src('src/img/main.svg') | ||
.pipe(svgo()) | ||
.pipe(xmlEdit(function(xml){ | ||
var nodes = xml.svg.g[0].circle; | ||
for(var i = 0 , l = nodes.length; i < l; i++){ | ||
var cn = nodes[i].$; | ||
if (cn.hasOwnProperty('transform')){ | ||
var transforms = cn.transform.match(/translate\(([\d\s\-\.]+)\)/)[1]; | ||
transforms = transforms.split(' '); | ||
cn.cx = parseInt(cn.cx) + parseInt(transforms[0]); | ||
cn.cx = Math.round(cn.cx*10)/10; | ||
if (transforms.length == 2){ | ||
cn.cy = parseInt(cn.cy) + parseInt(transforms[1]); | ||
cn.cy = Math.round(cn.cy*10)/10; | ||
gulp.task('svg', function() { | ||
gulp | ||
.src('src/img/main.svg') | ||
.pipe(svgo()) | ||
.pipe( | ||
xmlEdit(function(xml) { | ||
var nodes = xml.svg.g[0].circle; | ||
for (var i = 0, l = nodes.length; i < l; i++) { | ||
var cn = nodes[i].$; | ||
if (cn.hasOwnProperty('transform')) { | ||
var transforms = cn.transform.match(/translate\(([\d\s\-\.]+)\)/)[1]; | ||
transforms = transforms.split(' '); | ||
cn.cx = parseInt(cn.cx) + parseInt(transforms[0]); | ||
cn.cx = Math.round(cn.cx * 10) / 10; | ||
if (transforms.length == 2) { | ||
cn.cy = parseInt(cn.cy) + parseInt(transforms[1]); | ||
cn.cy = Math.round(cn.cy * 10) / 10; | ||
} | ||
delete cn.transform; | ||
delete cn.fill; | ||
} | ||
nodes[i].$ = cn; | ||
} | ||
delete cn.transform; | ||
delete cn.fill; | ||
} | ||
nodes[i].$ = cn; | ||
} | ||
xml.svg.g[0].circle = nodes; | ||
return xml; | ||
})) | ||
.pipe(gulp.dest('dist/img/')) | ||
}) | ||
xml.svg.g[0].circle = nodes; | ||
return xml; | ||
}) | ||
) | ||
.pipe(gulp.dest('dist/img/')); | ||
}); | ||
``` | ||
##Options | ||
## Options | ||
Its takes two optional arguments: | ||
``` | ||
xmlEdit(transform,options) | ||
``` | ||
**transform:** A function that can be used to do manual operations. It takes one argument: The `xml` as an object. *Remember to return the object at end*. | ||
**transform:** A function that can be used to do manual operations. It takes one argument: The `xml` as an object. _Remember to return the object at end_. | ||
default: | ||
```js | ||
@@ -73,2 +86,3 @@ function(data){ | ||
default: | ||
```js | ||
@@ -86,3 +100,4 @@ { | ||
###License | ||
### License | ||
[MIT](LICENSE.md) |
@@ -1,20 +0,13 @@ | ||
var xmlEdit = require('../index.js'), | ||
gutil = require('gulp-util'), | ||
es = require('event-stream'); | ||
const xmlEdit = require('../index.js'); | ||
const es = require('event-stream'); | ||
const Vinyl = require('vinyl'); | ||
describe('gulp-xml-edit', function(){ | ||
describe('gulp-xml-edit', function() { | ||
it('should work in buffer mode', function(done) { | ||
const stream = xmlEdit(); | ||
const fakeBuffer = new Buffer('<svg/>'); | ||
const fakeFile = new Vinyl({ contents: fakeBuffer }); | ||
it('should work in buffer mode', function(done){ | ||
var stream = xmlEdit(), | ||
fakeBuffer = new Buffer('<svg/>'), | ||
fakeFile = new gutil.File({ contents: fakeBuffer }); | ||
stream.on('data', function(file){ | ||
expect(file.contents.toString()).toEqual(fakeBuffer.toString()); | ||
}); | ||
stream.on('end', function() { | ||
done(); | ||
}); | ||
stream.on('data', file => expect(file.contents.toString()).toEqual(fakeBuffer.toString())); | ||
stream.on('end', () => done()); | ||
stream.write(fakeFile); | ||
@@ -24,18 +17,21 @@ stream.end(); | ||
it('should let null files pass through', function(done) { | ||
var stream = xmlEdit(), | ||
it('should let null files pass through', function(done) { | ||
const stream = xmlEdit(); | ||
const fakeFile = new Vinyl({ path: 'null.md', contents: null }); | ||
let n = 0; | ||
fakeFile = new gutil.File({path: 'null.md', contents: null}), | ||
stream.pipe( | ||
es.through( | ||
file => { | ||
expect(file.path).toBe('null.md'); | ||
expect(file.contents).toBe(null); | ||
n++; | ||
}, | ||
() => { | ||
expect(n).toBe(1); | ||
done(); | ||
} | ||
) | ||
); | ||
n = 0; | ||
stream.pipe(es.through(function(file) { | ||
expect(file.path).toBe('null.md'); | ||
expect(file.contents).toBe(null); | ||
n++; | ||
}, function() { | ||
expect(n).toBe(1); | ||
done(); | ||
})); | ||
stream.write(fakeFile); | ||
@@ -45,29 +41,33 @@ stream.end(); | ||
it('should transform as expected', function(done){ | ||
it('should transform as expected', function(done) { | ||
const stream = xmlEdit(data => { | ||
delete data.svg.g[0].circle[0].$.transform; | ||
return data; | ||
}); | ||
const fakeBuffer = new Buffer( | ||
"<svg><g><circle cx='20' cy='20' cr='20' transform='translate(20 20)'/></g></svg>" | ||
); | ||
const fakeFile = new Vinyl({ | ||
contents: fakeBuffer | ||
}); | ||
let n = 0; | ||
var stream = xmlEdit(function(data){ | ||
delete data.svg.g[0].circle[0].$.transform; | ||
return data; | ||
}), | ||
stream.pipe( | ||
es.through( | ||
file => { | ||
expect(file.contents.toString()).toBe( | ||
'<svg><g><circle cx="20" cy="20" cr="20"/></g></svg>' | ||
); | ||
n++; | ||
}, | ||
() => { | ||
expect(n).toBe(1); | ||
done(); | ||
} | ||
) | ||
); | ||
fakeBuffer = new Buffer("<svg><g><circle cx='20' cy='20' cr='20' transform='translate(20 20)'/></g></svg>"), | ||
fakeFile = new gutil.File({ | ||
contents: fakeBuffer | ||
}), | ||
n = 0; | ||
stream.pipe(es.through(function(file) { | ||
expect(file.contents.toString()).toBe('<svg><g><circle cx="20" cy="20" cr="20"/></g></svg>'); | ||
n++; | ||
}, function() { | ||
expect(n).toBe(1); | ||
done(); | ||
})); | ||
stream.write(fakeFile); | ||
stream.end(); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
9794
111
100
3
6
+ Addedplugin-error@^0.1.2
+ Addedansi-cyan@0.1.1(transitive)
+ Addedansi-red@0.1.1(transitive)
+ Addedarr-diff@1.1.0(transitive)
+ Addedarr-flatten@1.1.0(transitive)
+ Addedarr-union@2.1.0(transitive)
+ Addedarray-slice@0.2.3(transitive)
+ Addedextend-shallow@1.1.4(transitive)
+ Addedkind-of@1.1.0(transitive)
+ Addedplugin-error@0.1.2(transitive)
- Removedgulp-util@^3.0.4
- Removedansi-gray@0.1.1(transitive)
- Removedansi-regex@2.1.1(transitive)
- Removedansi-styles@2.2.1(transitive)
- Removedarray-differ@1.0.0(transitive)
- Removedarray-uniq@1.0.3(transitive)
- Removedbeeper@1.1.1(transitive)
- Removedchalk@1.1.3(transitive)
- Removedclone@1.0.4(transitive)
- Removedclone-stats@0.0.1(transitive)
- Removedcolor-support@1.1.3(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removeddateformat@2.2.0(transitive)
- Removedduplexer2@0.0.2(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedfancy-log@1.3.3(transitive)
- Removedglogg@1.0.2(transitive)
- Removedgulp-util@3.0.8(transitive)
- Removedgulplog@1.0.0(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removedhas-gulplog@0.1.0(transitive)
- Removedinherits@2.0.4(transitive)
- Removedisarray@0.0.11.0.0(transitive)
- Removedlodash._basecopy@3.0.1(transitive)
- Removedlodash._basetostring@3.0.1(transitive)
- Removedlodash._basevalues@3.0.0(transitive)
- Removedlodash._getnative@3.9.1(transitive)
- Removedlodash._isiterateecall@3.0.9(transitive)
- Removedlodash._reescape@3.0.0(transitive)
- Removedlodash._reevaluate@3.0.0(transitive)
- Removedlodash._reinterpolate@3.0.0(transitive)
- Removedlodash._root@3.0.1(transitive)
- Removedlodash.escape@3.2.0(transitive)
- Removedlodash.isarguments@3.1.0(transitive)
- Removedlodash.isarray@3.0.4(transitive)
- Removedlodash.keys@3.1.2(transitive)
- Removedlodash.restparam@3.6.1(transitive)
- Removedlodash.template@3.6.2(transitive)
- Removedlodash.templatesettings@3.1.1(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmultipipe@0.1.2(transitive)
- Removedobject-assign@3.0.0(transitive)
- Removedparse-node-version@1.0.1(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedreadable-stream@1.1.142.3.8(transitive)
- Removedreplace-ext@0.0.1(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedsparkles@1.0.1(transitive)
- Removedstring_decoder@0.10.311.1.1(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedsupports-color@2.0.0(transitive)
- Removedthrough2@2.0.5(transitive)
- Removedtime-stamp@1.1.0(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedvinyl@0.5.3(transitive)
- Removedxtend@4.0.2(transitive)