Ruby On Rails, Design, Simplicity, Web 2.0, Ajax, Mac and Tons of Pizza.

Oct 22

Dust-Me CSV to CSS with Rake

Posted by Sandro Paganotti in Ruby on Rails - comments are closed digg this add to delicious

Have you ever tried Dust-Me Selectors ? It is a Firefox Add-On that parse your CSS file against the pages of your website and return a list of unused selectors.

The problem is that this add-on has only a CSV export feature which let you save the list of unused selectors in a plain file. Next you have to manually find each line in you CSS file and delete it (ARGH!).

I’ve wrote a rake task to fix this problem; you can call it with the command:


rake eraser:css FILENAME="path/to/exported/csv/file.csv" 

and it create a new CSS file (named “mod_” + original_file_name) containing only the used selectors!

Here is the task code, it is still in beta but seems working well, anyway check the css generated code before use.


namespace :eraser do

  task :css => :environment do

    tmp_cols = File.open(ENV["FILENAME"],"r"){|f| f.collect{|e| e.split(",")}}
    cols = Array.new(tmp_cols[0].length)
    tmp_cols.each do |e|
      cols.each_index do |i|
        cols[i] = Array.new if cols[i].nil?
        cols[i] << e[i].to_s
      end
    end 

    cols.each_index do |index|
      cols[index][0] =~ Regexp.new("\\/([^\\?\\/]+)(\\?|$)")
      filename = $1
      cols[index][0] = filename
      cols[index][1] = File.open(File.join(RAILS_ROOT,"public","stylesheets",filename), "r"){ |file| file.collect{|e| e}.join("\n")}
    end

    cols.each do |lines|
      lines.each do |l|
        if (not l.strip.blank?) and (not (l == lines[1] or l == lines[0]))
          puts "Deleting: " + l
          rexp =  Regexp.new("^(.*)#{l},*(.*" +"\\{[^\\}]*\\})")
          lines[1] =~ rexp
          before = $1.to_s
          after = $2.to_s
          if before.strip.blank? and (after.split("{"))[0].to_s.strip.blank?
            lines[1].sub!(rexp,"")
          else
            lines[1].sub!(rexp,before+after).sub!(/, *\{/,"{")
          end
        end
      end
    end

    puts "creating modded file" 
    cols.each do |e|
      File.open(File.join(RAILS_ROOT,"public","stylesheets","mod_"+e[0]), "w") do |f|
        f.write(e[1])
      end
    end

  end
end


Comments

  • Sean

    Posted on October 22

    Wow, thanks for posting both this AND the info about the plugin. I had no idea. Is that Regexp in the cols.each_index correct? I assume it's trying to get the filename from the URL on the top line of the .csv file. The top of my .csv as exported from Dust-Me is (no idea how well this will format): "http://www.mysite.info/stylesheets/press.css" "873 unused selectors:" body.home .banner dl dt dd Any thoughts?
  • Sandro

    Posted on October 23

    Yes, the first Regexp tries to get the css filename, I've just noticed that if you don't have (as I have) a leading '?' on your CSS the Regexp doesn't work. (Ok, I've fixed it)
  • Sean

    Posted on October 23

    I ended up doing something similar and getting past that (though, the above still breaks for me--hacking a .chop! on got rid of the extra double-quote), and the script reports it's deleting several selectors, but the resulting file is actually bigger than the original because it writes out every line and adds another newline. I'm not so concerned with fixing the rake task, though. I'm just wondering why, using the same tools, we're getting such different results.
  • crccw

    Posted on October 26

    rake aborted! Don't know how to build task 'environment' (See full trace by running task with --trace) I can't use it
  • crccw

    Posted on October 27

    How can I add the take code?

Post a comment

Categories:

Tags:

Powered by Mephisto, Valid XHTML 1.1, Valid CSS - Supported by Wave Factory