Class: HTML::Proofer::Checkable
- Inherits:
-
Object
- Object
- HTML::Proofer::Checkable
show all
- Includes:
- Utils
- Defined in:
- lib/html/proofer/checkable.rb
Overview
Represents the superclass from which all checks derive.
Constant Summary
Constants included
from Utils
Utils::STORAGE_DIR
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Utils
clean_content, create_nokogiri, #pluralize, swap
Constructor Details
#initialize(obj, check) ⇒ Checkable
Returns a new instance of Checkable.
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/html/proofer/checkable.rb', line 12
def initialize(obj, check)
obj.attributes.each_pair do |attribute, value|
instance_variable_set("@#{attribute.tr('-:.', '_')}".to_sym, value.value)
end
@text = obj.content
@check = check
@checked_paths = {}
@type = self.class.name
@line = obj.line
if @href && @check.options[:href_swap]
@href = swap(@href, @check.options[:href_swap])
end
@href.insert 0, 'http:' if @href =~ %r{^//}
@src.insert 0, 'http:' if @src =~ %r{^//}
end
|
Instance Attribute Details
#line ⇒ Object
Returns the value of attribute line.
10
11
12
|
# File 'lib/html/proofer/checkable.rb', line 10
def line
@line
end
|
Instance Method Details
#absolute_path ⇒ Object
136
137
138
139
|
# File 'lib/html/proofer/checkable.rb', line 136
def absolute_path
path = file_path || @check.path
File.expand_path path, Dir.pwd
end
|
#allow_hash_href? ⇒ Boolean
93
94
95
|
# File 'lib/html/proofer/checkable.rb', line 93
def allow_hash_href?
@check.allow_hash_href
end
|
#exists? ⇒ Boolean
checks if a file exists relative to the current pwd
131
132
133
134
|
# File 'lib/html/proofer/checkable.rb', line 131
def exists?
return @checked_paths[absolute_path] if @checked_paths.key? absolute_path
@checked_paths[absolute_path] = File.exist? absolute_path
end
|
#external? ⇒ Boolean
path is external to the file
98
99
100
|
# File 'lib/html/proofer/checkable.rb', line 98
def external?
!internal?
end
|
#file_path ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/html/proofer/checkable.rb', line 107
def file_path
return if path.nil?
if path =~ %r{^/} base = File.directory?(@check.src) ? @check.src : File.dirname(@check.src)
elsif File.exist?(File.expand_path path, @check.src) base = File.dirname @check.path
elsif File.exist?(File.join(File.dirname(@check.path), path)) base = File.dirname @check.path
else base = @check.path
end
file = File.join base, path
if File.directory?(file) && !unslashed_directory?(file)
file = File.join file, @check.options[:directory_index_file]
end
file
end
|
#follow_location? ⇒ Boolean
157
158
159
|
# File 'lib/html/proofer/checkable.rb', line 157
def follow_location?
@check.typhoeus_opts && @check.typhoeus_opts[:followlocation]
end
|
#hash ⇒ Object
50
51
52
|
# File 'lib/html/proofer/checkable.rb', line 50
def hash
parts.fragment unless parts.nil?
end
|
#ignore? ⇒ Boolean
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/html/proofer/checkable.rb', line 67
def ignore?
return true if @data_proofer_ignore
if %w(ImageCheckable FaviconCheckable).include? @type
return true if url.match(/^data:image/)
end
return true if ignores_pattern_check(@check.url_ignores)
if 'LinkCheckable' == @type
return true if ignores_pattern_check(@check.href_ignores)
end
if 'ImageCheckable' == @type
return true if ignores_pattern_check(@check.alt_ignores)
end
end
|
#ignore_empty_alt? ⇒ Boolean
89
90
91
|
# File 'lib/html/proofer/checkable.rb', line 89
def ignore_empty_alt?
@check.empty_alt_ignore
end
|
#ignores_pattern_check(links) ⇒ Object
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/html/proofer/checkable.rb', line 141
def ignores_pattern_check(links)
links.each do |ignore|
if ignore.is_a? String
return true if ignore == url
elsif ignore.is_a? Regexp
return true if ignore =~ url
end
end
false
end
|
#internal? ⇒ Boolean
path is an anchor or a query
103
104
105
|
# File 'lib/html/proofer/checkable.rb', line 103
def internal?
url.start_with? '#', '?'
end
|
#non_http_remote? ⇒ Boolean
63
64
65
|
# File 'lib/html/proofer/checkable.rb', line 63
def non_http_remote?
!scheme.nil? && !remote?
end
|
#parts ⇒ Object
40
41
42
43
44
|
# File 'lib/html/proofer/checkable.rb', line 40
def parts
@parts ||= Addressable::URI.parse url
rescue URI::Error, Addressable::URI::InvalidURIError
@parts = nil
end
|
#path ⇒ Object
46
47
48
|
# File 'lib/html/proofer/checkable.rb', line 46
def path
Addressable::URI.unencode parts.path unless parts.nil?
end
|
#remote? ⇒ Boolean
path is to an external server
59
60
61
|
# File 'lib/html/proofer/checkable.rb', line 59
def remote?
%w( http https ).include? scheme
end
|
#scheme ⇒ Object
54
55
56
|
# File 'lib/html/proofer/checkable.rb', line 54
def scheme
parts.scheme unless parts.nil?
end
|
#unslashed_directory?(file) ⇒ Boolean
153
154
155
|
# File 'lib/html/proofer/checkable.rb', line 153
def unslashed_directory?(file)
File.directory?(file) && !file.end_with?(File::SEPARATOR) && !follow_location?
end
|
#url ⇒ Object
32
33
34
|
# File 'lib/html/proofer/checkable.rb', line 32
def url
@src || @srcset || @href || ''
end
|
#valid? ⇒ Boolean
36
37
38
|
# File 'lib/html/proofer/checkable.rb', line 36
def valid?
!parts.nil?
end
|