Saturday, June 7, 2008

Validating (X)HTML using Ruby


This function returns validation messages from the w3c website (http://validator.w3.org)

require 'net/http'
require 'uri'

class XHTML

VALIDATOR_URL = 'http://validator.w3.org/check'
XHTML_STRICT = 'XHTML 1.0 Strict'
XHTML_TRANSITIONAL = 'XHTML 1.0 Transitional'
XHTML_FRAMESET = 'XHTML 1.0 Frameset'

# Returns validation messages
def XHTML.validate(what, xhtml_version=XHTML_STRICT)
post_data = {
'fragment' => what,
'doctype' => xhtml_version
}

r = Net::HTTP.post_form(URI.parse(VALIDATOR_URL), post_data)

return r.body
end

end



We could try to parse the results into an array... maybe in the future.

No comments: