-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9691271
Showing
10 changed files
with
719 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
SVG Stacker | ||
=========== | ||
|
||
A tool for cleaning SVG Files - JavaScript port of Scour. | ||
|
||
http://codedread.com/scour/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
var _ = require('underscore') | ||
, fs = require('fs') | ||
, cheerio = require('cheerio') | ||
, $ | ||
, root | ||
; | ||
|
||
// Setup | ||
// ---------- | ||
|
||
// Namespace Prefixes that should be removed | ||
var namespacePrefixes = ['dc', 'rdf', 'sodipodi', 'cc', 'inkscape']; | ||
|
||
// Style-Properties and Element-Attributes that might contain an id, a reference to another object | ||
var referencingProperties = ['fill', 'stroke', 'filter', 'clip-path', 'mask', 'marker-start', 'marker-end', 'marker-mid']; | ||
|
||
function findElementsWithIdAttribute() { | ||
return $('*[id]'); | ||
} | ||
|
||
|
||
function findReferencedElements() { | ||
|
||
var referenced = _($('*')).filter(function(node) { | ||
var $node = $(node); | ||
console.log(node.name); | ||
|
||
// else if xlink:href is set, then grab the id | ||
var href = $node.attr('xlink:href'); | ||
if(href) { | ||
return true; | ||
} | ||
|
||
// now get all style properties | ||
var inlineStyle = $node.attr('style'); | ||
var styles = []; | ||
|
||
if(!_.isUndefined(inlineStyle)) { | ||
var styleRules = inlineStyle.split(';'); | ||
_(styleRules).each(function(style) { | ||
var keyValue = style.split(':'); | ||
if(keyValue.length == 2) { | ||
styles.push(style.split(':')); | ||
} | ||
}); | ||
} | ||
_(referencingProperties).each(function(referencingProperty) { | ||
var value = $node.attr(referencingProperty); | ||
if(!_.isUndefined(value)) { | ||
styles.push([referencingProperty, $node.attr(referencingProperty)]); | ||
} | ||
}); | ||
|
||
_(styles).each(function(style) { | ||
if(style[1].indexOf('url') !== -1) { | ||
return true; | ||
} | ||
}); | ||
|
||
return false; | ||
}); | ||
|
||
return referenced; | ||
} | ||
|
||
function removeNamespacedElements(namespacesToRemove) { | ||
|
||
var namespacedElements = _($('*')).filter(function(node) { | ||
|
||
var $node = $(node)[0]; | ||
if($node.type !== 'tag') { | ||
return false; | ||
} | ||
|
||
var namespaceTagName = $node.name.split(':'); | ||
if(namespaceTagName.length < 2) { | ||
return false; | ||
} | ||
|
||
if(_.indexOf(namespacesToRemove, namespaceTagName[0]) === -1) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}); | ||
|
||
$(namespacedElements).remove(); | ||
|
||
} | ||
|
||
function removeNamespacedAttributes(namespacesToRemove) { | ||
|
||
$('*').each(function(ix, node) { | ||
_.each(node.attribs, function(value, key) { | ||
var namespaceAtrributeName = key.split(':'); | ||
if(namespaceAtrributeName.length < 2) { | ||
return; | ||
} | ||
if(_.indexOf(namespacesToRemove, namespaceAtrributeName[0]) !== -1) { | ||
$(node).removeAttr(key); | ||
} | ||
}); | ||
}); | ||
|
||
} | ||
|
||
function removeComments() { | ||
|
||
// process on NODE level | ||
function searchAndRemoveComment(root) { | ||
_.each(root.children, function(node) { | ||
if(node.type == 'comment') { | ||
$(node).remove(); | ||
} | ||
if(node.type == 'tag') { | ||
// walk the NODE tree | ||
_.each(node.children, searchAndRemoveComment); | ||
} | ||
}); | ||
} | ||
searchAndRemoveComment($._root); | ||
} | ||
|
||
function clean(svgString, options) { | ||
|
||
//root = cheerio(svgString); | ||
$ = cheerio.load(svgString); | ||
|
||
removeNamespacedElements(namespacePrefixes); | ||
removeNamespacedAttributes(namespacePrefixes); | ||
removeComments(); | ||
//var referenced = findReferencedElements(); | ||
|
||
|
||
//console.log(findElementsWithIdAttribute().size()); | ||
//console.log(referenced.length); | ||
var svg = $.html(); //.html(); | ||
console.log(svg); | ||
return svg; | ||
} | ||
|
||
function cleanFile(filename, options) { | ||
return clean(fs.readFileSync(filename, 'utf-8')); | ||
} | ||
|
||
//cleanFile('stack.svg'); | ||
|
||
module.exports = { | ||
clean: clean | ||
, cleanFile: cleanFile | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "svg-cleaner", | ||
"version": "0.0.1", | ||
"description": "Cleaning SVG Files - A port of Scour to JavaScript.", | ||
"keywords": [ | ||
"svg", | ||
"assets", | ||
"cleaner" | ||
], | ||
"homepage": "http://github.com/preciousforever/SVG-Cleaner", | ||
"author": { | ||
"name": "Michael Schieben", | ||
"email": "michael@precious-forever.com", | ||
"url": "http://rockitbaby.de" | ||
}, | ||
"repository": { | ||
"type" : "git", | ||
"url" : "http://github.com/preciousforever/SVG-Cleaner" | ||
}, | ||
"engines": { | ||
"node": "0.6.x" | ||
}, | ||
"dependencies": { | ||
"underscore": "x.x.x", | ||
"cheerio": "0.9.x", | ||
"expect.js" : "*" | ||
}, | ||
"bugs": "http://github.com/preciousforever/SVG-Cleaner/issues" | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--reporter dot | ||
--growl |
Oops, something went wrong.