Dust-Me CSV to CSS with Rake
Posted by Sandro Paganotti in
Ruby on Rails -
comments are closed
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
Sandro
Posted on October 23
Sean
Posted on October 23
crccw
Posted on October 26
crccw
Posted on October 27