cesspit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 969ed71d9c70ae5d9d2487b78ba3c2fb26c0e46c
4
+ data.tar.gz: 5c8d0730941808d903a13d89e61c6fc2110c417e
5
+ SHA512:
6
+ metadata.gz: 44822c6d1fe2d558a36af08c8aef6e63803ed69b8d36f344063015c8d58ff75e0fabdcbe8ae704887b989eb3dad0a028e363e89f2217a5dae5e77665e3bc9b1d
7
+ data.tar.gz: cd0e24857d9d053f78889530fb79ddaa72b5f1771fc1532ab2aa82abd5b595fcc77f50002f90f8a94ee369c6f07e4def864ed0f7852bb5c5c20dea6979c557fd
@@ -0,0 +1,70 @@
1
+ Cesspit
2
+ =======
3
+
4
+ Clean up your stinking pit of css.
5
+
6
+ Cesspit finds unused CSS by comparing your css to examples of your HTML. You can either provide HTML files, or integrate Cesspit with existing test suites.
7
+
8
+ Standalone Usage
9
+ ----------------
10
+
11
+ You can analyze CSS using the standalone `cesspit` script:
12
+
13
+ ~~~ shell
14
+ $ cesspit --css test/fixtures/colors.css --html test/fixtures/red.html
15
+ test/fixtures/colors.css
16
+ .blue
17
+ ~~~
18
+
19
+ You may pass as many CSS and HTML documents as you like in. When Cesspit is done, it will print out unused selectors and the CSS files they were declared in.
20
+
21
+ Test Integration
22
+ ----------------
23
+
24
+ Why bother generating piles of HTML documents when your tests already do it for you?
25
+
26
+ Cesspit integrates with Minitest.
27
+
28
+ ~~~ ruby
29
+ # When run individually
30
+ ruby test/home_controller_test.rb --cesspit public/application.css
31
+
32
+ # With Rake:
33
+ TESTOPTS="--cesspit public/application.css" rake test
34
+ ~~~
35
+
36
+ TODO
37
+ ----
38
+
39
+ * Add support for other test frameworks.
40
+
41
+ LICENSE:
42
+ --------
43
+
44
+ (The MIT License)
45
+
46
+ Copyright (c) Adam Sanderson
47
+
48
+ Permission is hereby granted, free of charge, to any person obtaining
49
+ a copy of this software and associated documentation files (the
50
+ 'Software'), to deal in the Software without restriction, including
51
+ without limitation the rights to use, copy, modify, merge, publish,
52
+ distribute, sublicense, and/or sell copies of the Software, and to
53
+ permit persons to whom the Software is furnished to do so, subject to
54
+ the following conditions:
55
+
56
+ The above copyright notice and this permission notice shall be
57
+ included in all copies or substantial portions of the Software.
58
+
59
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
60
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
61
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
62
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
63
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
64
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
65
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
66
+
67
+
68
+ -----
69
+
70
+ Adam Sanderson, http://www.monkeyandcrow.com
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require_relative '../lib/cesspit'
5
+ require_relative '../lib/cesspit/cli'
6
+
7
+ options = {
8
+ html_paths: [],
9
+ css_paths: [],
10
+ }
11
+ read_from_stdin = nil
12
+
13
+ opts = OptionParser.new
14
+
15
+ opts.banner = "Usage: #{$0} --html a.html,b.html --css a.css,b.css"
16
+
17
+ opts.on("--html PATHS", Array, "Comma separated list of html documents to scan") do |paths|
18
+ options[:html_paths] += paths
19
+ end
20
+
21
+ opts.on("--css PATHS", Array, "Comma separated list of css paths to scan") do |paths|
22
+ options[:css_paths] += paths
23
+ end
24
+
25
+ opts.on("--[no]-stream", "Read files from STDIN, delimited by ASCII:\\28") do |enabled|
26
+ read_from_stdin = enabled
27
+ end
28
+
29
+ opts.on("-v","--version", "Print version") do |enabled|
30
+ puts Cesspit::VERSION
31
+ exit
32
+ end
33
+
34
+ opts.on_tail("-h", "--help", "Show this message") do
35
+ puts opts
36
+ exit
37
+ end
38
+
39
+ opts.parse!(ARGV)
40
+
41
+ if options[:css_paths].empty?
42
+ puts "--css is required.\n"
43
+ puts opts
44
+ exit 1
45
+ end
46
+
47
+ unless read_from_stdin === false
48
+ if ARGV.last == "--"
49
+ ARGV.pop
50
+ read_from_stdin = true
51
+ elsif !STDIN.tty?
52
+ read_from_stdin = true
53
+ end
54
+ end
55
+
56
+ options[:html_paths] += ARGV
57
+
58
+ cli = Cesspit::CLI.new(options)
59
+ cli.read_from(STDIN) if read_from_stdin
60
+ cli.report
@@ -0,0 +1,111 @@
1
+ require 'nokogiri'
2
+ require 'css_parser'
3
+ require_relative './version'
4
+
5
+ class Cesspit
6
+ # http://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text
7
+ FILE_DELIMITER = 28.chr
8
+ IGNORED_PSEUDO_SELECTORS = /
9
+ (:active)|(:hover)|(:focus)|(:disabled)|(:checked) # Ignore stateful selectors
10
+ (\:before)|(:after) # Ignore content selectors
11
+ |(\:\:[\w+\-]+) # Ignore pseudo elements
12
+ /xi
13
+
14
+ attr_reader :selectors_by_source
15
+ attr_accessor :io
16
+
17
+ def initialize
18
+ @selectors_by_source = {}
19
+ @io = STDERR
20
+ end
21
+
22
+ def read_css(path)
23
+ open(path, "r") do |io|
24
+ add_css(io.read, path)
25
+ end
26
+ end
27
+
28
+ def add_css(text, path=nil)
29
+ path ||= "anonymous_#{selectors_by_source.length}.css"
30
+
31
+ if !has_css_path?(path)
32
+ @selectors_by_source[path] = extract_selectors(text)
33
+ end
34
+ end
35
+
36
+ def has_css_path?(path)
37
+ selectors_by_source.has_key? path
38
+ end
39
+
40
+ def read_html(path)
41
+ open(path, "r") do |io|
42
+ scan_html(io.read)
43
+ end
44
+ end
45
+
46
+ def scan_html(html)
47
+ doc = Nokogiri::HTML(html)
48
+
49
+ selectors_by_source.each do |path, selectors|
50
+ selectors.reject! do |selector|
51
+ begin
52
+ doc.at_css(selector)
53
+ rescue StandardError => ex
54
+ io.puts "Could not process #{selector.inspect}"
55
+ true
56
+ end
57
+ end
58
+ end
59
+ end
60
+
61
+ def unmatched_selectors
62
+ selectors_by_source.values.flatten
63
+ end
64
+
65
+ def to_s
66
+ out = []
67
+ selectors_by_source.each do |path, selectors|
68
+ if !selectors.empty?
69
+ out << "#{path} (#{selectors.length})"
70
+
71
+ selectors.each do |selector|
72
+ out << "\t#{selector}"
73
+ end
74
+ end
75
+ end
76
+
77
+ if out.empty?
78
+ ""
79
+ else
80
+ "Cesspit:\n" + out.join("\n")
81
+ end
82
+ end
83
+
84
+ private
85
+
86
+ def extract_selectors(text)
87
+ parser = CssParser::Parser.new(
88
+ :absolute_paths => false,
89
+ :import => false
90
+ )
91
+
92
+ all_selectors = []
93
+ parser.add_block!(text)
94
+ parser.each_selector do |selector, declarations, specificity, media_types|
95
+ scrubbed = rewrite_pseudo_selectors(selector)
96
+ all_selectors << scrubbed
97
+ end
98
+
99
+ all_selectors
100
+ end
101
+
102
+ def rewrite_pseudo_selectors(selector)
103
+ selector.gsub(IGNORED_PSEUDO_SELECTORS) do |match|
104
+ if $`[-1] =~ /[\w\*\]]/
105
+ "" # .div:hover
106
+ else
107
+ "*" # .div > :hover
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,32 @@
1
+ class Cesspit::CLI
2
+ attr_reader :cesspit
3
+ attr_reader :in, :out
4
+
5
+ def initialize(options)
6
+ @cesspit = Cesspit.new
7
+ @in = options[:in]
8
+ @out = options[:out] || STDOUT
9
+
10
+ options[:css_paths].each do |path|
11
+ cesspit.read_css(path)
12
+ end
13
+
14
+ if options[:html_paths]
15
+ options[:html_paths].each do |path|
16
+ cesspit.read_html(path)
17
+ end
18
+ end
19
+
20
+ end
21
+
22
+ def read_from(stream)
23
+ stream.each(Cesspit::FILE_DELIMITER) do |html|
24
+ cesspit.scan_html(html)
25
+ end
26
+ end
27
+
28
+ def report
29
+ out.puts(cesspit.to_s)
30
+ end
31
+
32
+ end
@@ -0,0 +1,31 @@
1
+ require_relative '../cesspit'
2
+
3
+ module Cesspit::MinitestScanner
4
+
5
+ def self.cesspit= cesspit
6
+ @cesspit = cesspit
7
+ end
8
+
9
+ def self.cesspit
10
+ @cesspit ||= Cesspit.new
11
+ end
12
+
13
+ def before_teardown
14
+ response = instance_variable_get(:@response)
15
+ response ||= self.response if respond_to?(:response)
16
+
17
+ if response &&
18
+ response.respond_to?(:body) &&
19
+ response.respond_to?(:content_type) &&
20
+ response.respond_to?(:status)
21
+
22
+ if response.content_type.to_s.downcase == "text/html" &&
23
+ (response.status/100 != 3)
24
+
25
+ Cesspit::MinitestScanner.cesspit.scan_html(response.body)
26
+ end
27
+ end
28
+
29
+ super
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ require_relative '../cesspit/minitest_scanner'
2
+
3
+ # Minitest Plugin for Minitest 5.x
4
+ module Minitest
5
+ def self.plugin_cesspit_options(opts, options)
6
+ opts.on "--cesspit PATHS", Array, "Enable Cesspit response scanning" do |paths|
7
+ options[:cesspit] = paths
8
+ end
9
+ end
10
+
11
+ def self.plugin_cesspit_init(options)
12
+ paths = options[:cesspit] || []
13
+ if !paths.empty?
14
+ # Include hooks for MinitestScanner
15
+ Minitest::Test.send :include, Cesspit::MinitestScanner
16
+ cesspit = Cesspit::MinitestScanner.cesspit
17
+
18
+ paths.each{|path| cesspit.read_css(path) }
19
+
20
+ # Set up CesspitReporter
21
+ self.reporter << CesspitReporter.new(options[:io], options)
22
+ end
23
+ end
24
+
25
+ class CesspitReporter < Minitest::Reporter
26
+ def report
27
+ io.puts Cesspit::MinitestScanner.cesspit
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,3 @@
1
+ class Cesspit
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cesspit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Sanderson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: css_parser
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
41
+ description: Scans and finds unused css declarations.
42
+ email:
43
+ - netghost@gmail.com
44
+ executables:
45
+ - cesspit
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - README.markdown
50
+ - bin/cesspit
51
+ - lib/cesspit.rb
52
+ - lib/cesspit/cli.rb
53
+ - lib/cesspit/minitest_scanner.rb
54
+ - lib/minitest/cesspit_plugin.rb
55
+ - lib/version.rb
56
+ homepage: https://github.com/adamsanderson/cesspit
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.4.6
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Clean up your stinking CeSSPit (see that's a joke there).
80
+ test_files: []