Class: HTML::Proofer
- Inherits:
-
Object
- Object
- HTML::Proofer
- Includes:
- Utils
- Defined in:
- lib/html/proofer.rb,
lib/html/proofer/log.rb,
lib/html/proofer/version.rb,
lib/html/proofer/checkable.rb,
lib/html/proofer/check_runner.rb,
lib/html/proofer/url_validator.rb,
lib/html/proofer/xpathfunctions.rb
Defined Under Namespace
Classes: CheckRunner, Checkable, Log, UrlValidator, XpathFunctions
Constant Summary collapse
- TYPHOEUS_DEFAULTS =
{ :followlocation => true, :headers => { "User-Agent" => "Mozilla/5.0 (compatible; HTML Proofer/#{VERSION}; +https://github.com/gjtorikian/html-proofer)" } }
- VERSION =
'2.4.2'
Instance Attribute Summary collapse
-
#hydra_opts ⇒ Object
readonly
Returns the value of attribute hydra_opts.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parallel_opts ⇒ Object
readonly
Returns the value of attribute parallel_opts.
-
#typhoeus_opts ⇒ Object
readonly
Returns the value of attribute typhoeus_opts.
-
#validation_opts ⇒ Object
readonly
Returns the value of attribute validation_opts.
Instance Method Summary collapse
-
#check_directory_of_files ⇒ Object
Collects any external URLs found in a directory of files.
-
#check_files_for_internal_woes ⇒ Object
Walks over each implemented check and runs them on the files, in parallel.
- #check_list_of_links ⇒ Object
- #checks ⇒ Object
- #failed_tests ⇒ Object
- #files ⇒ Object
- #ignore_file?(file) ⇒ Boolean
-
#initialize(src, opts = {}) ⇒ Proofer
constructor
A new instance of Proofer.
- #logger ⇒ Object
- #print_failed_tests ⇒ Object
- #run ⇒ Object
- #validate_urls(external_urls) ⇒ Object
Methods included from Utils
Constructor Details
#initialize(src, opts = {}) ⇒ Proofer
Returns a new instance of Proofer.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/html/proofer.rb', line 34 def initialize(src, opts = {}) @src = src @proofer_opts = { :ext => '.html', :check_favicon => false, :href_swap => [], :href_ignore => [], :file_ignore => [], :url_ignore => [], :check_external_hash => false, :alt_ignore => [], :empty_alt_ignore => false, :disable_external => false, :verbose => false, :only_4xx => false, :directory_index_file => 'index.html', :check_html => false, :error_sort => :path, :checks_to_ignore => [] } @typhoeus_opts = TYPHOEUS_DEFAULTS.merge(opts[:typhoeus] || {}) opts.delete(:typhoeus) @hydra_opts = opts[:hydra] || {} opts.delete(:hydra) # fall back to parallel defaults @parallel_opts = opts[:parallel] || {} opts.delete(:parallel) @validation_opts = opts[:validation] || {} opts.delete(:validation) = @proofer_opts.merge(opts) @failed_tests = [] end |
Instance Attribute Details
#hydra_opts ⇒ Object (readonly)
Returns the value of attribute hydra_opts.
25 26 27 |
# File 'lib/html/proofer.rb', line 25 def hydra_opts @hydra_opts end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
25 26 27 |
# File 'lib/html/proofer.rb', line 25 def end |
#parallel_opts ⇒ Object (readonly)
Returns the value of attribute parallel_opts.
25 26 27 |
# File 'lib/html/proofer.rb', line 25 def parallel_opts @parallel_opts end |
#typhoeus_opts ⇒ Object (readonly)
Returns the value of attribute typhoeus_opts.
25 26 27 |
# File 'lib/html/proofer.rb', line 25 def typhoeus_opts @typhoeus_opts end |
#validation_opts ⇒ Object (readonly)
Returns the value of attribute validation_opts.
25 26 27 |
# File 'lib/html/proofer.rb', line 25 def validation_opts @validation_opts end |
Instance Method Details
#check_directory_of_files ⇒ Object
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.
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/html/proofer.rb', line 109 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 validate_urls(external_urls) unless [:disable_external] logger.log :info, :blue, "Ran on #{files.length} files!\n\n" end |
#check_files_for_internal_woes ⇒ Object
Walks over each implemented check and runs them on the files, in parallel.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/html/proofer.rb', line 124 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, , @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 |
#check_list_of_links ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/html/proofer.rb', line 96 def check_list_of_links if [:href_swap] @src = @src.map do |external_url| swap(external_url, [:href_swap]) end end external_urls = Hash[*@src.map { |s| [s, nil] }.flatten] validate_urls(external_urls) end |
#checks ⇒ Object
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/html/proofer.rb', line 166 def checks return @checks unless @checks.nil? @checks = HTML::Proofer::CheckRunner.checks.map(&:name) @checks.delete('FaviconCheck') unless [:check_favicon] @checks.delete('HtmlCheck') unless [:check_html] [:checks_to_ignore].each do |ignored| @checks.delete(ignored) end @checks end |
#failed_tests ⇒ Object
177 178 179 180 181 182 |
# File 'lib/html/proofer.rb', line 177 def failed_tests return [] if @failed_tests.empty? result = [] @failed_tests.each { |f| result << f.to_s } result end |
#files ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/html/proofer.rb', line 145 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) == [:ext] [@src].reject { |f| ignore_file?(f) } else [] end end |
#ignore_file?(file) ⇒ Boolean
157 158 159 160 161 162 163 164 |
# File 'lib/html/proofer.rb', line 157 def ignore_file?(file) [: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 |
#logger ⇒ Object
74 75 76 |
# File 'lib/html/proofer.rb', line 74 def logger @logger ||= HTML::Proofer::Log.new([:verbose]) end |
#print_failed_tests ⇒ Object
184 185 186 187 188 189 190 191 |
# File 'lib/html/proofer.rb', line 184 def print_failed_tests sorted_failures = HTML::Proofer::CheckRunner::SortedIssues.new(@failed_tests, [:error_sort], logger) sorted_failures.sort_and_report count = @failed_tests.length failure_text = "#{count} " << (count == 1 ? 'failure' : 'failures') fail logger.colorize :red, "HTML-Proofer found #{failure_text}!" end |
#run ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/html/proofer.rb', line 78 def run count = checks.length check_text = "#{checks} " << (count == 1 ? 'check' : 'checks') logger.log :info, :blue, "Running #{check_text} on #{@src} on *#{@options[:ext]}... \n\n" if @src.is_a?(Array) && ![: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_urls(external_urls) ⇒ Object
140 141 142 143 |
# File 'lib/html/proofer.rb', line 140 def validate_urls(external_urls) url_validator = HTML::Proofer::UrlValidator.new(logger, external_urls, , @typhoeus_opts, @hydra_opts) @failed_tests.concat(url_validator.run) end |