Class: HTML::Proofer
- Inherits:
-
Object
- Object
- HTML::Proofer
- Includes:
- Yell::Loggable
- Defined in:
- lib/html/proofer.rb,
lib/html/proofer/checks.rb,
lib/html/proofer/checkable.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#failed_tests ⇒ Object
Returns the value of attribute failed_tests.
Class Method Summary collapse
Instance Method Summary collapse
- #colorize(color, string) ⇒ Object
-
#external_link_checker(external_urls) ⇒ Object
the hypothesis is that Proofer runs way faster if we pull out all the external URLs and run the checks at the end.
- #files ⇒ Object
- #get_checks ⇒ Object
- #hydra ⇒ Object
-
#initialize(src, opts = {}) ⇒ Proofer
constructor
A new instance of Proofer.
- #log_level ⇒ Object
- #queue_request(method, href, filenames) ⇒ Object
- #response_handler(response, filenames) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(src, opts = {}) ⇒ Proofer
Returns a new instance of Proofer.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/html/proofer.rb', line 13 def initialize(src, opts={}) @src = src @proofer_opts = { :ext => ".html", :favicon => false, :href_swap => [], :href_ignore => [], :alt_ignore => [], :disable_external => false, :verbose => false, :as_link_array => false } @options = @proofer_opts.merge({:followlocation => true}).merge(opts) @failed_tests = [] Yell.new({ :format => false, :name => "HTML::Proofer", :level => "gte.#{log_level}" }) do |l| l.adapter :stdout, level: [:debug, :info, :warn] l.adapter :stderr, level: [:error, :fatal] end end |
Instance Attribute Details
#failed_tests ⇒ Object
Returns the value of attribute failed_tests.
11 12 13 |
# File 'lib/html/proofer.rb', line 11 def failed_tests @failed_tests end |
Class Method Details
.create_nokogiri(path) ⇒ Object
148 149 150 151 152 |
# File 'lib/html/proofer.rb', line 148 def self.create_nokogiri(path) path << "/index.html" if File.directory? path # support for Jekyll-style links content = File.open(path, "rb") {|f| f.read } Nokogiri::HTML(content) end |
Instance Method Details
#colorize(color, string) ⇒ Object
164 165 166 167 168 169 170 |
# File 'lib/html/proofer.rb', line 164 def colorize(color, string) if $stdout.isatty && $stderr.isatty Colored.colorize(string, :foreground => color) else string end end |
#external_link_checker(external_urls) ⇒ Object
the hypothesis is that Proofer runs way faster if we pull out all the external URLs and run the checks at the end. Otherwise, we’re halting the consuming process for every file. In addition, sorting the list lets libcurl keep connections to hosts alive. Finally, we’ll make a HEAD request, rather than GETing all the contents
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/html/proofer.rb', line 80 def external_link_checker(external_urls) external_urls = Hash[external_urls.sort] logger.info colorize :yellow, "Checking #{external_urls.length} external links..." # Typhoeus won't let you pass any non-Typhoeus option @proofer_opts.each_key do |opt| @options.delete opt end Ethon.logger = logger # log from Typhoeus/Ethon external_urls.each_pair do |href, filenames| queue_request(:head, href, filenames) end logger.debug colorize :yellow, "Running requests for all #{hydra.queued_requests.size} external URLs..." hydra.run end |
#files ⇒ Object
140 141 142 143 144 145 146 |
# File 'lib/html/proofer.rb', line 140 def files if File.directory? @src Dir.glob("#{@src}/**/*#{@options[:ext]}") else File.extname(@src) == @options[:ext] ? [@src] : [] end end |
#get_checks ⇒ Object
154 155 156 157 158 |
# File 'lib/html/proofer.rb', line 154 def get_checks checks = HTML::Proofer::Checks::Check.subclasses.map { |c| c.name } checks.delete("Favicons") unless @options[:favicon] checks end |
#hydra ⇒ Object
136 137 138 |
# File 'lib/html/proofer.rb', line 136 def hydra @hydra ||= Typhoeus::Hydra.new end |
#log_level ⇒ Object
160 161 162 |
# File 'lib/html/proofer.rb', line 160 def log_level @options[:verbose] ? :debug : :info end |
#queue_request(method, href, filenames) ⇒ Object
99 100 101 102 103 |
# File 'lib/html/proofer.rb', line 99 def queue_request(method, href, filenames) request = Typhoeus::Request.new(href, @options.merge({:method => method})) request.on_complete { |response| response_handler(response, filenames) } hydra.queue request end |
#response_handler(response, filenames) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/html/proofer.rb', line 105 def response_handler(response, filenames) href = response.[:effective_url] method = response.request.[:method] response_code = response.code debug_msg = "Received a #{response_code} for #{href}" debug_msg << " in #{filenames.join(' ')}" unless filenames.nil? logger.debug debug_msg if response_code.between?(200, 299) # continue with no op elsif response.timed_out? failed_test_msg = "External link #{href} failed: got a time out" failed_test_msg.insert(0, "#{filenames.join(' ').blue}: ") unless filenames.nil? @failed_tests << failed_test_msg elsif (response_code == 405 || response_code == 420 || response_code == 503) && method == :head # 420s usually come from rate limiting; let's ignore the query and try just the path with a GET uri = URI(href) queue_request(:get, uri.scheme + "://" + uri.host + uri.path, filenames) # just be lazy; perform an explicit get request. some servers are apparently not configured to # intercept HTTP HEAD elsif method == :head queue_request(:get, href, filenames) else # Received a non-successful http response. failed_test_msg = "External link #{href} failed: #{response_code} #{response.}" failed_test_msg.insert(0, "#{filenames.join(' ').blue}: ") unless filenames.nil? @failed_tests << failed_test_msg end end |
#run ⇒ Object
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 73 |
# File 'lib/html/proofer.rb', line 36 def run unless @options[:as_link_array] total_files = 0 external_urls = {} logger.info colorize :white, "Running #{get_checks} checks on #{@src} on *#{@options[:ext]}... \n\n" files.each do |path| total_files += 1 html = HTML::Proofer.create_nokogiri(path) get_checks.each do |klass| logger.debug colorize :blue, "Checking #{klass.to_s.downcase} on #{path} ..." check = Object.const_get(klass).new(@src, path, html, @options) check.run external_urls.merge!(check.external_urls) @failed_tests.concat(check.issues) if check.issues.length > 0 end end external_link_checker(external_urls) unless @options[:disable_external] logger.info colorize :green, "Ran on #{total_files} files!\n\n" else external_urls = Hash[*@src.map{ |s| [s, nil] }.flatten] external_link_checker(external_urls) unless @options[:disable_external] end if @failed_tests.empty? logger.info colorize :green, "HTML-Proofer finished successfully." else @failed_tests.sort.each do |issue| logger.error colorize :red, issue.to_s end raise colorize :red, "HTML-Proofer found #{@failed_tests.length} failures!" end end |