Skip to content

Instantly share code, notes, and snippets.

@nudded
Created June 3, 2012 18:40
Show Gist options
  • Save nudded/2864553 to your computer and use it in GitHub Desktop.
Save nudded/2864553 to your computer and use it in GitHub Desktop.
W3C validator for css and html
require 'rest_client'
require 'nori'
Nori.configure do |config|
config.strip_namespaces = true
config.convert_tags_to { |tag| tag.snakecase.to_sym }
end
module W3C
class Base
class << self
attr_accessor :base_uri, :file_field
end
def self.check(file)
# make sure we have a File instance
file = File.new(file) if file.kind_of? String
resp = RestClient.post base_uri, file_field => file, output: 'soap12'
file.close
to_errors Nori.parse(resp)[:envelope][:body]
end
private
def self.to_errors(hash)
raise NotImplementedError
end
end
class HTML < Base
self.base_uri = "validator.w3.org/check"
self.file_field = :uploaded_file
def self.to_errors(hash)
hash[:markupvalidationresponse][:errors][:errorlist][:error].map do |e|
Error.new e[:line], e[:message], e[:col]
end
end
end
class CSS < Base
self.base_uri = "http://jigsaw.w3.org/css-validator/validator/"
self.file_field = :file
def self.to_errors(hash)
hash[:cssvalidationresponse][:result][:errors][:errorlist][:error].map do |e|
arr = e[:message].split
arr.delete ':'
Error.new e[:line], arr.join(' ')
end
end
end
class Error
attr_accessor :line, :message, :column
def initialize(line, message, column = nil)
@line, @message, @column = line, message, column
end
def to_s
"#{line_info} => #{message}"
end
private
def line_info
if column
"#{line}:#{column}"
else
line
end
end
end
end
url = File.expand_path ARGV.first
errors = W3C::HTML.check url
errors.each { |e| puts e }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment