Class: Deadweight
- Inherits:
-
Object
- Object
- Deadweight
- Defined in:
- lib/deadweight.rb,
lib/deadweight/cli.rb
Defined Under Namespace
Classes: CLI
Instance Attribute Summary collapse
-
#ignore_selectors ⇒ Object
Returns the value of attribute ignore_selectors.
-
#log_file ⇒ Object
Returns the value of attribute log_file.
-
#mechanize ⇒ Object
Returns the value of attribute mechanize.
-
#pages ⇒ Object
Returns the value of attribute pages.
-
#root ⇒ Object
Returns the value of attribute root.
-
#rules ⇒ Object
Returns the value of attribute rules.
-
#stylesheets ⇒ Object
Returns the value of attribute stylesheets.
-
#unused_selectors ⇒ Object
readonly
Returns the value of attribute unused_selectors.
Instance Method Summary collapse
-
#agent ⇒ Object
Returns the Mechanize instance, if
mechanize
is set totrue
. - #analyze(html) ⇒ Object
-
#fetch(path) ⇒ Object
Fetch a path, using Mechanize if
mechanize
is set totrue
. -
#initialize ⇒ Deadweight
constructor
A new instance of Deadweight.
- #process!(html) ⇒ Object
-
#run ⇒ Object
Find all unused CSS selectors and return them as an array.
Constructor Details
#initialize ⇒ Deadweight
Returns a new instance of Deadweight.
10 11 12 13 14 15 16 17 18 |
# File 'lib/deadweight.rb', line 10 def initialize @root = 'http://localhost:3000' @stylesheets = [] @pages = [] @rules = "" @ignore_selectors = [] @mechanize = false @log_file = STDERR end |
Instance Attribute Details
#ignore_selectors ⇒ Object
Returns the value of attribute ignore_selectors.
7 8 9 |
# File 'lib/deadweight.rb', line 7 def ignore_selectors @ignore_selectors end |
#log_file ⇒ Object
Returns the value of attribute log_file.
7 8 9 |
# File 'lib/deadweight.rb', line 7 def log_file @log_file end |
#mechanize ⇒ Object
Returns the value of attribute mechanize.
7 8 9 |
# File 'lib/deadweight.rb', line 7 def mechanize @mechanize end |
#pages ⇒ Object
Returns the value of attribute pages.
7 8 9 |
# File 'lib/deadweight.rb', line 7 def pages @pages end |
#root ⇒ Object
Returns the value of attribute root.
7 8 9 |
# File 'lib/deadweight.rb', line 7 def root @root end |
#rules ⇒ Object
Returns the value of attribute rules.
7 8 9 |
# File 'lib/deadweight.rb', line 7 def rules @rules end |
#stylesheets ⇒ Object
Returns the value of attribute stylesheets.
7 8 9 |
# File 'lib/deadweight.rb', line 7 def stylesheets @stylesheets end |
#unused_selectors ⇒ Object (readonly)
Returns the value of attribute unused_selectors.
8 9 10 |
# File 'lib/deadweight.rb', line 8 def unused_selectors @unused_selectors end |
Instance Method Details
#agent ⇒ Object
Returns the Mechanize instance, if mechanize
is set to true
.
95 96 97 |
# File 'lib/deadweight.rb', line 95 def agent @agent ||= initialize_agent end |
#analyze(html) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/deadweight.rb', line 20 def analyze(html) doc = Hpricot(html) found_selectors = [] @unused_selectors.collect do |selector, declarations| # We test against the selector stripped of any pseudo classes, # but we report on the selector with its pseudo classes. unless doc.search(strip(selector)).empty? log.info(" #{selector}") selector end end end |
#fetch(path) ⇒ Object
Fetch a path, using Mechanize if mechanize
is set to true
.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/deadweight.rb', line 100 def fetch(path) log.info(path) loc = root + path if @mechanize loc = "file://#{File.(loc)}" unless loc =~ %r{^\w+://} page = agent.get(loc) log.warn("#{path} redirected to #{page.uri}") unless page.uri.to_s == loc page.body else open(loc).read end end |
#process!(html) ⇒ Object
88 89 90 91 92 |
# File 'lib/deadweight.rb', line 88 def process!(html) analyze(html).each do |selector| @unused_selectors.delete(selector) end end |
#run ⇒ Object
Find all unused CSS selectors and return them as an array.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/deadweight.rb', line 36 def run css = CssParser::Parser.new @stylesheets.each do |path| css.add_block!(fetch(path)) end css.add_block!(rules) @unused_selectors = {} total_selectors = 0 css.each_selector do |selector, declarations, specificity| unless @unused_selectors[selector] total_selectors += 1 @unused_selectors[selector] = declarations unless selector =~ ignore_selectors end end # Remove selectors with pseudo classes that already have an equivalent # without the pseudo class. Keep the ones that don't, we need to test # them. @unused_selectors.keys.each do |selector| if has_pseudo_classes(selector) && @unused_selectors.include?(strip(selector)) @unused_selectors.delete(selector) end end pages.each do |page| if page.respond_to?(:read) html = page.read elsif page.respond_to?(:call) result = instance_eval(&page) html = case result when String result else @agent.page.body end else html = fetch(page) end process!(html) end log.info "found #{@unused_selectors.size} unused selectors out of #{total_selectors} total" @unused_selectors end |