Class: HTMLProofer::UrlValidator::Internal

Inherits:
HTMLProofer::UrlValidator show all
Defined in:
lib/html_proofer/url_validator/internal.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HTMLProofer::Utils

#blank?, #create_nokogiri, #pluralize

Constructor Details

#initialize(runner, internal_urls) ⇒ Internal

Returns a new instance of Internal.



8
9
10
11
12
# File 'lib/html_proofer/url_validator/internal.rb', line 8

def initialize(runner, internal_urls)
  super(runner)

  @internal_urls = internal_urls
end

Instance Attribute Details

#internal_urlsObject (readonly)

Returns the value of attribute internal_urls.



6
7
8
# File 'lib/html_proofer/url_validator/internal.rb', line 6

def internal_urls
  @internal_urls
end

Instance Method Details



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
70
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
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/html_proofer/url_validator/internal.rb', line 24

def run_internal_link_checker(links)
  # collect urls and metadata for hashes to be checked in the same target file
  file_paths_hashes_to_check = {}
  to_add = []
  links.each_with_index do |(link, matched_files), i|
    matched_count_to_log = pluralize(matched_files.count, "reference", "references")
    @logger.log(:debug, "(#{i + 1} / #{links.count}) Internal link #{link}: Checking #{matched_count_to_log}")
    matched_files.each do ||
      url = HTMLProofer::Attribute::Url.new(@runner, link, base_url: [:base_url], source: [:source], filename: [:filename])

      unless url.exists?
        @failed_checks << Failure.new(
          [:filename],
          "Links > Internal",
          "internally linking to #{url}, which does not exist",
          line: [:line],
          status: nil,
          content: nil,
        )
        to_add << [url, , false]
        next
      end

      hash_exists = hash_exists_for_url?(url)
      if hash_exists.nil?
        # the hash needs to be checked in the target file, we collect the url and metadata
        target_file_path = url.resolved_path
        unless file_paths_hashes_to_check.key?(target_file_path)
          file_paths_hashes_to_check[target_file_path] = {}
        end
        unless file_paths_hashes_to_check[target_file_path].key?(url.hash)
          file_paths_hashes_to_check[target_file_path][url.hash] = []
        end
        file_paths_hashes_to_check[target_file_path][url.hash] << [url, ]
        next
      end
      unless hash_exists
        @failed_checks << Failure.new(
          [:filename],
          "Links > Internal",
          "internally linking to #{url}; the file exists, but the hash '#{url.hash}' does not",
          line: [:line],
          status: nil,
          content: nil,
        )
        to_add << [url, , false]
        next
      end

      to_add << [url, , true]
    end
  end

  # check hashes by target file
  @logger.log(:info, "Checking internal link hashes in #{pluralize(file_paths_hashes_to_check.count, "file", "files")}")
  file_paths_hashes_to_check.each_with_index do |(file_path, hashes_to_check), i|
    hash_count_to_log = pluralize(hashes_to_check.count, "hash", "hashes")
    @logger.log(:debug, "(#{i + 1} / #{file_paths_hashes_to_check.count}) Checking #{hash_count_to_log} in #{file_path}")
    html = create_nokogiri(file_path)
    hashes_to_check.each_pair do |href_hash, |
      exists = hash_exists_in_html?(href_hash, html)
      .each do |(url, )|
        unless exists
          @failed_checks << Failure.new(
            [:filename],
            "Links > Internal",
            "internally linking to #{url}; the file exists, but the hash '#{href_hash}' does not",
            line: [:line],
            status: nil,
            content: nil,
          )
        end
        to_add << [url, , exists]
      end
    end
  end

  # adding directly to the cache above results in an endless loop
  to_add.each do |(url, , exists)|
    @cache.add_internal(url.to_s, , exists)
  end

  @failed_checks
end

#validateObject



14
15
16
17
18
19
20
21
22
# File 'lib/html_proofer/url_validator/internal.rb', line 14

def validate
  urls_to_check = @cache.internal_enabled? ? @runner.load_internal_cache : @internal_urls
  urls_detected = pluralize(urls_to_check.count, "internal link", "internal links")
  @logger.log(:info, "Checking #{urls_detected}")

  run_internal_link_checker(urls_to_check)

  @failed_checks
end