Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 23c8aceaa755292b649297a0737ac4d0e0270fe4 @Panya committed Jul 31, 2011
Showing with 205 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 README.md
  3. +19 −0 USAGE
  4. +1 −0 VERSION
  5. +13 −0 bin/durilka
  6. +111 −0 lib/durilka.js
  7. +19 −0 lib/getopts.js
  8. +16 −0 package.json
  9. +1 −0 sample/_blue.css
  10. +23 −0 sample/config.js
@@ -0,0 +1 @@
+.DS_Store
@@ -0,0 +1 @@
+see USAGE
19 USAGE
@@ -0,0 +1,19 @@
+durilka
+ durilka -h
+ durilka --help
+ показывает этот текст
+ durilka -v
+ durilka --version
+ показывает версию "Дурилки"
+ durilka -i <CSS_filename>
+ durilka --input <CSS_filename>
+ принимает <CSS_filename> на вход
+ durilka -o <CSS_filename>
+ durilka --output <CSS_filename>
+ писать результат в файл <CSS_filename> (по умолчанию пишет в исходный файл)
+ durilka -m <kilobytes>
+ durilka --max-size <kilobytes>
+ ограничение на размер кодируемого изображения в килобайтах (по умолчанию: 32)
+ durilka -b <url>
+ durilka --base-url <url>
+ базовый URL для изображений
@@ -0,0 +1 @@
+Durilka v0.0.1
@@ -0,0 +1,13 @@
+#!/usr/bin/env bash
+
+SELF_PATH=$(cd -P -- "$(dirname -- "$0")" && pwd -P) && SELF_PATH="$SELF_PATH/$(basename -- "$0")"
+
+while [ -h "$SELF_PATH" ]; do
+ DIR=$(dirname -- "$SELF_PATH")
+ SYM=$(readlink -- "$SELF_PATH")
+ SELF_PATH=$(cd -- "$DIR" && cd -- $(dirname -- "$SYM") && pwd)/$(basename -- "$SYM")
+done
+
+DURILKA_HOME=$(dirname -- "$(dirname -- "$SELF_PATH")")
+
+node $DURILKA_HOME/lib/durilka.js $@
@@ -0,0 +1,111 @@
+var url = require('url'),
+ http = require('http'),
+ fs = require('fs'),
+ getopts = require('./getopts.js').getopts,
+ opts = getopts(process.argv.slice(2, process.argv.length),
+ ['--version', '-v',
+ '--help', '-h'],
+ ['--max-size', '-m',
+ '--input', '-i',
+ '--base-url', '-b',
+ '--output', '-o']).known,
+ srcFile, outFile, baseUrl, src, maxSize;
+
+if (opts['--version'] || opts['-v']) {
+ printFile('VERSION');
+} else if (opts['--help'] || opts['-h']) {
+ printFile('USAGE');
+} else {
+ if (maxSize = (opts['--max-size'] || opts['-m'])) {
+ maxSize = parseInt(maxSize[0], 10);
+ }
+ maxSize = maxSize || 32;
+
+ if (baseUrl = (opts['--base-url'] || opts['-b'])) {
+ baseUrl = baseUrl[0];
+ }
+
+ if (srcFile = (opts['--input'] || opts['-i'])) {
+ srcFile = fs.realpathSync(srcFile[0]);
+ src = fs.readFileSync(srcFile).toString();
+
+ encodeImages(src, function(encodedSrc) {
+ outFile = (opts['--output'] || opts['-o']);
+ fs.writeFileSync(outFile ? outFile[0] : srcFile, encodedSrc);
+ });
+ } else {
+ printFile('USAGE');
+ }
+}
+
+function encodeImages(src, callback) {
+ var reUrl = /url\(["']?([^()]+?)["']?\)/gi,
+ len = src.match(reUrl).length, url;
+
+ while (url = reUrl.exec(src)) {
+ (function(url) {
+ encodeImage(url, function(data) {
+ if (data) {
+ src = src.replace(url, data);
+ }
+ if (!--len && callback) {
+ callback(src);
+ }
+ });
+ })(url[1]);
+ }
+}
+
+function encodeImage(path, callback) {
+ if (path.length && path.substr(0, 5) != 'data:') {
+ var cPath = '';
+ if (path.substr(0, 2) == '//') {
+ cPath = 'http:' + path;
+ } else if (/^https?/.test(path)) {
+ cPath = path;
+ } else {
+ cPath = baseUrl ? baseUrl + path : path;
+ }
+ } else {
+ callback && callback();
+ return;
+ }
+
+ var urlObj = url.parse(cPath),
+ opts = {
+ host: urlObj.hostname,
+ port: urlObj.port || 80,
+ path: urlObj.pathname
+ },
+ request = http.get(opts);
+
+ request.on('response', function(response) {
+ var type = response.headers['content-type'],
+ prefix = 'data:' + type + ';base64,';
+
+ response.body = [];
+ response.setEncoding('binary');
+ response.on('data', function(chunk) {
+ if (response.statusCode == 200) {
+ response.body.push(chunk);
+ } else {
+ console.error('Image was not loaded. Status: %s [%s]', response.statusCode, cPath);
+ }
+ });
+
+ response.on('end', function() {
+ var b = new Buffer(response.body.join(''), 'binary'), data;
+
+ if (Buffer.byteLength(b.toString('binary')) > maxSize * 1024) {
+ console.warn('Image size greater than %d kilobytes [%s]', maxSize, cPath);
+ } else {
+ data = prefix + b.toString('base64');
+ }
+ callback && callback(data);
+ });
+ });
+}
+
+function printFile(filename) {
+ console.log(fs.readFileSync(__dirname.slice(0, __dirname.lastIndexOf('/')) + '/' + filename).toString());
+}
@@ -0,0 +1,19 @@
+exports.getopts = function(argv, o_single, o_pairs) {
+ var opts = { known: {}, other: [] },
+ a,
+ arg;
+
+ for (var i = 0; i < argv.length; i++) {
+ arg = argv[i];
+
+ if (o_single && o_single.indexOf(arg) !== -1 && (!o_pairs || o_pairs.indexOf(arg) === -1)) {
+ opts.known[arg] = true;
+ } else if (o_pairs && o_pairs.indexOf(arg) !== -1 && (!o_single || o_single.indexOf(arg) === -1)) {
+ a = opts.known[arg] || [];
+ a.push(argv[++i]);
+ opts.known[arg] = a;
+ } else opts.other.push(arg);
+ }
+
+ return opts;
+};
@@ -0,0 +1,16 @@
+{
+ "name" : "durilka",
+ "description" : "Simple tool for making data URIs from images in CSS files",
+ "version" : "0.0.1",
+ "author" : "Mikhail Korepanov <panyakor@gmail.com>",
+ "homepage": "http://github.com/panya/durilka",
+ "repository" : {
+ "type" : "git",
+ "url" : "http://github.com/panya/durilka.git"
+ },
+ "directories" : { "lib" : "./lib" },
+ "main" : "./lib/durilka",
+ "bin" : "./bin/durilka",
+ "engines" : { "node" : ">=0.4.0" },
+ "licenses" : [ { "type" : "MIT" } ]
+}
Oops, something went wrong.
@@ -0,0 +1,23 @@
+exports.config = {
+ outFile: function(path) { return prefixFile(path, 'base.') },
+ aliases: {
+ 'url': {
+ tokens: {
+ 'value': [/url\(/i]
+ },
+ outFile: function(path) { return prefixFile(path, 'img.') }
+ }
+ }
+}
+
+function getDirName(path) {
+ return path ? path.substr(0, path.lastIndexOf('/')) : '';
+}
+
+function getFileName(path) {
+ return path ? path.substr(path.lastIndexOf('/') + 1) : '';
+}
+
+function prefixFile(path, prefix) {
+ return getDirName(path) + '/' + prefix + getFileName(path);
+}

0 comments on commit 23c8ace

Please sign in to comment.