directory-encoder
Advanced tools
Comparing version
@@ -13,6 +13,15 @@ /*global require:true*/ | ||
function DirectoryEncoder( input, output, options ){ | ||
this.input = input; | ||
var files; | ||
if( typeof input === "string" && fs.lstatSync( input ).isDirectory()){ | ||
files = fs.readdirSync( input ).map( function( file ){ | ||
return path.join( input, file ); | ||
}); | ||
} else if( Array.isArray( input ) ){ | ||
files = input; | ||
} else { | ||
throw new Error( "Input must be Array of files or String that is a directory" ); | ||
} | ||
this.output = output; | ||
this.options = options || {}; | ||
this.files = files; | ||
this.prefix = this.options.prefix || ".icon-"; | ||
@@ -45,5 +54,5 @@ | ||
// append each selector | ||
fs.readdirSync( this.input ).forEach(function( file ) { | ||
this.files.forEach(function( filepath ) { | ||
var css, datauri, stats, | ||
filepath = path.join( self.input, file ), | ||
file = path.basename( filepath ), | ||
extension = path.extname( file ); | ||
@@ -50,0 +59,0 @@ |
@@ -29,3 +29,5 @@ /*global require:true*/ | ||
//replace | ||
.replace(/'/gmi, "\\i") ); | ||
.replace(/'/gmi, "\\i") ) | ||
//encode brackets | ||
.replace(/\(/g, "%28").replace(/\)/g, "%29"); | ||
}; | ||
@@ -32,0 +34,0 @@ |
{ | ||
"name": "directory-encoder", | ||
"description": "Encode a directory of images to CSS", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"homepage": "https://github.com/filamentgroup/directory-encoder", | ||
@@ -6,0 +6,0 @@ "author": { |
# directory-encoder [](http://travis-ci.org/filamentgroup/directory-encoder) | ||
[ ](http://www.filamentgroup.com/) | ||
Encode a directory of PNG or SVG images to CSS | ||
@@ -26,3 +28,4 @@ | ||
Takes three arguments, source directory for encoding, destination css | ||
Takes three arguments, source directory for encoding/an array of files | ||
you'd like to encode, destination css | ||
file for when it writes, and an options hash that includes a spot for | ||
@@ -56,5 +59,8 @@ customselectors, a hbs template in case you want to get nuts with your | ||
* 0.6.0 Fix encoding brackets and ability to take an array | ||
* 0.5.0 PNGpath | ||
* 0.4.0 Width/Height from template | ||
* 0.3.0 Adding custom selectors that allow for wildcards | ||
* 0.2.0 Adding switchable icon name prefixes | ||
* 0.1.0 Woo | ||
* 0.2.0 Adding switchable icon name prefixes | ||
* 0.3.0 Adding custom selectors that allow for wildcards | ||
@@ -61,0 +67,0 @@ ## License |
@@ -85,3 +85,24 @@ /*global require:true*/ | ||
/** | ||
* Test encoding brackets '(' and ')' to generate a valid encoded SVG | ||
* in case of <g> element with functions in the 'transformation' attribute | ||
*/ | ||
exports['SvgURIEncoder_brackets'] = { | ||
setUp: function( done ) { | ||
this.encoder = new SvgURIEncoder( "test/files/bear.svg" ); | ||
done(); | ||
}, | ||
encode: function( test ) { | ||
var datauri = this.encoder.encode(); | ||
test.ok( datauri.indexOf(SvgURIEncoder.prefix) >= 0 ); | ||
test.ok( datauri.indexOf( '%' ) >= 0 ); | ||
test.ok( datauri.indexOf( '(' ) === -1 ); | ||
test.ok( datauri.indexOf( ')' ) === -1 ); | ||
test.done(); | ||
} | ||
}; | ||
exports['PngURIEncoder'] = { | ||
@@ -88,0 +109,0 @@ setUp: function( done ) { |
/*global require:true*/ | ||
/*global __dirname:true*/ | ||
(function(exports){ | ||
@@ -11,7 +12,9 @@ "use strict"; | ||
var outputFileData2 = '\n.icon-2,\n\n.icon-bear:before,\n\n.icon-bear:after,\n\n.what-bear {\n\tbackground-image: url(\'data:image/svg+xml;charset=US-ASCII,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22100%22%20height%3D%2262.905px%22%3E%3Cpath%20d%3D%22M11.068%2C34.558c-1.585-2.365-2.595-5.098-2.939-8.106c-0.344%2C0.092-0.666%2C0.161-1.033%2C0.161%20c-2.342%2C0-4.248-1.906-4.248-4.248c0-1.47%2C0.758-2.756%2C1.883-3.514l12.147-8.45c2.549-1.562%2C5.534-2.526%2C8.749-2.641l30.149%2C0.092%20L77.819%2C4.34c0-0.115%2C0-0.229%2C0-0.345C77.819%2C1.791%2C79.586%2C0%2C81.791%2C0c2.205%2C0%2C3.996%2C1.791%2C3.996%2C3.995%20c0%2C0.345-0.046%2C0.712-0.138%2C1.034l2.043%2C0.275c2.365%2C0.459%2C4.156%2C2.549%2C4.156%2C5.052c0%2C0.161%2C0%2C0.298-0.022%2C0.436l6.544%2C3.536%20c0.941%2C0.368%2C1.63%2C1.309%2C1.63%2C2.388c0%2C0.367-0.068%2C0.689-0.206%2C1.01l-1.631%2C3.697c-0.804%2C1.309-2.181%2C2.228-3.788%2C2.411%20l-15.041%2C1.791L65.787%2C41.527l7.738%2C13.363l5.098%2C2.365c0.803%2C0.552%2C1.354%2C1.493%2C1.354%2C2.549c0%2C1.699-1.378%2C3.078-3.101%2C3.078%20l-9.805%2C0.022c-2.525%2C0-4.707-1.424-5.809-3.49l-8.382-15.155l-18.92%2C0.023l6.682%2C10.287l4.937%2C2.25%20c0.919%2C0.551%2C1.516%2C1.538%2C1.516%2C2.664c0%2C1.699-1.378%2C3.076-3.077%2C3.076l-9.828%2C0.023c-2.388%2C0-4.5-1.286-5.649-3.215l-9.208-14.627%20l-6.429%2C6.246l-0.528%2C4.087l2.158%2C1.423c0.368%2C0.184%2C0.689%2C0.438%2C0.965%2C0.758c1.056%2C1.332%2C0.872%2C3.284-0.459%2C4.34%20c-0.574%2C0.482-1.286%2C0.713-1.975%2C0.689l-4.317%2C0.023c-1.194-0.139-2.273-0.758-2.962-1.677l-5.029-8.68C0.275%2C51.033%2C0%2C50%2C0%2C48.898%20c0-1.676%2C0.62-3.215%2C1.676-4.387L11.068%2C34.558z%22%2F%3E%3C%2Fsvg%3E\');\n\tbackground-repeat: no-repeat;\n\twidth: 100px;\n\theight: 62.905px;\n}\n\n\n\n.icon-dog:before,\n\n.icon-dog:after,\n\n.what-dog {\n\tbackground-image: url(\'dog.png\');\n\tbackground-repeat: no-repeat;\n\twidth: 100px;\n\theight: 62.905px;\n}\n\n\n'; | ||
var encoder, output = "test/output/encoded.css"; | ||
var encoder, output = path.resolve( path.join( __dirname, "output", "encoded.css" ) ); | ||
exports['encode'] = { | ||
setUp: function( done ) { | ||
encoder = new Constructor( "test/directory-files", output ); | ||
this.encoder = new Constructor( "test/directory-files", output ); | ||
this.encoder2 = new Constructor( [path.resolve( path.join(__dirname, "directory-files", "bear.svg")), | ||
path.resolve( path.join(__dirname, "directory-files", "dog.png"))], output ); | ||
done(); | ||
@@ -21,5 +24,10 @@ }, | ||
output: function( test ) { | ||
encoder.encode(); | ||
this.encoder.encode(); | ||
test.ok( fs.existsSync(output) ); | ||
test.ok( /\.bear/.test(fs.readFileSync(output)) ); | ||
fs.unlinkSync(output); | ||
this.encoder2.encoder(); | ||
test.ok( fs.existsSync(output) ); | ||
test.ok( /\.bear/.test(fs.readFileSync(output)) ); | ||
test.done(); | ||
@@ -29,3 +37,3 @@ }, | ||
selector: function( test ) { | ||
encoder._css = function( name, data ){ | ||
this.encoder._css = function( name, data ){ | ||
test.ok( name === "bear" || name === "dog" ); | ||
@@ -36,3 +44,3 @@ | ||
encoder.encode(); | ||
this.encoder.encode(); | ||
test.done(); | ||
@@ -42,4 +50,4 @@ }, | ||
dup: function( test ) { | ||
encoder = new Constructor( "test/encoding-dup", output ); | ||
test.throws(function() { encoder.encode(); }); | ||
this.encoder = new Constructor( "test/encoding-dup", output ); | ||
test.throws(function() { this.encoder.encode(); }); | ||
test.done(); | ||
@@ -49,2 +57,3 @@ } | ||
var encoders; | ||
@@ -51,0 +60,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
35374
5.4%645
6.09%68
9.68%