broken_link_finder 0.9.4 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,116 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module BrokenLinkFinder
4
- class Reporter
5
- # The amount of pages/links to display when verbose is false.
6
- NUM_VALUES = 3
7
-
8
- # Creates a new Reporter instance.
9
- # stream is any Object that responds to :puts.
10
- def initialize(stream, sort, broken_links, ignored_links)
11
- raise 'stream must respond_to? :puts' unless stream.respond_to?(:puts)
12
- raise "sort by either :page or :link, not #{sort}" \
13
- unless %i[page link].include?(sort)
14
-
15
- @stream = stream
16
- @sort = sort
17
- @broken_links = broken_links
18
- @ignored_links = ignored_links
19
- end
20
-
21
- # Pretty print a report detailing the link summary.
22
- def pretty_print_link_report(broken_verbose: true, ignored_verbose: false)
23
- report_broken_links(verbose: broken_verbose)
24
- report_ignored_links(verbose: ignored_verbose)
25
-
26
- nil
27
- end
28
-
29
- private
30
-
31
- # Report a summary of the broken links.
32
- def report_broken_links(verbose: true)
33
- if @broken_links.empty?
34
- print 'Good news, there are no broken links!'
35
- else
36
- num_pages, num_links = get_hash_stats(@broken_links)
37
- print "Found #{num_links} broken link(s) across #{num_pages} page(s):"
38
-
39
- @broken_links.each do |key, values|
40
- msg = sort_by_page? ?
41
- "The following broken links were found on '#{key}':" :
42
- "The broken link '#{key}' was found on the following pages:"
43
- nprint msg
44
-
45
- if verbose || (values.length <= NUM_VALUES)
46
- values.each { |value| print value }
47
- else # Only print N values and summarise the rest.
48
- NUM_VALUES.times { |i| print values[i] }
49
-
50
- objects = sort_by_page? ? 'link(s)' : 'page(s)'
51
- print "+ #{values.length - NUM_VALUES} other #{objects}, remove --concise to see them all"
52
- end
53
- end
54
- end
55
- end
56
-
57
- # Report a summary of the ignored links.
58
- def report_ignored_links(verbose: false)
59
- if @ignored_links.any?
60
- num_pages, num_links = get_hash_stats(@ignored_links)
61
- nprint "Ignored #{num_links} unsupported link(s) across #{num_pages} page(s), which you should check manually:"
62
-
63
- @ignored_links.each do |key, values|
64
- msg = sort_by_page? ?
65
- "The following links were ignored on '#{key}':" :
66
- "The link '#{key}' was ignored on the following pages:"
67
- nprint msg
68
-
69
- if verbose || (values.length <= NUM_VALUES)
70
- values.each { |value| print value }
71
- else # Only print N values and summarise the rest.
72
- NUM_VALUES.times { |i| print values[i] }
73
-
74
- objects = sort_by_page? ? 'link(s)' : 'page(s)'
75
- print "+ #{values.length - NUM_VALUES} other #{objects}, use --verbose to see them all"
76
- end
77
- end
78
- end
79
- end
80
-
81
- # Return true if the sort is by page.
82
- def sort_by_page?
83
- @sort == :page
84
- end
85
-
86
- # Returns the key/value statistics of hash e.g. the number of keys and
87
- # combined values. The hash should be of the format: { 'str' => [...] }.
88
- # Use like: `num_pages, num_links = get_hash_stats(links)`.
89
- def get_hash_stats(hash)
90
- num_keys = hash.keys.length
91
- values = hash.values.flatten
92
- num_values = sort_by_page? ? values.length : values.uniq.length
93
-
94
- sort_by_page? ?
95
- [num_keys, num_values] :
96
- [num_values, num_keys]
97
- end
98
-
99
- # Prints the text + \n. Defaults to a blank line.
100
- def print(text = '')
101
- @stream.puts(text)
102
- end
103
-
104
- # Prints text + \n\n.
105
- def printn(text)
106
- print(text)
107
- print
108
- end
109
-
110
- # Prints \n + text + \n.
111
- def nprint(text)
112
- print
113
- print(text)
114
- end
115
- end
116
- end