
Untitled
By: a guest on
May 1st, 2012 | syntax:
None | size: 0.77 KB | hits: 16 | expires: Never
#! /usr/local/bin/ruby
require 'rubygems'
require 'nokogiri'
require 'css_parser'
puts "Report of Unused CSS Rules\n---\nHTML Path:\t" + ARGV[0] + "\n"
htmls = (`ack '' #{ARGV[0]} -l --type html`).split("\n").map { |html| Nokogiri::HTML(File.read(html)) }
styles = (1..ARGV.length-1).inject(Array.new) { |a, i| a | (`ack '' #{ARGV[i]} -l --type css`).split("\n") }
puts "\t- Checking " + htmls.length.to_s + " HTML file(s) against " + styles.length.to_s + " stylesheet(s)\n\n"
styles.each do |style|
puts "Stylesheet:\t" + style
(css = CssParser::Parser.new).load_file!(style)
css.each_selector do |sel, dec, spe|
begin
if (htmls.inject(false) { |a, html| a ||= html.css(sel).empty? }) then puts "\t- " + sel end
rescue
puts "\t- IGNORING: #{sel}"
end
end
puts "\n"
end