Module: Middleman::Xmlvalidator

Defined in:
lib/middleman/xmlvalidator/version.rb,
lib/middleman/xmlvalidator/extension.rb

Constant Summary collapse

VERSION =
"0.2.1"
PACKAGE =
"middleman-xmlvalidator"

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object Also known as: included



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/middleman/xmlvalidator/extension.rb', line 4

def registered(app)
  require 'nokogiri'

  app.after_build do |builder|
    puts "", "Validating with NokoGiri", ""

    Dir.glob("build/**/*.{xml,rss}").each do |full_path|
      Xmlvalidator.validate_file(full_path)
    end

    puts "", "Validation with NokoGiri " + "Complete".green, ""
  end
end

.validate(document_path, schema_path) ⇒ Object



32
33
34
35
36
# File 'lib/middleman/xmlvalidator/extension.rb', line 32

def self.validate(document_path, schema_path)
  schema = Nokogiri::XML::Schema(File.read(schema_path))     
  document = Nokogiri::XML(File.read(document_path))
  schema.validate(document)
end

.validate_file(file_path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/middleman/xmlvalidator/extension.rb', line 21

def self.validate_file(file_path)
  file_name = file_path.split('/').last
  validator_file = file_name.gsub(/\.\w*/, '.xsd')
  schema_path = File.join(File.dirname(__FILE__), '/schema/')
  schema_path += File.exists?(schema_path + validator_file) ? validator_file : (file_name.end_with?('.rss') ? "RSSSchema.xsd" : "XMLSchema.xsd")

  errors = Xmlvalidator.validate(file_path, schema_path)
  puts "  validating".blue + "  #{file_path}....." + (errors.size == 0 ? "COMPLETE".green : "ERRORS FOUND".red)
  errors.each { |error| puts "     " + error.message }
end