Bd9893bfd15fa7b443d112607b937734

There is a lot of repetition here. But I don't know how to trim it up.

def compress(source)
    source.gsub!(/\s+/, " ") # collapse space
    source.gsub!(/\/\*(.*?)\*\//, "") # remove comments - caution, might want to remove this if using css hacks
    source.gsub!(/;\s+/, ";") #remove extra space after ;
    source.gsub!(/\}\s+/, "}") #remove extra space after }
    source.gsub!(/\{\s+/, "{") #remove extra space after {
    source.gsub!(/\:\s+/, ":") #remove extra space after :
    source.gsub!(/\,\s+/, ",") #remove extra space after ,

    source.gsub!(/\s+;/, ";") #remove extra space before ;
    source.gsub!(/\s+\}/, "}") #remove extra space before }
    source.gsub!(/\s+\{/, "{") #remove extra space before {
    source.gsub!(/\s+\:/, ":") #remove extra space before :
    source.gsub!(/\s+\,/, ",") #remove extra space before ,

    source.strip!
  end

Refactorings

No refactoring yet !

A8d3f35baafdaea851914b17dae9e1fc

Adam

October 31, 2008, October 31, 2008 13:32, permalink

1 rating. Login to rate!

You could do more with your regexps, like source.gsub!(/\s*([;\}\{\:\,])\s*/, '\1'). But why reinvent the wheel?

# http://code.google.com/p/rainpress/
require 'rainpress/packer'

def compress(source)
  Rainpress::Packer.new.compress(source)
end
Bd9893bfd15fa7b443d112607b937734

marko.z

October 31, 2008, October 31, 2008 14:16, permalink

No rating. Login to rate!

Thanks for the tip. Didn't know about that tool. I was trying to use css tidy and yui compressor at first but didn't have support on server.

Your refactoring





Format Copy from initial code

or Cancel