lib/headhunter/css_validator.rb in headhunter-0.1.7 vs lib/headhunter/css_validator.rb in headhunter-0.1.8

- old
+ new

@@ -1,5 +1,7 @@ +require 'open3' +require 'colorize' require 'net/http' require 'nokogiri/xml' module Headhunter class CssValidator @@ -10,26 +12,30 @@ def initialize(stylesheets = [], profile = 'css3', vextwarning = true) @stylesheets = stylesheets @profile = profile # TODO! @vextwarning = vextwarning # TODO! - @responses = @stylesheets.map do |stylesheet| - validate(stylesheet) - end + @responses = [] + + @stylesheets.map do |stylesheet| + validate(stylesheet) + end end def validate(uri) - # See http://stackoverflow.com/questions/1137884/is-there-an-open-source-css-validator-that-can-be-run-locally - # More config options see http://jigsaw.w3.org/css-validator/manual.html - results = if File.exists?(uri) - # TODO: Better use Open3.popen3! - Dir.chdir(VALIDATOR_DIR) { `java -jar css-validator.jar --output=soap12 file:#{uri}` } - else - raise "Couldn't locate uri #{uri}" - end + Dir.chdir(VALIDATOR_DIR) do + raise "Couldn't locate uri #{uri}" unless File.exists? uri - Response.new(results) + # See http://stackoverflow.com/questions/1137884/is-there-an-open-source-css-validator-that-can-be-run-locally + # More config options see http://jigsaw.w3.org/css-validator/manual.html + stdin, stdout, stderr = Open3.popen3("java -jar css-validator.jar --output=soap12 file:#{uri}") + stdin.close + stderr.close + + @responses << Response.new(stdout.read) + stdout.close + end end def valid_responses @responses.select(&:valid?) end @@ -40,11 +46,11 @@ def statistics lines = [] lines << "Validated #{responses.size} stylesheets.".yellow - lines << "All stylesheets are valid.".green if invalid_responses.size == 0 - lines << "#{x_stylesheets_be(invalid_responses.size)} invalid.".red if invalid_responses.size > 0 + lines << "All stylesheets are valid.".green unless invalid_responses.any? + lines << "#{x_stylesheets_be(invalid_responses.size)} invalid.".red if invalid_responses.any? invalid_responses.each do |response| lines << " #{extract_filename(response.uri)}:".red response.errors.each do |error|