Skip to content

Commit

Permalink
version 0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
heldr committed May 14, 2013
1 parent 0716492 commit 8b32435
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 183 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ logs
results

node_modules
npm-debug.log
npm-debug.log
9 changes: 5 additions & 4 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"es5": true,
"esnext": true,
"bitwise": true,
"curly": true,
Expand All @@ -10,9 +9,11 @@
"noempty": true,
"regexp": true,
"undef": true,
"strict": false,
"strict": true,
"trailing": true,
"smarttabs": true,
"node": true,
"globalstrict": true
}
"globalstrict": true,
"white": true,
"indent": 4
}
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test/
.jshintrc
.travis.yml
.gitignore
.gitignore
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fulltest: clean install test
clean:
rm -rf node_modules
lint:
./node_modules/.bin/jshint datauri.js lib/*
./node_modules/.bin/jshint datauri.js cli.js lib/*
spec:
@echo "Running test suite..."
$(NODE) test/run.js
Expand Down
31 changes: 22 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ A simple [Data URI scheme][datauri] generator built on top of [Node.js][nodejs].

`npm install -g datauri` (it may require Root privileges)

MODULE
------

```js
var Datauri = require('datauri');

console.log(Datauri('test/myfile.png')); //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...";
```

CLIENT
------
Expand All @@ -33,6 +25,27 @@ If you want to define a Class Name, just type:
$ datauri brand.png asset/background.css MyNewClass
```

API
---

```js
var Datauri = require('datauri');

// without instance
var datauri = Datauri('test/myfile.png');
console.log(datauri); //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...";

// with instance
var dUri = new Datauri('test/myfile.png');

console.log(dUri.content); //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...";
console.log(dUri.mimetype); //=> "image/png";
console.log(dUri.base64); //=> "iVBORw0KGgoAAAANSUhEUgAA...";
console.log(dUri.getCSS()); //=> "\n.case {\n background: url('data:image/png;base64,iVBORw...";
console.log(dUri.getCSS("myClass")); //=> "\n.myClass {\n background: url('data:image/png;base64,iVBORw...";

```

DEVELOPING
----------

Expand All @@ -55,4 +68,4 @@ MIT License
(c) [Helder Santana](http://heldr.com)

[nodejs]: http://nodejs.org/download
[datauri]: http://en.wikipedia.org/wiki/Data_URI_scheme
[datauri]: http://en.wikipedia.org/wiki/Data_URI_scheme
72 changes: 41 additions & 31 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,60 @@
* https://raw.github.com/heldr/datauri/master/MIT-LICENSE.txt
*/

var args = process.argv,
fs = require('fs'),
path = require('path'),
DataURI = require('./datauri');

function writeNewCssFile( file , content , action ) {
fs.writeFile( file , content , 'utf-8' , function(err) {
if(err) {
throw err;
}
(function (args) {
"use strict";

console.log('File ' + action + ': ' + file);
});
}
var fs = require('fs'),
path = require('path'),
DataURI = require('./datauri');

function outputCSS( file , content ) {
function writeNewCssFile(file, content, action) {
fs.writeFile(file, content, 'utf-8', function (err) {
if (err) {
throw err;
}

if ( fs.existsSync(file) ) {
console.log('File ' + action + ': ' + file);
});
}

if ( path.extname(file).match(/(css|sass|less)/) ) {
fs.readFile( file , 'utf-8' , function( err , cssContent ) {
function outputCSS(file, content) {

cssContent += content;
if (fs.existsSync(file)) {

writeNewCssFile( file , cssContent , 'updated' );
if (path.extname(file).match(/(css|sass|less)/)) {
fs.readFile(file, 'utf-8', function (err, cssContent) {

});
} else {
console.log( 'Must be a CSS file' );
}
cssContent += content;

writeNewCssFile(file, cssContent, 'updated');

});
} else {
console.log('Must be a CSS file');
}

} else {

writeNewCssFile( file , content , 'created' );
} else {

writeNewCssFile(file, content, 'created');

}
}
}

if (args.length > 2) {
if (args.length > 2) {

var cls = args[4] || '',
uri = new DataURI( args[2] );
var cls = args[4] || '',
uri = new DataURI(args[2]),
content;

( args.length < 4 ) ? console.log( uri.content ) : outputCSS( args[3] , uri.getCss( cls ) );
if (args.length < 4) {
content = uri.content;
console.log(content);
} else {
content = uri.getCss(cls);
outputCSS(args[3], content);
}

}
}
}(process.argv));
19 changes: 11 additions & 8 deletions lib/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
* https://raw.github.com/heldr/datauri/master/MIT-LICENSE.txt
*/

var _ = require('lodash');
module.exports = (function (templayed) {
"use strict";

var template = [
"",
".<%= className %> {",
" background: url('<%= background %>')",
"}"
].join('\n');
var tpl = [
"",
".{{ className }} {",
" background: url('{{ background }}')",
"}"
].join('\n');

module.exports = _.template(template);
return templayed(tpl);

}(require('templayed')));
59 changes: 37 additions & 22 deletions lib/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,52 @@
* https://raw.github.com/heldr/datauri/master/MIT-LICENSE.txt
*/

var path = require('path'),
fs = require('fs'),
mime = require('./mime'),
uri = require('./uri'),
css = require('./css');
module.exports = (function () {
"use strict";

var path = require('path'),
fs = require('fs'),
mimer = require('mimer'),
uri = require('./uri'),
css = require('./css'),
existsSync = fs.existsSync || path.existsSync;

var DataURI = function (fileName) {
if (!(this instanceof DataURI)) {
if (fileName) {
var dUri = new DataURI();

function dataURI( fileName ) {
return dUri.createConfig(fileName);
}
return new DataURI();
}

var self = this,
existsSync = fs.existsSync || path.existsSync;
if (fileName) {
return this.createConfig(fileName);
}
};

if ( existsSync( fileName ) ) {
DataURI.prototype.createConfig = function (fileName) {

this.fileName = fileName;
this.base64 = fs.readFileSync( fileName , 'base64' );
this.mimetype = mime.getFromFile( fileName );
this.content = uri( this );
if (existsSync(fileName)) {
this.fileName = fileName;
this.base64 = fs.readFileSync(fileName, 'base64');
this.mimetype = mimer(fileName);
this.content = uri(this);
}

}
return this.content;

return this.content;
}
};

DataURI.prototype.getCss = function (className) {

dataURI.prototype = {
getCss: function( className ) {
return css({
className: className || path.basename( this.fileName , path.extname( this.fileName ) ),
className: className || path.basename(this.fileName, path.extname(this.fileName)),
background: this.content
});
}
};
};

return DataURI;

module.exports = dataURI;
}).call(this);
99 changes: 0 additions & 99 deletions lib/mime.js

This file was deleted.

Loading

0 comments on commit 8b32435

Please sign in to comment.