thecocktail (owner)

Forks

Revisions

gist: 8961 Download_button fork
public
Description:
Nanoc rake task to make a w3c validation of your output html/css
Public Clone URL: git://gist.github.com/8961.git
Embed All Files: show embed
validate.rake #
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
# #Nanoc validation task
#
# To use this validation task you need the w3c_validators gem
# gem install w3c_validators
# and run rake validate on your project root
#
require 'yaml'
require 'w3c_validators'
include W3CValidators
 
desc "W3C validation of the output folder"
task :validate do
  # perform a setup of all our variables
  setup
  validate '.html'
  validate '.css'
end
 
 
private
 
# Colorize the 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
 
# Reads the yaml with the configuration of the project to get always the correct
# output_dir and initializes the validator
def setup
  @config = YAML.load_file("config.yaml")
end
 
 
# Method to validate calling to the w3c_validators methods
def validate ext
  @validator = (ext == ".css" ? CSSValidator.new : MarkupValidator.new )
  
  files(@config['output_dir'], 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
 
# From nanoc
# # Returns a list of all files in +dir+, ignoring any unwanted files (files
# that end with '~', '.orig', '.rej' or '.bak').
#
# +recursively+:: When +true+, finds files in +dir+ as well as its
# subdirectories; when +false+, only searches +dir+
# itself.
 
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