Class: Nanoc3::Extra::Validators::Links

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc3/extra/validators/links.rb

Overview

A validator that verifies that all links (‘<a href=“…”>…</a>`) point to a location that exists.

Defined Under Namespace

Classes: ThreadsafeHashEnumerator

Instance Method Summary collapse

Constructor Details

#initialize(dir, index_filenames, params = {}) ⇒ Links

Returns a new instance of Links.

Parameters:

  • dir (String)

    The directory that will be searched for HTML files to validate

  • index_filenames (Array<String>)

    An array of index filenames that will be appended to URLs by web servers if a directory is requested instead of a file

  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • :internal (Boolean) — default: false

    True if internal links should be checked; false if they should not

  • :external (Boolean) — default: false

    True if external links should be checked; false if they should not



21
22
23
24
25
26
# File 'lib/nanoc3/extra/validators/links.rb', line 21

def initialize(dir, index_filenames, params={})
  @dir              = dir
  @index_filenames  = index_filenames
  @include_internal = params.has_key?(:internal) && params[:internal]
  @include_external = params.has_key?(:external) && params[:external]
end

Instance Method Details

#runvoid

This method returns an undefined value.

Starts the validator. The results will be printed to stdout.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/nanoc3/extra/validators/links.rb', line 31

def run
  require 'nokogiri'

  @delegate = self
  links = all_broken_hrefs
  if links.empty?
    puts "No broken links found!"
  else
    links.each_pair do |href, origins|
      puts "Broken link: #{href} -- referenced from:"
      origins.each do |origin|
        puts "    #{origin}"
      end
      puts
    end
  end
end