class Bookbinder::Postprocessing::LinkChecker

Public Class Methods

new(fs, root_path, output_streams) click to toggle source
# File lib/bookbinder/postprocessing/link_checker.rb, line 8
def initialize(fs, root_path, output_streams)
  @fs = fs
  @root_path = root_path
  @output_streams = output_streams
  @broken_link_count = 0

  @convert_to_relative = %r{\A.*#{root_path.to_s}/public}
  @default_link_exclusions = %r{\A(?:https?://|javascript:|mailto:)}
  @excluded_pages = %r{\A/(?:404\.html|subnavs|javascripts|stylesheets|style_guide)}
end

Public Instance Methods

check!(link_exclusions = /(?!.*)/) click to toggle source
# File lib/bookbinder/postprocessing/link_checker.rb, line 19
def check!(link_exclusions = /(?!.*)/)
  @output_streams[:out].puts "\nChecking for broken links..."
  @redirects = Redirection.new(@fs, File.join(@root_path, 'redirects.rb'))
  load_page_links

  report_broken_links!(link_exclusions)
  report_orphaned_pages!

  if has_errors?
    err "\nFound #{@broken_link_count} broken links!"
  else
    out "\nNo broken links!"
  end
end
has_errors?() click to toggle source
# File lib/bookbinder/postprocessing/link_checker.rb, line 34
def has_errors?
  @broken_link_count > 0
end

Private Instance Methods

err(str) click to toggle source
# File lib/bookbinder/postprocessing/link_checker.rb, line 120
def err(str)
  @output_streams[:err].puts(str)
end
file_exists?(link) click to toggle source
# File lib/bookbinder/postprocessing/link_checker.rb, line 94
def file_exists?(link)
  full_path = File.join(@root_path, 'public', link)
  @fs.is_file?(full_path) || (@fs.is_dir?(full_path) && @fs.is_file?(File.join(full_path, 'index.html')))
end
out(str) click to toggle source
# File lib/bookbinder/postprocessing/link_checker.rb, line 116
def out(str)
  @output_streams[:out].puts(str)
end
page_exists?(link) click to toggle source
# File lib/bookbinder/postprocessing/link_checker.rb, line 83
def page_exists?(link)
  @page_links.has_key?(link) || @redirects.redirected?(link)
end
report_orphaned_pages!() click to toggle source
# File lib/bookbinder/postprocessing/link_checker.rb, line 63
def report_orphaned_pages!
  linked_pages = @page_links.map do |page, links|
    links.map do |link|
      normalize_link(link, page)[0]
    end
  end.flatten.uniq

  orphaned_pages = @page_links.keys.reject { |page| page == '/index.html' || linked_pages.include?(page) }
  if orphaned_pages.size > 0
    err "\nOrphaned pages"
    orphaned_pages.each do |page|
      err "No links to => #{page}"
    end
  end
end
skip?(link_path, link_exclusions) click to toggle source
# File lib/bookbinder/postprocessing/link_checker.rb, line 79
def skip?(link_path, link_exclusions)
  @default_link_exclusions.match(link_path) || link_path.match(link_exclusions)
end