2017-08-13 09:02:48 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2010-05-24 12:07:08 -04:00
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# This script validates the generated guides against the W3C Validator.
|
|
|
|
#
|
|
|
|
# Guides are taken from the output directory, from where all .html files are
|
|
|
|
# submitted to the validator.
|
|
|
|
#
|
2012-08-24 05:52:04 -04:00
|
|
|
# This script is prepared to be launched from the guides directory as a rake task:
|
2010-05-24 12:07:08 -04:00
|
|
|
#
|
2012-08-24 05:52:04 -04:00
|
|
|
# rake guides:validate
|
2010-05-24 12:07:08 -04:00
|
|
|
#
|
|
|
|
# If nothing is specified, all files will be validated, but you can check just
|
|
|
|
# some of them using this environment variable:
|
|
|
|
#
|
|
|
|
# ONLY
|
|
|
|
# Use ONLY if you want to validate only one or a set of guides. Prefixes are
|
|
|
|
# enough:
|
|
|
|
#
|
|
|
|
# # validates only association_basics.html
|
2012-08-24 05:52:04 -04:00
|
|
|
# rake guides:validate ONLY=assoc
|
2010-05-24 12:07:08 -04:00
|
|
|
#
|
|
|
|
# Separate many using commas:
|
|
|
|
#
|
2016-07-12 07:12:24 -04:00
|
|
|
# # validates only association_basics.html and command_line.html
|
|
|
|
# rake guides:validate ONLY=assoc,command
|
2010-05-24 12:07:08 -04:00
|
|
|
#
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
2016-08-06 13:21:59 -04:00
|
|
|
require "w3c_validators"
|
2010-05-24 12:07:08 -04:00
|
|
|
include W3CValidators
|
|
|
|
|
|
|
|
module RailsGuides
|
|
|
|
class Validator
|
|
|
|
def validate
|
2017-02-23 15:57:01 -05:00
|
|
|
# https://github.com/w3c-validators/w3c_validators/issues/25
|
|
|
|
validator = NuValidator.new
|
2010-06-18 09:26:07 -04:00
|
|
|
STDOUT.sync = true
|
|
|
|
errors_on_guides = {}
|
2010-05-24 12:07:08 -04:00
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
guides_to_validate.each do |f|
|
2012-08-24 06:06:44 -04:00
|
|
|
begin
|
|
|
|
results = validator.validate_file(f)
|
|
|
|
rescue Exception => e
|
|
|
|
puts "\nCould not validate #{f} because of #{e}"
|
|
|
|
next
|
|
|
|
end
|
2010-05-24 12:07:08 -04:00
|
|
|
|
2017-02-23 15:57:01 -05:00
|
|
|
if results.errors.length > 0
|
2010-06-18 09:26:07 -04:00
|
|
|
print "E"
|
|
|
|
errors_on_guides[f] = results.errors
|
2017-02-23 15:57:01 -05:00
|
|
|
else
|
|
|
|
print "."
|
2010-05-24 12:07:08 -04:00
|
|
|
end
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-06-18 09:26:07 -04:00
|
|
|
show_results(errors_on_guides)
|
2010-05-24 12:07:08 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-05-24 12:07:08 -04:00
|
|
|
private
|
2016-08-06 13:55:02 -04:00
|
|
|
def guides_to_validate
|
|
|
|
guides = Dir["./output/*.html"]
|
|
|
|
guides.delete("./output/layout.html")
|
|
|
|
guides.delete("./output/_license.html")
|
|
|
|
guides.delete("./output/_welcome.html")
|
|
|
|
ENV.key?("ONLY") ? select_only(guides) : guides
|
|
|
|
end
|
2010-05-24 12:07:08 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def select_only(guides)
|
|
|
|
prefixes = ENV["ONLY"].split(",").map(&:strip)
|
|
|
|
guides.select do |guide|
|
2016-08-16 03:30:11 -04:00
|
|
|
prefixes.any? { |p| guide.start_with?("./output/#{p}") }
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2010-05-24 12:07:08 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def show_results(error_list)
|
|
|
|
if error_list.size == 0
|
|
|
|
puts "\n\nAll checked guides validate OK!"
|
|
|
|
else
|
|
|
|
error_summary = error_detail = ""
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
error_list.each_pair do |name, errors|
|
|
|
|
error_summary += "\n #{name}"
|
|
|
|
error_detail += "\n\n #{name} has #{errors.size} validation error(s):\n"
|
|
|
|
errors.each do |error|
|
2016-10-28 23:05:58 -04:00
|
|
|
error_detail += "\n " + error.to_s.delete("\n")
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2010-06-18 09:26:07 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
puts "\n\nThere are #{error_list.size} guides with validation errors:\n" + error_summary
|
|
|
|
puts "\nHere are the detailed errors for each guide:" + error_detail
|
|
|
|
end
|
2010-06-18 09:26:07 -04:00
|
|
|
end
|
2010-05-24 12:07:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-24 14:49:33 -05:00
|
|
|
RailsGuides::Validator.new.validate
|