thecocktail (owner)

Revisions

gist: 9281 Download_button fork
public
Description:
Rake task to validates massively html files
Public Clone URL: git://gist.github.com/9281.git
Embed All Files: show embed
Text #
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
# # 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