Guest User

Untitled

a guest
Feb 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. # #Nanoc validation task
  2. #
  3. # To use this validation task you need the w3c_validators gem
  4. # gem install w3c_validators
  5. # and run rake validate on your project root
  6. #
  7. require 'yaml'
  8. require 'w3c_validators'
  9. include W3CValidators
  10.  
  11. desc "W3C validation of the output folder"
  12. task :validate do
  13. # perform a setup of all our variables
  14. setup
  15. validate '.html'
  16. validate '.css'
  17. end
  18.  
  19.  
  20. private
  21.  
  22. # Colorize the output :)
  23. def colorize(text, color_code); "#{color_code}#{text}\e[0m"; end
  24. def red(text); colorize(text, "\e[31m"); end
  25. def green(text); colorize(text, "\e[32m"); end
  26.  
  27. # Reads the yaml with the configuration of the project to get always the correct
  28. # output_dir and initializes the validator
  29. def setup
  30. @config = YAML.load_file("config.yaml")
  31. end
  32.  
  33.  
  34. # Method to validate calling to the w3c_validators methods
  35. def validate ext
  36. @validator = (ext == ".css" ? CSSValidator.new : MarkupValidator.new )
  37.  
  38. files(@config['output_dir'], true, ext).each do |file|
  39. results = @validator.validate_file(file)
  40. if results.errors.length > 0
  41. results.errors.each do |err|
  42. puts "\t #{file} => #{red(err)}"
  43. end
  44. else
  45. puts "\t #{file} => #{green('Valid!')}"
  46. end
  47. end
  48.  
  49. end
  50.  
  51. # From nanoc
  52. # # Returns a list of all files in +dir+, ignoring any unwanted files (files
  53. # that end with '~', '.orig', '.rej' or '.bak').
  54. #
  55. # +recursively+:: When +true+, finds files in +dir+ as well as its
  56. # subdirectories; when +false+, only searches +dir+
  57. # itself.
  58.  
  59. def files(dir, recursively, ext = '')
  60. glob = File.join([dir] + (recursively ? [ "**", "*#{ext}" ] : [ "*#{ext}" ]))
  61. Dir[glob].reject { |f| File.directory?(f) or f =~ /(~|\.orig|\.rej|\.bak)$/ }
  62. end
Add Comment
Please, Sign In to add comment