Sha256: 545b71582eb30612d5da993e5d06c3e5a095fc1c7b7d5169114d1cc0b8c0b66b

Contents?: true

Size: 1.83 KB

Versions: 9

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

module Overcommit::Hook::PreCommit
  # Runs `w3c_validators` against any modified HTML files.
  #
  # @see https://github.com/alexdunae/w3c_validators
  class W3cHtml < Base
    def run
      collect_messages
    rescue W3CValidators::ParsingError,
           W3CValidators::ValidatorUnavailable => e
      [:fail, e.message]
    end

    private

    def collect_messages
      applicable_files.collect do |path|
        results = validator.validate_file(path)
        messages = results.errors + results.warnings
        messages.collect do |msg|
          # Some warnings are not per-line, so use 0 as a default
          line = Integer(msg.line || 0)

          # Build message by hand to reduce noise from the validator response
          text = "#{msg.type.to_s.upcase}; URI: #{path}; line #{line}: #{msg.message.strip}"
          Overcommit::Hook::Message.new(msg.type, path, line, text)
        end
      end.flatten
    end

    def validator
      unless @validator
        @validator = W3CValidators::MarkupValidator.new(opts)
        @validator.set_charset!(charset, true) unless charset.nil?
        @validator.set_doctype!(doctype, true) unless doctype.nil?
        @validator.set_debug!
      end
      @validator
    end

    def opts
      @opts ||= {
        validator_uri: config['validator_uri'],
        proxy_server: config['proxy_server'],
        proxy_port: config['proxy_port'],
        proxy_user: config['proxy_user'],
        proxy_pass: config['proxy_pass']
      }
    end

    # Values specified at
    #   http://www.rubydoc.info/gems/w3c_validators/1.2/W3CValidators#CHARSETS
    def charset
      @charset ||= config['charset']
    end

    # Values specified at
    #   http://www.rubydoc.info/gems/w3c_validators/1.2/W3CValidators#DOCTYPES
    def doctype
      @doctype ||= config['doctype']
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
overcommit-0.64.1 lib/overcommit/hook/pre_commit/w3c_html.rb
overcommit-0.64.0 lib/overcommit/hook/pre_commit/w3c_html.rb
overcommit-0.63.0 lib/overcommit/hook/pre_commit/w3c_html.rb
overcommit-0.62.0 lib/overcommit/hook/pre_commit/w3c_html.rb
overcommit-0.61.0 lib/overcommit/hook/pre_commit/w3c_html.rb
overcommit-0.60.0 lib/overcommit/hook/pre_commit/w3c_html.rb
overcommit-0.59.1 lib/overcommit/hook/pre_commit/w3c_html.rb
overcommit-0.59.0 lib/overcommit/hook/pre_commit/w3c_html.rb
overcommit-jeygeethanmedia-0.58.0 lib/overcommit/hook/pre_commit/w3c_html.rb