forked from sds/overcommit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
w3c_css.rb
67 lines (58 loc) · 1.87 KB
/
w3c_css.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
# frozen_string_literal: true
module Overcommit::Hook::PreCommit
# Runs `w3c_validators` against any modified CSS files.
#
# @see https://github.com/alexdunae/w3c_validators
class W3cCss < Base
def run
collect_messages
rescue W3CValidators::ParsingError,
W3CValidators::ValidatorUnavailable => e
[:fail, e.message]
end
private
def collect_messages
applicable_files.collect do |path|
results = validator.validate_file(path)
messages = results.errors + results.warnings
messages.collect do |msg|
# Some warnings are not per-line, so use 0 as a default
line = Integer(msg.line || 0)
# Build message by hand to reduce noise from the validator response
text = "#{msg.type.to_s.upcase}; URI: #{path}; line #{line}: #{msg.message.strip}"
Overcommit::Hook::Message.new(msg.type, path, line, text)
end
end.flatten
end
def validator
unless @validator
@validator = W3CValidators::CSSValidator.new(opts)
@validator.set_language!(language) unless language.nil?
@validator.set_profile!(profile) unless profile.nil?
@validator.set_warn_level!(warn_level) unless warn_level.nil?
end
@validator
end
def opts
@opts ||= {
validator_uri: config['validator_uri'],
proxy_server: config['proxy_server'],
proxy_port: config['proxy_port'],
proxy_user: config['proxy_user'],
proxy_pass: config['proxy_pass']
}
end
def language
@language ||= config['language']
end
# Values specified at
# http://www.rubydoc.info/gems/w3c_validators/1.2/W3CValidators#CSS_PROFILES
def profile
@profile ||= config['profile']
end
# One of 0, 1, 2, 'no'
def warn_level
@warn_level ||= config['warn_level']
end
end
end