-
Notifications
You must be signed in to change notification settings - Fork 52
/
validate.rb
executable file
·133 lines (122 loc) · 3.52 KB
/
validate.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env ruby
#
# Original accessed in July 2015 by Simon Sigurdhsson.
# https://github.com/urdh/blog/blob/gh-pages/validate.rb
#
# Modifications in July 2015 by Brandon Amos <http://bamos.github.io>.
# https://github.com/bamos/bamos.github.io/blob/master/validate.rb
require 'rubygems'
require 'nokogiri'
require 'html5_validator'
require 'w3c_validators'
require 'colorize'
require 'open-uri'
puts "Validating jekyll output in '_site/'..."
puts "\n"
failed = 0
passed = 0
skipped = 0
IGNORED_FILES = [
'_site/data/2014-07-28/blueimp-gallery.css',
'_site/data/2014-12-15/Blueprint-VerticalTimeline/css/component.css',
'_site/css/font-awesome.min.css'
]
class XMLValidator < W3CValidators::Validator
def validate_file(file)
if file.respond_to? :read
src = file.read
file_path = file.path ||= nil
else
src = read_local_file(file)
file_path = file
end
begin
document = Nokogiri::XML(src)
@results = if document.xpath('*/@xsi:schemaLocation').empty?
W3CValidators::Results.new({:uri => nil, :validity => true})
else
schema_uri = document.xpath('*/@xsi:schemaLocation').to_s.split[1]
schema = Nokogiri::XML::Schema(open(schema_uri).read)
errors = schema.validate(document)
r = W3CValidators::Results.new(
{:uri => nil, :validity => errors.empty?}
)
errors.each { |msg|
r.add_error({ :message => msg.to_s }) if msg.error?
}
r
end
rescue
@results = W3CValidators::Results.new(
{:uri => nil, :validity => false}
)
@results.add_error({ :message => 'Nokogiri threw errors on input.' })
end
@results
end
end
class HtmlValidator < W3CValidators::Validator
def validate_file(file)
if file.respond_to? :read
src = file.read
file_path = file.path ||= nil
else
src = read_local_file(file)
file_path = file
end
validator = Html5Validator::Validator.new
validator.validate_text(src)
@results = W3CValidators::Results.new(
{:uri => nil, :validity => validator.valid?}
)
validator.errors.each { |err|
@results.add_error({ :message => err['message'] })
}
@results
end
end
Dir.glob("_site/**/*") do |file|
next if File.directory?(file)
next if IGNORED_FILES.include? file
next if file.include? "vendor"
validator = case File.extname(file)
when '.html'
HtmlValidator.new
when '.xml'
if File.basename(file) == 'atom.xml'
W3CValidators::FeedValidator.new
else
XMLValidator.new
end
when '.css'
W3CValidators::CSSValidator.new
else
skipped += 1
# puts file.colorize(:light_black)
next
end
errors = nil
begin
errors = validator.validate_file(file).errors
rescue => e
puts
puts file
puts ' + Warning: Unable to validate:'
puts '==========='
puts e
puts '==========='
puts
end
if errors.nil?
elsif errors.empty?
puts file.colorize(:green)
passed += 1
else
puts file.colorize(:red)
puts errors
failed += 1
end
end
puts "\n"
puts "#{passed} files pass validation, #{failed} files failed."
exit failed