Skip to content

Commit

Permalink
go!
Browse files Browse the repository at this point in the history
  • Loading branch information
deepsweet committed Sep 27, 2012
1 parent b1f3a62 commit 13af2ed
Show file tree
Hide file tree
Showing 35 changed files with 2,947 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules/
*.sublime-*
*~
*.lock
*.DS_Store
*.swp
*.min.svg
119 changes: 119 additions & 0 deletions .svgo
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"saxXMLParser": {
"strict": true,
"options": {
"trim": true,
"normalize": true,
"lowercase": true,
"xmlns": true,
"position": false
}
},
"plugins": {
"directPass": [
{
"name": "removeDoctype",
"active": true
},{
"name": "removeXMLProcInst",
"active": true
},{
"name": "removeComments",
"active": true
},{
"name": "removeMetadata",
"active": true
},{
"name": "removeEditorsNSData",
"active": true
},{
"name": "convertStyleToAttrs",
"active": true
},{
"name": "cleanupAttrs",
"active": true,
"params": {
"newlines": true,
"trim": true,
"spaces": true
}
},{
"name": "removeEmptyAttrs",
"active": true
},{
"name": "removeDefaultPx",
"active": true
},{
"name": "removeHiddenElems",
"active": true,
"params": {
"displayNone": true,
"opacity0": true,
"circleR0": true,
"ellipseRX0": true,
"ellipseRY0": true,
"rectWidth0": true,
"rectHeight0": true,
"patternWidth0": true,
"patternHeight0": true,
"imageWidth0": true,
"imageHeight0": true,
"pathEmptyD": true,
"polylineEmptyPoints": true,
"polygonEmptyPoints": true
}
},{
"name": "removeEmptyText",
"active": true,
"params": {
"text": true,
"tspan": true,
"tref": true
}
},{
"name": "removeSVGAttrs",
"active": true,
"params": {
"id": true,
"version": true,
"x0": true,
"y0": true,
"xmlspace": true
}
},{
"name": "convertColors",
"active": true,
"params": {
"names2hex": true,
"rgb2hex": true,
"shorthex": true
}
}
],
"reversePass": [
{
"name": "removeEmptyContainers",
"active": true,
"params": {
"a": true,
"defs": true,
"glyph": true,
"g": true,
"marker": true,
"mask": true,
"missing-glyph": true,
"pattern": true,
"svg": true,
"switch": true,
"symbol": true
}
},{
"name": "moveElemsAttrsToGroup",
"active": true
},{
"name": "collapseGroups",
"active": true
}
]
}
}
7 changes: 7 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test:
@./node_modules/.bin/mocha --reporter min

test-v:
@./node_modules/.bin/mocha --reporter spec

.PHONY: test test-v
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2012 Kir Belevich

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
76 changes: 74 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,74 @@
svgo
====
```
o-o o o o--o o-o
\ \ / | | | |
o-o o o--o o-o
|
o--o
```

## SVGO

**SVG** **O**ptimizer is a Nodejs-based tool for optimizing SVG vector graphics files.

## Why?

SVG files, especially exported from various editors, usually contains a lot of redundant and useless information such as editor metadata, comments, hidden elements and other stuff that can be safely removed without affecting SVG rendering result.

## What it can do

SVGO has a plugin-based architecture, so almost every optimization is a separate plugin.

Today we have:

* [ [>](svgo/tree/master/plugins/cleanupAttrs.js) ] cleanup attributes from newlines, trailing and repeating spaces
* [ [>](svgo/tree/master/plugins/removeDoctype.js) ] remove doctype declaration
* [ [>](svgo/tree/master/plugins/removeXMLProcInst.js) ] remove XML processing instructions
* [ [>](svgo/tree/master/plugins/removeComments.js) ] remove comments
* [ [>](svgo/tree/master/plugins/removeMetadata.js) ] remove metadata
* [ [>](svgo/tree/master/plugins/removeEditorsNSData.js) ] remove editors namespaces, elements and attributes
* [ [>](svgo/tree/master/plugins/removeEmptyAttrs.js) ] remove empty attributes
* [ [>](svgo/tree/master/plugins/removeDefaultPx.js) ] remove default "px" unit
* [ [>](svgo/tree/master/plugins/removeHiddenElems.js) ] remove a lot of hidden elements
* [ [>](svgo/tree/master/plugins/removeEmptyText.js) ] remove empty Text elements
* [ [>](svgo/tree/master/plugins/removeEmptyContainers.js) ] remove empty Container elements
* [ [>](svgo/tree/master/plugins/convertStyleToAttrs.js) ] convert styles into attributes
* [ [>](svgo/tree/master/plugins/convertColors.js) ] convert colors
* [ [>](svgo/tree/master/plugins/moveElemsAttrsToGroup.js) ] move elements attributes to the existing group wrapper
* [ [>](svgo/tree/master/plugins/collapseGroups.js) ] collapse groups

But it's not only about rude removing, SVG has a strict [specification](http://www.w3.org/TR/SVG/expanded-toc.html) with a lot of opportunities for optimizations, default values, geometry hacking and more.

How-to instructions and plugins API docs will coming ASAP.


## How to use

```
npm install -g svgo
```

```
Usage:
svgo [OPTIONS] [ARGS]
Options:
-h, --help : Help
-v, --version : Version
-c CONFIG, --config=CONFIG : Local config
-i INPUT, --input=INPUT : Input file (default: stdin)
-o OUTPUT, --output=OUTPUT : Output file (default: stdout)
```

```
svgo -i myTestFile.svg -o myTestFile.min.svg
```

## TODO
It's only the very first public alpha :)

1. documentation!
2. phantomjs-based server-side SVG rendering "before vs after" tests
3. more unit tests
4. more plugins
5.
3 changes: 3 additions & 0 deletions bin/svgo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('../lib/coa').run();
60 changes: 60 additions & 0 deletions lib/coa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
var Q = require('q'),
info = JSON.parse(require('fs').readFileSync(__dirname + '/../package.json'));

module.exports = require('coa').Cmd()
.helpful()
.name(info.name)
.title(info.description)
.opt()
.name('version').title('Version')
.short('v').long('version')
.only()
.flag()
.act(function(opts) {
return info.version;
})
.end()
.opt()
.name('config').title('Local config')
.short('c').long('config')
.end()
.opt()
.name('input').title('Input file (default: stdin)')
.short('i').long('input')
.input()
.end()
.opt()
.name('output').title('Output file (default: stdout)')
.short('o').long('output')
.output()
.end()
.act(function(options) {

var input = [],
deferred = Q.defer(),
SVGO = require('./svgo');

options.input
.on('data', function(chunk) {
input.push(chunk);
})
.once('end', function() {
deferred.resolve(input.join());
})
.resume();

return deferred.promise
.then(function(svg) {
return SVGO(svg, options);
})
.then(function(svgmin) {
var output = options.output;

output.write(svgmin);

output === process.stdout ?
output.write('\n') :
output.end();
});

});
52 changes: 52 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var QFS = require('q-fs'),
PATH = require('path'),
extend = require('./tools').extend;

module.exports = function(options) {

var defaultConfigPath = PATH.resolve(__dirname, '../.svgo');

if (!options || !options.config) return readConfig(defaultConfigPath);

return readConfig(defaultConfigPath)
.then(function(defaultConfig) {

if (typeof options.config === 'string') {

var localConfigPath = PATH.resolve(process.cwd, options.config);

return QFS.exists(localConfigPath)
.then(function(exists) {

if (!exists) return defaultConfig;

return readConfig(localConfigPath)
.then(function(localConfig) {
return extend(true, defaultConfig, localConfig);
});

});

} else if (Object.prototype.toString.call(options.config) === '[object Object]') {

return extend(true, defaultConfig, options.config);

} else {

// TODO: ...
throw new Error('...');

}

});

};

function readConfig(path) {

return QFS.read(path)
.then(function(data) {
return JSON.parse(data);
});

};
Loading

0 comments on commit 13af2ed

Please sign in to comment.