Created
June 29, 2011 17:28
-
-
Save markandrus/1054369 to your computer and use it in GitHub Desktop.
stylecheck.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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