Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 0.77 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #! /usr/local/bin/ruby
  2. require 'rubygems'
  3. require 'nokogiri'
  4. require 'css_parser'
  5. puts "Report of Unused CSS Rules\n---\nHTML Path:\t" + ARGV[0] + "\n"
  6. htmls = (`ack '' #{ARGV[0]} -l --type html`).split("\n").map { |html| Nokogiri::HTML(File.read(html)) }
  7. styles = (1..ARGV.length-1).inject(Array.new) { |a, i| a | (`ack '' #{ARGV[i]} -l --type css`).split("\n") }
  8. puts "\t- Checking " + htmls.length.to_s + " HTML file(s) against " + styles.length.to_s + " stylesheet(s)\n\n"
  9. styles.each do |style|
  10.         puts "Stylesheet:\t" + style
  11.         (css = CssParser::Parser.new).load_file!(style)
  12.         css.each_selector do |sel, dec, spe|
  13.                 begin
  14.                         if (htmls.inject(false) { |a, html| a ||= html.css(sel).empty? }) then puts "\t- " + sel end
  15.                 rescue
  16.                         puts "\t- IGNORING: #{sel}"
  17.                 end
  18.         end
  19.         puts "\n"
  20. end