Class: HTML::Proofer

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/html/proofer.rb,
lib/html/proofer/log.rb,
lib/html/proofer/cache.rb,
lib/html/proofer/utils.rb,
lib/html/proofer/version.rb,
lib/html/proofer/checkable.rb,
lib/html/proofer/check_runner.rb,
lib/html/proofer/configuration.rb,
lib/html/proofer/url_validator.rb,
lib/html/proofer/xpathfunctions.rb

Defined Under Namespace

Modules: Configuration, Utils Classes: Cache, CheckRunner, Checkable, Log, UrlValidator, XpathFunctions

Constant Summary collapse

VERSION =
'2.6.1'

Constants included from Utils

Utils::STORAGE_DIR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

clean_content, create_nokogiri, #pluralize, swap

Constructor Details

#initialize(src, opts = {}) ⇒ Proofer

Returns a new instance of Proofer.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/html/proofer.rb', line 25

def initialize(src, opts = {})
  FileUtils.mkdir_p(STORAGE_DIR) unless File.exist?(STORAGE_DIR)

  @src = src

  if opts[:verbose]
    warn '`@options[:verbose]` will be removed in a future 3.x.x release: http://git.io/vGHHh'
  end
  if opts[:href_ignore]
    warn '`@options[:href_ignore]` will be renamed in a future 3.x.x release: http://git.io/vGHHy'
  end

  @proofer_opts = HTML::Proofer::Configuration::PROOFER_DEFAULTS

  @typhoeus_opts = HTML::Proofer::Configuration::TYPHOEUS_DEFAULTS.merge(opts[:typhoeus] || {})
  opts.delete(:typhoeus)

  @hydra_opts = HTML::Proofer::Configuration::HYDRA_DEFAULTS.merge(opts[:hydra] || {})
  opts.delete(:hydra)

  # fall back to parallel defaults
  @parallel_opts = opts[:parallel] || {}
  opts.delete(:parallel)

  @validation_opts = opts[:validation] || {}
  opts.delete(:validation)

  @options = @proofer_opts.merge(opts)

  @failed_tests = []
end

Instance Attribute Details

#external_urlsObject (readonly)

Returns the value of attribute external_urls.



23
24
25
# File 'lib/html/proofer.rb', line 23

def external_urls
  @external_urls
end

#hydra_optsObject (readonly)

Returns the value of attribute hydra_opts.



23
24
25
# File 'lib/html/proofer.rb', line 23

def hydra_opts
  @hydra_opts
end

#iterable_external_urlsObject (readonly)

Returns the value of attribute iterable_external_urls.



23
24
25
# File 'lib/html/proofer.rb', line 23

def iterable_external_urls
  @iterable_external_urls
end

#optionsObject (readonly)

Returns the value of attribute options.



23
24
25
# File 'lib/html/proofer.rb', line 23

def options
  @options
end

#parallel_optsObject (readonly)

Returns the value of attribute parallel_opts.



23
24
25
# File 'lib/html/proofer.rb', line 23

def parallel_opts
  @parallel_opts
end

#typhoeus_optsObject (readonly)

Returns the value of attribute typhoeus_opts.



23
24
25
# File 'lib/html/proofer.rb', line 23

def typhoeus_opts
  @typhoeus_opts
end

#validation_optsObject (readonly)

Returns the value of attribute validation_opts.



23
24
25
# File 'lib/html/proofer.rb', line 23

def validation_opts
  @validation_opts
end

Instance Method Details

#check_directory_of_filesObject

Collects any external URLs found in a directory of files. Also collectes every failed test from check_files_for_internal_woes. Sends the external URLs to Typhoeus for batch processing.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/html/proofer.rb', line 90

def check_directory_of_files
  @external_urls = {}
  results = check_files_for_internal_woes

  results.each do |item|
    @external_urls.merge!(item[:external_urls])
    @failed_tests.concat(item[:failed_tests])
  end

  # TODO: lazy. if we're checking only external links,
  # we'll just trash all the failed tests. really, we should
  # just not run those other checks at all.
  if @options[:external_only]
    @failed_tests = []
    validate_urls
  elsif !@options[:disable_external]
    validate_urls
  end

  count = files.length
  file_text = pluralize(count, 'file', 'files')
  logger.log :info, :blue, "Ran on #{file_text}!\n\n"
end

#check_files_for_internal_woesObject

Walks over each implemented check and runs them on the files, in parallel.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/html/proofer.rb', line 115

def check_files_for_internal_woes
  Parallel.map(files, @parallel_opts) do |path|
    html = create_nokogiri(path)
    result = { :external_urls => {}, :failed_tests => [] }

    checks.each do |klass|
      logger.log :debug, :yellow, "Checking #{klass.to_s.downcase} on #{path} ..."
      check = Object.const_get(klass).new(@src, path, html, @options, @typhoeus_opts, @hydra_opts, @parallel_opts, @validation_opts)
      check.run
      result[:external_urls].merge!(check.external_urls)
      result[:failed_tests].concat(check.issues) if check.issues.length > 0
    end
    result
  end
end


77
78
79
80
81
82
83
84
85
# File 'lib/html/proofer.rb', line 77

def check_list_of_links
  if @options[:href_swap]
    @src = @src.map do |url|
      swap(url, @options[:href_swap])
    end
  end
  @external_urls = Hash[*@src.map { |s| [s, nil] }.flatten]
  validate_urls
end

#checksObject



158
159
160
161
162
163
164
165
166
167
# File 'lib/html/proofer.rb', line 158

def checks
  return @checks unless @checks.nil?
  @checks = HTML::Proofer::CheckRunner.checks.map(&:name)
  @checks.delete('FaviconCheck') unless @options[:check_favicon]
  @checks.delete('HtmlCheck') unless @options[:check_html]
  @options[:checks_to_ignore].each do |ignored|
    @checks.delete(ignored)
  end
  @checks
end

#failed_testsObject



169
170
171
172
173
174
# File 'lib/html/proofer.rb', line 169

def failed_tests
  return [] if @failed_tests.empty?
  result = []
  @failed_tests.each { |f| result << f.to_s }
  result
end

#filesObject



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/html/proofer.rb', line 137

def files
  if File.directory? @src
    pattern = File.join(@src, '**', "*#{@options[:ext]}")
    files = Dir.glob(pattern).select { |fn| File.file? fn }
    files.reject { |f| ignore_file?(f) }
  elsif File.extname(@src) == @options[:ext]
    [@src].reject { |f| ignore_file?(f) }
  else
    []
  end
end

#ignore_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
# File 'lib/html/proofer.rb', line 149

def ignore_file?(file)
  options[:file_ignore].each do |pattern|
    return true if pattern.is_a?(String) && pattern == file
    return true if pattern.is_a?(Regexp) && pattern =~ file
  end

  false
end

#loggerObject



57
58
59
# File 'lib/html/proofer.rb', line 57

def logger
  @logger ||= HTML::Proofer::Log.new(@options[:verbose], @options[:verbosity])
end


176
177
178
179
180
181
182
183
# File 'lib/html/proofer.rb', line 176

def print_failed_tests
  sorted_failures = HTML::Proofer::CheckRunner::SortedIssues.new(@failed_tests, @options[:error_sort], logger)

  sorted_failures.sort_and_report
  count = @failed_tests.length
  failure_text = pluralize(count, 'failure', 'failures')
  fail logger.colorize :red, "HTML-Proofer found #{failure_text}!"
end

#runObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/html/proofer.rb', line 61

def run
  logger.log :info, :blue, "Running #{checks} on #{@src} on *#{@options[:ext]}... \n\n"

  if @src.is_a?(Array) && !@options[:disable_external]
    check_list_of_links
  else
    check_directory_of_files
  end

  if @failed_tests.empty?
    logger.log :info, :green, 'HTML-Proofer finished successfully.'
  else
    print_failed_tests
  end
end

#validate_urlsObject



131
132
133
134
135
# File 'lib/html/proofer.rb', line 131

def validate_urls
  url_validator = HTML::Proofer::UrlValidator.new(logger, @external_urls, @options, @typhoeus_opts, @hydra_opts)
  @failed_tests.concat(url_validator.run)
  @iterable_external_urls = url_validator.iterable_external_urls
end