css_dead_class 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +7 -0
- data/Gemfile +18 -0
- data/Rakefile +14 -0
- data/Readme.md +82 -0
- data/css_dead_class-1.0.0.gem +0 -0
- data/css_dead_class.gemspec +27 -0
- data/features/support/env.rb +4 -0
- data/lib/css_dead_class.rb +74 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5e81d84c300a27ca3cb3d49d71b0377a91e5b4cc
|
4
|
+
data.tar.gz: 79208456a35ee7c20c67c94a3c648f5a061d222f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ee22edcef063aefffed1a4a96e3ba9da6288f0b35d45572b98a56ccad2298d53edec948f81b5edff447f7efe190e5c86b211c314af558b02cc48a59947a51c91
|
7
|
+
data.tar.gz: 1354350b3bfaff4ea2f483100e2f94a68b1b27afd3286fcfa9afc2bef4a97127efb327943b1b67970a3583a250dd0ad57157707d07831a5380d99df4ed39c835
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# If you do not have OpenSSL installed, update
|
2
|
+
# the following line to use "http://" instead
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in bem_html.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
group :development do
|
9
|
+
gem 'rake'
|
10
|
+
gem 'rdoc'
|
11
|
+
gem 'yard'
|
12
|
+
end
|
13
|
+
|
14
|
+
group :test do
|
15
|
+
gem 'cucumber'
|
16
|
+
gem 'aruba'
|
17
|
+
gem 'rspec'
|
18
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'cucumber/rake/task'
|
5
|
+
|
6
|
+
Cucumber::Rake::Task.new(:cucumber, 'Run features that should pass') do |t|
|
7
|
+
t.cucumber_opts = '--color --tags ~@wip --strict'
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'rake/clean'
|
11
|
+
|
12
|
+
task test: ['cucumber']
|
13
|
+
|
14
|
+
task default: :test
|
data/Readme.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# CSSDeadClass
|
2
|
+
|
3
|
+
Remove undefined CSS class attributes from your HTML.
|
4
|
+
|
5
|
+
## Purpose
|
6
|
+
|
7
|
+
This Gem will, given a set of HTML files and a set of CSS files, remove any mentions in the class attributes of HTML objects classes that are not defined in the CSS files.
|
8
|
+
|
9
|
+
## Sample Usage
|
10
|
+
|
11
|
+
Given the following files
|
12
|
+
|
13
|
+
`index.html`
|
14
|
+
|
15
|
+
~~~html
|
16
|
+
<html>
|
17
|
+
<body>
|
18
|
+
<h1 class="happy sad">Heading</h1>
|
19
|
+
<p class="red blue">Hello</p>
|
20
|
+
</body>
|
21
|
+
</html>
|
22
|
+
~~~
|
23
|
+
|
24
|
+
`app.css`
|
25
|
+
~~~css
|
26
|
+
.red {
|
27
|
+
color: red;
|
28
|
+
}
|
29
|
+
|
30
|
+
.happy {
|
31
|
+
color: black;
|
32
|
+
}
|
33
|
+
~~~
|
34
|
+
|
35
|
+
Running CSSDeadClass on those two files will change `index.html` to the following, remove calls to `.sad` and `.blue`, as they do not exist:
|
36
|
+
|
37
|
+
~~~ html
|
38
|
+
<html>
|
39
|
+
<body>
|
40
|
+
<h1 class="happy">Heading</h1>
|
41
|
+
<p class="red">Hello</p>
|
42
|
+
</body>
|
43
|
+
</html>
|
44
|
+
~~~
|
45
|
+
|
46
|
+
## Installation
|
47
|
+
|
48
|
+
Install using `gem css_dead_class`.
|
49
|
+
|
50
|
+
## Configuration
|
51
|
+
|
52
|
+
CSSDeadClass takes three options:
|
53
|
+
|
54
|
+
### `:css_files`
|
55
|
+
|
56
|
+
An array of file paths to CSS files to scan.
|
57
|
+
|
58
|
+
### `:html_files`
|
59
|
+
|
60
|
+
An array of file paths to HTML files to scan.
|
61
|
+
|
62
|
+
### `:classes_to_keep`
|
63
|
+
|
64
|
+
An array of CSS classes to keep, regardless of presence in the HTML files. Can either be of the form `.classname` or `classname`.
|
65
|
+
|
66
|
+
## Usage
|
67
|
+
|
68
|
+
Sample usage in a ruby program:
|
69
|
+
|
70
|
+
~~~ ruby
|
71
|
+
require 'css_dead_classes'
|
72
|
+
|
73
|
+
css_deadfiles = CSSDeadClass.new({
|
74
|
+
html_files: Dir.glob("**/*.html"),
|
75
|
+
css_files: Dir.glob("**/*.css"),
|
76
|
+
classes_to_keep: [
|
77
|
+
".no-js",
|
78
|
+
"blue"
|
79
|
+
]
|
80
|
+
})
|
81
|
+
css_deadfiles.parse
|
82
|
+
~~~
|
Binary file
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "css_dead_class"
|
6
|
+
s.version = "1.0.1"
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["Andrew Pilsch"]
|
9
|
+
s.email = ["apilsch@tamu.edu"]
|
10
|
+
s.homepage = "https://github.com/oncomouse/css_dead_class"
|
11
|
+
s.summary = %q{Gem that, given a collection of HTML documents and CSS files, will remove any class attributes in the HTML set to CSS selectors that do not exist in the CSS files.}
|
12
|
+
# s.description = %q{A longer description of your extension}
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
# The version of middleman-core your extension depends on
|
20
|
+
s.add_runtime_dependency("nokogiri", ["~> 1.6"])
|
21
|
+
s.add_runtime_dependency("css_parser", ["~> 1.4"])
|
22
|
+
|
23
|
+
s.license = "ISC"
|
24
|
+
|
25
|
+
# Additional dependencies
|
26
|
+
# s.add_runtime_dependency("gem-name", "gem-version")
|
27
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'hashie'
|
2
|
+
require 'css_parser'
|
3
|
+
require 'nokogiri'
|
4
|
+
|
5
|
+
class Config < Hash
|
6
|
+
include Hashie::Extensions::MethodAccessWithOverride
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
class CSSDeadClass
|
11
|
+
def self.config
|
12
|
+
@_config ||= Config.new
|
13
|
+
end
|
14
|
+
def self.option(key, default=nil, description=nil, options={})
|
15
|
+
self.config[key] = Config.new(default: default, description: description, options: {})
|
16
|
+
end
|
17
|
+
option :css_files, [], "CSS Files to Scan"
|
18
|
+
option :html_files, [], "HTML Files to Scan"
|
19
|
+
option :classes_to_keep, [], "Classes to Keep Regardless of Presence in CSS Files"
|
20
|
+
|
21
|
+
def initialize(opts={})
|
22
|
+
@options = self.class.config.dup
|
23
|
+
@options.finalize!
|
24
|
+
opts.each do |k, v|
|
25
|
+
@options[k] = v
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def parse
|
30
|
+
cssSelectors = []
|
31
|
+
cssFiles = @options.css_files
|
32
|
+
htmlFiles = @options.html_files
|
33
|
+
classesToKeep = @options.classes_to_keep
|
34
|
+
if cssFiles.length > 0
|
35
|
+
parser = CssParser::Parser.new
|
36
|
+
cssFiles.each do |cssFile|
|
37
|
+
parser.load_file!(cssFile)
|
38
|
+
end
|
39
|
+
parser.each_selector(:screen).each do |rule|
|
40
|
+
rule = rule[:rules]
|
41
|
+
rule.selectors.each do |segment|
|
42
|
+
segment.split(/(?: |\+|>)/).each do |r|
|
43
|
+
next if r.include? ":"
|
44
|
+
if(r[0] == ".")
|
45
|
+
cssSelectors.push(r)
|
46
|
+
elsif(r.include? ".")
|
47
|
+
s = r.split(".")
|
48
|
+
s.shift
|
49
|
+
s.each { |v| cssSelectors.push('.' + v)}
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
cssSelectors.uniq!
|
55
|
+
end
|
56
|
+
if cssSelectors.length > 0
|
57
|
+
htmlFiles.each do |htmlFile|
|
58
|
+
htmlDoc = Nokogiri::HTML(IO.read(htmlFile))
|
59
|
+
htmlDoc.css('*').each do |node|
|
60
|
+
next if node.attributes.nil? or node.attributes['class'].nil?
|
61
|
+
node['class'] = node.attributes['class'].value.split(/ /).select{ |cn|
|
62
|
+
cssSelectors.include?('.' + cn) || classesToKeep.include?(cn) || classesToKeep.include?('.' + cn)
|
63
|
+
}.join(" ")
|
64
|
+
if(node.attributes['class'].value == "")
|
65
|
+
node.attributes['class'].remove
|
66
|
+
end
|
67
|
+
end
|
68
|
+
File.open(htmlFile, "w") do |fp|
|
69
|
+
fp.puts htmlDoc.to_s
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: css_dead_class
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Pilsch
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-02 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.4'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.4'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- apilsch@tamu.edu
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- Rakefile
|
51
|
+
- Readme.md
|
52
|
+
- css_dead_class-1.0.0.gem
|
53
|
+
- css_dead_class.gemspec
|
54
|
+
- features/support/env.rb
|
55
|
+
- lib/css_dead_class.rb
|
56
|
+
homepage: https://github.com/oncomouse/css_dead_class
|
57
|
+
licenses:
|
58
|
+
- ISC
|
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.5.1
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: Gem that, given a collection of HTML documents and CSS files, will remove
|
80
|
+
any class attributes in the HTML set to CSS selectors that do not exist in the CSS
|
81
|
+
files.
|
82
|
+
test_files:
|
83
|
+
- features/support/env.rb
|