Permalink
Browse files

[Documentation] Validator for use on other application.

Signed-off-by: Laurent Arnoud <laurent@spkdev.net>
  • Loading branch information...
1 parent 9afe730 commit 4538e51a93b6a80980553d019a9d4c721fff6c6a @spk committed Sep 4, 2011
Showing with 23 additions and 0 deletions.
  1. +7 −0 README.rdoc
  2. +8 −0 lib/validate_website/core.rb
  3. +8 −0 lib/validate_website/validator.rb
View
@@ -62,6 +62,13 @@ HTML5 support with Validator.nu Web Service.
66::
There are not valid markup and pages not found.
+== On your application ==
+
+ require 'validate_website/validator'
+ body = '<!DOCTYPE html><html></html>'
+ v = ValidateWebsite::Validator.new(Nokogiri::HTML(body), body)
+ v.valid? # => false
+
== REQUIREMENTS
See validate-website.gemspec file.
@@ -180,6 +180,14 @@ def extract_urls_from_css(page)
end
end
+ ##
+ # @param [Nokogiri::HTML::Document] original_doc
+ # @param [String] The raw HTTP response body of the page
+ # @param [String] url
+ # @param [Hash] options
+ # :quiet no output (true, false)
+ # :color color output (true, false)
+ #
def validate(doc, body, url, opts={})
opts = @options.merge(opts)
validator = Validator.new(doc, body)
@@ -1,11 +1,16 @@
# encoding: utf-8
+require 'uri'
+require 'nokogiri'
module ValidateWebsite
class Validator
XHTML_PATH = File.join(File.dirname(__FILE__), '..', '..', 'data', 'schemas')
attr_reader :original_doc, :body, :dtd, :doc, :namespace, :xsd, :errors
+ ##
+ # @param [Nokogiri::HTML::Document] original_doc
+ # @param [String] The raw HTTP response body of the page
def initialize(original_doc, body)
@original_doc = original_doc
@body = body
@@ -37,6 +42,7 @@ def initialize(original_doc, body)
@errors = @xsd.validate(@doc)
elsif document =~ /^\<!DOCTYPE html\>/i
# TODO: use a local Java, Python parser... write a Ruby HTML5 parser ?
+ require 'net/http'
require 'multipart_body'
url = URI.parse('http://validator.nu/')
multipart = MultipartBody.new(:content => document)
@@ -63,6 +69,8 @@ def initialize(original_doc, body)
@errors << e
end
+ ##
+ # @return [Boolean]
def valid?
@errors.length == 0
end

0 comments on commit 4538e51

Please sign in to comment.