forked from flapjack/flapjack.io
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfind_broken_doclinks.rb
47 lines (41 loc) · 1.29 KB
/
find_broken_doclinks.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
#!/usr/bin/env ruby
# A simple spider to check all links in the built HTML, against this local copy
# of the Flapjack documentation.
# Before running this script, start the local middleman server
# `bundle exec middleman build; bundle exec middleman server`
require 'open-uri'
require 'nokogiri'
require 'set'
def parse uri
begin
status = open(uri).status.first
puts uri, status if !(status == '200' || status == '301')
rescue OpenURI::HTTPError => e
puts "Tried to open #{uri}: #{e.message}"
end
end
def find_links uri, links
doc = Nokogiri::HTML(uri)
doc.css('a').each do |link|
if link.attributes['href']
uri = link.attributes['href'].value
if uri.start_with?('http')
links.add(uri) unless uri.include?('localhost')
# FIXME: The following two conditions result in some false-positives
# of the form http://0.0.0.0:4567/docs/0.9/support when they're actually
# at http://0.0.0.0:4567/support
elsif uri.start_with?('/')
links.add("http://0.0.0.0:4567/docs/0.9#{uri}")
elsif !uri.start_with?('#')
links.add("http://0.0.0.0:4567/docs/0.9/#{uri}")
end
end
end
end
links = Set.new
Dir.glob('build/**/*.html') do |file|
File.open(file) do |f|
find_links(f, links)
end
end
links.each { |u| parse (u) }