Skip to content

Instantly share code, notes, and snippets.

@markandrus
Created June 29, 2011 17:28
Show Gist options
  • Select an option

  • Save markandrus/1054369 to your computer and use it in GitHub Desktop.

Select an option

Save markandrus/1054369 to your computer and use it in GitHub Desktop.
stylecheck.rb
#! /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
@markandrus

Copy link
Copy Markdown
Author

Use in one of the following ways
./stylecheck.rb path/to/html path/to/css
./stylecheck.rb index.htm path/to/css extra.css
./stylecheck.rb path/to/html file1.css file2.css dir/css

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment