Class: HTML::Proofer

Inherits:
Object
  • Object
show all
Defined in:
lib/html/proofer.rb,
lib/html/proofer/checks.rb,
lib/html/proofer/version.rb,
lib/html/proofer/checkable.rb

Defined Under Namespace

Classes: Checkable, Checks

Constant Summary collapse

VERSION =
"0.4.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Proofer.



8
9
10
11
12
13
14
15
# File 'lib/html/proofer.rb', line 8

def initialize(src, opts={})
  @srcDir = src

  @proofer_opts = {:ext => ".html", :href_swap => [], :href_ignore => [], :disable_external => false }
  @options = @proofer_opts.merge({:followlocation => true}).merge(opts)

  @failed_tests = []
end

Class Method Details

.create_nokogiri(path) ⇒ Object



109
110
111
112
113
# File 'lib/html/proofer.rb', line 109

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

#filesObject



101
102
103
104
105
106
107
# File 'lib/html/proofer.rb', line 101

def files
  if File.directory? @srcDir
    Dir.glob("#{@srcDir}/**/*#{@options[:ext]}")
  else
    File.extname(@srcDir) == @options[:ext] ? [@srcDir] : []
  end
end

#get_checksObject



115
116
117
# File 'lib/html/proofer.rb', line 115

def get_checks
  HTML::Proofer::Checks::Check.subclasses
end

#hydraObject



97
98
99
# File 'lib/html/proofer.rb', line 97

def hydra
  @hydra ||= Typhoeus::Hydra.new
end

#response_handler(response, filenames) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/html/proofer.rb', line 71

def response_handler(response, filenames)
  href = response.options[:effective_url]
  method = response.request.options[:method]
  response_code = response.code

  if response_code.between?(200, 299)
    # continue with no op
  elsif response.timed_out?
     @failed_tests << "#{filenames.join(' ').blue}: External link #{href} failed: got a time out"
  # hey here's a funny bug: sometimes HEAD requests return a 404, even on legitimate pages! The
  # bug seems to affect curl; try `curl -I -X HEAD https://help.github.com/changing-author-info`
  # so in these cases, try a regular `GET`. if it fails, it fails.
  elsif response_code == 404 && method == :head
    next_response = Typhoeus.get(href, @options)
    response_handler(next_response, filenames)
  elsif (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)
    next_response = Typhoeus.get(uri.scheme + "://" + uri.host + uri.path, @options)
    response_handler(next_response, filenames)
  else
    # Received a non-successful http response.
    @failed_tests << "#{filenames.join(' ').blue}: External link #{href} failed: #{response_code} #{response.return_message}"
  end
end

#runObject



17
18
19
20
21
22
23
24
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/html/proofer.rb', line 17

def run
  total_files = 0
  external_urls = {}

  puts "Running #{get_checks} checks on #{@srcDir} on *#{@options[:ext]}... \n\n"

  files.each do |path|
    total_files += 1
    html = HTML::Proofer.create_nokogiri(path)

    get_checks.each do |klass|
      check = klass.new(@srcDir, path, html, @options)
      check.run
      external_urls.merge!(check.external_urls)
      @failed_tests.concat(check.issues) if check.issues.length > 0
    end
  end

  # 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
  external_urls = Hash[external_urls.sort]

  unless @options[:disable_external]
    puts "Checking #{external_urls.length} external links... \n\n"

    # Typhoeus won't let you pass any non-Typhoeus option
    @proofer_opts.each_key do |opt|
      @options.delete opt
    end

    external_urls.each_pair do |href, filenames|
      request = Typhoeus::Request.new(href, @options.merge({:method => :head}))
      request.on_complete { |response| response_handler(response, filenames) }
      hydra.queue request
    end
    hydra.run
  end

  puts "Ran on #{total_files} files!"

  if @failed_tests.empty?
    puts "HTML-Proofer finished successfully.".green
  else
    @failed_tests.each do |issue|
      $stderr.puts issue + "\n\n"
    end

    raise "HTML-Proofer found #{@failed_tests.length} failures!"
  end
end