Class: MarkupValidity::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/markup_validity/validator.rb

Overview

:nodoc:

Constant Summary collapse

DTD_REFS =
{
  XHTML1_STRICT => [
    'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd',
    'xhtml1-strict.dtd'
  ],
  XHTML1_TRANSITIONAL => [
    'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd',
    'xhtml1-transitional.dtd'
  ],
  XHTML1_RDFA => [
    'http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd',
    'xhtml-rdfa-1.dtd'
  ]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml, reference = XHTML1_TRANSITIONAL) ⇒ Validator

Returns a new instance of Validator.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/markup_validity/validator.rb', line 27

def initialize xml, reference = XHTML1_TRANSITIONAL
  if DTD_REFS[reference]
    fixed_dtd = xml.sub(DTD_REFS[reference].first, DTD_REFS[reference][1])
  else
    fixed_dtd = xml
  end

  doc = Dir.chdir(File.dirname(__FILE__)) do
    Nokogiri::XML(fixed_dtd) { |cfg|
      cfg.noent.dtdload.dtdvalid
    }
  end
  @reference  = reference
  @xml        = xml
  @errors     = reference.validate(doc)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



26
27
28
# File 'lib/markup_validity/validator.rb', line 26

def errors
  @errors
end

Instance Method Details

#inspectObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/markup_validity/validator.rb', line 46

def inspect
  lines = @xml.split("\n")
  strings = []
  errors.each do |error|

    start = error.line - [2, error.line].min

    error_line = error.line == 0 ? 1 : error.line

    strings << "Error on line: #{error_line}:"
    strings << error.message.gsub(/\{[^\}]*\}/, '')
    Range.new(start, error.line + 2).each { |number|
      strings << "#{number + 1}: #{lines[number]}"
    }
    strings << ""
  end
  strings.join("\n")
end

#valid?Boolean

Returns:

  • (Boolean)


44
# File 'lib/markup_validity/validator.rb', line 44

def valid?; @errors.length == 0; end