Created
September 7, 2008 16:04
-
-
Save thecocktail/9281 to your computer and use it in GitHub Desktop.
Rake task to validates massively html files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# # Massive html validation task | |
# | |
# This rake task comes from the nanoc validation task (http://gist.github.com/8961) | |
# Copy this Rakefile to the root of your htmls or add the task to your existing Rakefile | |
# and run: | |
# | |
# rake validate | |
# | |
# and that's all :) | |
task :validate do | |
require 'w3c_validators' | |
include W3CValidators | |
desc "W3C validation of all the files of the current folder" | |
task :validate do | |
validate '.html' | |
# add validate '.theextensionofyourhtml' to extend this task | |
end | |
private | |
# Colorize your output :) | |
def colorize(text, color_code); "#{color_code}#{text}\e[0m"; end | |
def red(text); colorize(text, "\e[31m"); end | |
def green(text); colorize(text, "\e[32m"); end | |
# Validation calling to the w3c_validators methods | |
def validate ext | |
@validator = (ext == ".css" ? CSSValidator.new : MarkupValidator.new ) | |
files(".", true, ext).each do |file| | |
results = @validator.validate_file(file) | |
if results.errors.length > 0 | |
results.errors.each do |err| | |
puts "\t #{file} => #{red(err)}" | |
end | |
else | |
puts "\t #{file} => #{green('Valid!')}" | |
end | |
end | |
end | |
# Stoled from nanoc :) (but with a lot of love) | |
def files(dir, recursively, ext = '') | |
glob = File.join([dir] + (recursively ? [ "**", "*#{ext}" ] : [ "*#{ext}" ])) | |
Dir[glob].reject { |f| File.directory?(f) or f =~ /(~|\.orig|\.rej|\.bak)$/ } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment