Sha256: 6cf7cdd0b4321f213303f3e055aa4fea1110f9bccf82de15bbb4a31505083a91

Contents?: true

Size: 1.35 KB

Versions: 12

Compression:

Stored size: 1.35 KB

Contents

# encoding: utf-8

module Nanoc3::Extra::Validators

  # Nanoc3::Extra::Validators::W3C is a validator that uses the W3C web
  # service to validate HTML and CSS files.
  class W3C

    def initialize(site, type)
      @site = site
      @type = type
    end

    def run
      # Load validator
      require 'w3c_validators'

      # Make sure config is loaded
      @site.load_data

      # Find all files
      files = extensions.map { |extension| Dir["#{@site.config[:output_dir]}/**/*.#{extension}"] }.flatten

      # Validate each file
      files.each do |file|
        validation_started(file)
        results = validator.validate_file(file)
        validation_ended(file, results.errors)
      end
    end

  private

    def extensions
      case @type
      when :html
        [ 'html', 'htm' ]
      when :css
        [ 'css' ]
      end
    end

    def validator_class
      case @type
      when :html
        ::W3CValidators::MarkupValidator
      when :css
        ::W3CValidators::CSSValidator
      end
    end

    def validator
      @validator ||= validator_class.new
    end

    def validation_started(file)
      $stdout.print "Validating #{file}... "
      $stdout.flush
    end

    def validation_ended(file, errors)
      $stdout.puts(errors.empty? ? "valid" : "INVALID")

      errors.each do |err|
        puts "    #{err}"
      end
    end

  end

end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
nanoc3-3.0.9 lib/nanoc3/extra/validators/w3c.rb
nanoc3-3.0.8 lib/nanoc3/extra/validators/w3c.rb
nanoc3-3.1.0a2 lib/nanoc3/extra/validators/w3c.rb
nanoc3-3.1.0a1 lib/nanoc3/extra/validators/w3c.rb
nanoc3-3.0.7 lib/nanoc3/extra/validators/w3c.rb
nanoc3-3.0.6 lib/nanoc3/extra/validators/w3c.rb
nanoc3-3.0.5 lib/nanoc3/extra/validators/w3c.rb
nanoc3-3.0.4 lib/nanoc3/extra/validators/w3c.rb
nanoc3-3.0.3 lib/nanoc3/extra/validators/w3c.rb
nanoc3-3.0.2 lib/nanoc3/extra/validators/w3c.rb
nanoc3-3.0.1 lib/nanoc3/extra/validators/w3c.rb
nanoc3-3.0.0 lib/nanoc3/extra/validators/w3c.rb