Class: W3CValidators::MarkupValidator

Inherits:
Validator
  • Object
show all
Defined in:
lib/w3c_validators/markup_validator.rb

Constant Summary collapse

MARKUP_VALIDATOR_URI =
'http://validator.w3.org/check'

Constants inherited from Validator

Validator::HEAD_ERROR_COUNT_HEADER, Validator::HEAD_STATUS_HEADER, Validator::SOAP_OUTPUT_PARAM, Validator::USER_AGENT, Validator::VERSION

Instance Attribute Summary

Attributes inherited from Validator

#results, #validator_uri

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MarkupValidator

Create a new instance of the MarkupValidator.

Options

The options hash allows you to set request parameters (see validator.w3.org/docs/api.html#requestformat) quickly. Request parameters can also be set using set_charset!, set_debug! and set_doctype!.

You can pass in your own validator’s URI (i.e. MarkupValidator.new(:validator_uri => 'http://localhost/check')).



14
15
16
17
18
19
20
21
22
# File 'lib/w3c_validators/markup_validator.rb', line 14

def initialize(options = {})
  if options[:validator_uri]
    @validator_uri = URI.parse(options[:validator_uri])
    options.delete(options[:validator_uri])
  else
    @validator_uri = URI.parse(MARKUP_VALIDATOR_URI)
  end
  super(options)
end

Instance Method Details

#set_charset!(charset, only_as_fallback = false) ⇒ Object

Specify the character encoding to use when parsing the document.

When only_as_fallback is true, the given encoding will only be used as a fallback value, in case the charset is absent or unrecognized.

charset can be a string (e.g. set_charset!('utf-8')) or a symbol (e.g. set_charset!(:utf_8)) from the W3CValidators::CHARSETS hash.

Has no effect when using validate_uri_quickly.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/w3c_validators/markup_validator.rb', line 34

def set_charset!(charset, only_as_fallback = false)
  if charset.kind_of?(Symbol)
    if CHARSETS.has_key?(charset)
      charset = CHARSETS[charset]
    else
      return
    end
  end
  @options[:charset] = charset
  @options[:fbc] = only_as_fallback
end

#set_debug!(debug = true) ⇒ Object

When set the validator will output some extra debugging information on the validated resource (such as HTTP headers) and validation process (such as parser used, parse mode, etc.).

Debugging information is stored in the Results debug_messages hash. Custom debugging messages can be set with Results#add_debug_message.

Has no effect when using validate_uri_quickly.



77
78
79
# File 'lib/w3c_validators/markup_validator.rb', line 77

def set_debug!(debug = true)
  @options[:debug] = debug
end

#set_doctype!(doctype, only_as_fallback = false) ⇒ Object

Specify the Document Type (DOCTYPE) to use when parsing the document.

When only_as_fallback is true, the given document type will only be used as a fallback value, in case the document’s DOCTYPE declaration is missing or unrecognized.

doctype can be a string (e.g. set_doctype!('HTML 3.2')) or a symbol (e.g. set_doctype!(:html32)) from the W3CValidators::DOCTYPES hash.

Has no effect when using validate_uri_quickly.



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/w3c_validators/markup_validator.rb', line 57

def set_doctype!(doctype, only_as_fallback = false)
  if doctype.kind_of?(Symbol)
    if DOCTYPES.has_key?(doctype)
      doctype = DOCTYPES[doctype]
    else
      return
    end
  end
  @options[:doctype] = doctype
  @options[:fbd] = only_as_fallback
end

#validate_file(file_path) ⇒ Object

Validate the markup of a local file.

file_path may be either the fully-expanded path to the file or an IO object (like File).

Returns W3CValidators::Results.



108
109
110
111
112
113
114
115
116
# File 'lib/w3c_validators/markup_validator.rb', line 108

def validate_file(file_path)
  if file_path.respond_to? :read
    src = file_path.read
  else
    src = read_local_file(file_path)
  end 

  return validate({:uploaded_file => src, :file_path => file_path}, false)
end

#validate_text(text) ⇒ Object

Validate the markup of a string.

Returns W3CValidators::Results.



98
99
100
# File 'lib/w3c_validators/markup_validator.rb', line 98

def validate_text(text)
  return validate({:fragment => text}, false)
end

#validate_uri(uri) ⇒ Object

Validate the markup of an URI using a SOAP request.

Returns W3CValidators::Results.



84
85
86
# File 'lib/w3c_validators/markup_validator.rb', line 84

def validate_uri(uri)
  return validate({:uri => uri}, false)
end

#validate_uri_quickly(uri) ⇒ Object

Validate the markup of an URI using a HEAD request.

Returns W3CValidators::Results with an error count, not full error messages.



91
92
93
# File 'lib/w3c_validators/markup_validator.rb', line 91

def validate_uri_quickly(uri)
  return validate({:uri => uri}, true)
end