Created
May 13, 2013 05:30
-
-
Save toshimaru/5566341 to your computer and use it in GitHub Desktop.
jekyll check_links task. (via. https://github.com/krisb/jekyll-template/blob/master/Rakefile)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
desc 'Check links for site already running on localhost:4000' | |
task :check_links do | |
begin | |
require 'anemone' | |
root = 'http://localhost:4000/' | |
Anemone.crawl(root, :discard_page_bodies => true) do |anemone| | |
anemone.after_crawl do |pagestore| | |
broken_links = Hash.new { |h, k| h[k] = [] } | |
pagestore.each_value do |page| | |
if page.code != 200 | |
referrers = pagestore.pages_linking_to(page.url) | |
referrers.each do |referrer| | |
broken_links[referrer] << page | |
end | |
end | |
end | |
broken_links.each do |referrer, pages| | |
puts "#{referrer.url} contains the following broken links:" | |
pages.each do |page| | |
puts " HTTP #{page.code} #{page.url}" | |
end | |
end | |
end | |
end | |
rescue LoadError | |
abort 'Install anemone gem: gem install anemone' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment