/*
 * grunt-webpcss
 * https://github.com/lexich/webpcss-transform
 *
 * Copyright (c) 2014 Efremov Alexey
 * Licensed under the MIT license.
 */
'use strict';

var postcss = require('postcss');
var _ = require('lodash');

module.exports = function (data, options){
	var proc = postcss(function(css){
		var nodes = [];
		css.eachDecl(function(rule, data) {
			if(rule.prop.match(/^background/) && rule._value.match(/url/) ){
				var selector = "";
				var selectors = rule.parent.selector.split(',');
				for(var index in selectors){
					if(!!selector){
						selector += ", ";
					}
					selector += options.baseClass + " " + selectors[index].trim();
				}
				var rx = options.replace_from;
				if(!_.isRegExp(options.replace_from)){
					rx = new RegExp(rx);
				}
				var value = rule._value.replace(rx,options.replace_to);
				var prop = rule.prop;
				if( value.indexOf(",") === -1 && /url[ ]*\((.+)\)/g.exec(value) ){
					value = "url(" + RegExp.$1 + ");";
					prop = "background-image";
				}
				var new_rule = postcss.rule({selector:selector});
				new_rule.append({
					prop:prop,
					value: value
				});
				nodes.push(new_rule);
			}
		});
		for(var n=0; n < nodes.length; n++){
			css.append(nodes[n]);
		}
	});
	return proc.process(data).css;
}

@@ -220,79 +220,54 @@
 );%0A%0A
-module.exports = function (data, options)%7B%0A%09var proc = postcss(
+function processor (options)%7B%0A%09return 
 function
 (css
@@ -262,16 +262,17 @@
 function
+ 
 (css)%7B%0A%09
@@ -1203,41 +1203,178 @@
 %7D%0A%09%7D
-);%0A%09return proc.process(data).css;%0A%7D
+%0A%7D%0A%0Afunction transform (data, options)%7B%0A%09return postcss(processor(options)).process(data).css;%0A%7D%0A%0Amodule.exports.processor = processor;%0Amodule.exports.transform = transform;
 %0A

add support postcss processor