Class: HTML::Proofer::Checkable
- Inherits:
-
Object
- Object
- HTML::Proofer::Checkable
- Defined in:
- lib/html/proofer/checkable.rb
Instance Method Summary collapse
- #absolute_path ⇒ Object
-
#exists? ⇒ Boolean
checks if a file exists relative to the current pwd.
-
#external? ⇒ Boolean
path is external to the file.
- #file_path ⇒ Object
- #hash ⇒ Object
- #ignore? ⇒ Boolean
-
#initialize(obj, type, check) ⇒ Checkable
constructor
A new instance of Checkable.
-
#internal? ⇒ Boolean
path is an anchor.
- #parts ⇒ Object
- #path ⇒ Object
-
#remote? ⇒ Boolean
path is to an external server.
- #url ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(obj, type, check) ⇒ Checkable
Returns a new instance of Checkable.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/html/proofer/checkable.rb', line 4 def initialize(obj, type, check) @src = obj['src'] @href = obj['href'] @alt = obj['alt'] @name = obj['name'] @id = obj['id'] @data_ignore_proofer = obj['data-proofer-ignore'] @check = check @checked_paths = {} @type = type if @href && @check.[:href_swap] @check.[:href_swap].each do |link, replace| @href = @href.gsub(link, replace) end end end |
Instance Method Details
#absolute_path ⇒ Object
119 120 121 122 |
# File 'lib/html/proofer/checkable.rb', line 119 def absolute_path path = file_path || @check.path File. path, Dir.pwd end |
#exists? ⇒ Boolean
checks if a file exists relative to the current pwd
114 115 116 117 |
# File 'lib/html/proofer/checkable.rb', line 114 def exists? return @checked_paths[absolute_path] if @checked_paths.has_key? absolute_path @checked_paths[absolute_path] = File.exist? absolute_path end |
#external? ⇒ Boolean
path is external to the file
83 84 85 |
# File 'lib/html/proofer/checkable.rb', line 83 def external? !internal? end |
#file_path ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/html/proofer/checkable.rb', line 92 def file_path return if path.nil? if path =~ /^\// # path relative to root base = File.directory?(@check.src) ? @check.src : File.dirname(@check.src) elsif File.exist?(File. path, @check.src) # relative links, path is a file base = File.dirname @check.path elsif File.exist?(File.join(File.dirname(@check.path), path)) # relative links in nested dir, path is a file base = File.dirname @check.path else # relative link, path is a directory base = @check.path end file = File.join base, path # implicit /index.html support, with support for tailing slashes file = File.join path, "index.html" if File.directory? File. file, @check.src file end |
#hash ⇒ Object
43 44 45 |
# File 'lib/html/proofer/checkable.rb', line 43 def hash parts.fragment end |
#ignore? ⇒ Boolean
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/html/proofer/checkable.rb', line 62 def ignore? return true if @ignored || @data_ignore_proofer || @check.additional_href_ignores.include?(url) unless @check.additional_href_ignores.empty? @check.additional_href_ignores.each do |href_ignore| if href_ignore.is_a? String return true if href_ignore == url elsif href_ignore.is_a? Regexp return true if href_ignore =~ url end end end uri = URI.parse url %w( mailto ).include?(uri.scheme) rescue URI::BadURIError false rescue URI::InvalidURIError false end |
#internal? ⇒ Boolean
path is an anchor
88 89 90 |
# File 'lib/html/proofer/checkable.rb', line 88 def internal? url[0] == "#" end |
#parts ⇒ Object
35 36 37 |
# File 'lib/html/proofer/checkable.rb', line 35 def parts URI.parse url end |
#path ⇒ Object
39 40 41 |
# File 'lib/html/proofer/checkable.rb', line 39 def path parts.path end |
#remote? ⇒ Boolean
path is to an external server
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/html/proofer/checkable.rb', line 48 def remote? uri = URI.parse url %w( http https ).include?(uri.scheme) rescue URI::BadURIError false rescue URI::InvalidURIError if @type == "image" @ignored = true return true if url.match(/^data:image/) end false end |
#url ⇒ Object
23 24 25 |
# File 'lib/html/proofer/checkable.rb', line 23 def url @src || @href || "" end |
#valid? ⇒ Boolean
27 28 29 30 31 32 33 |
# File 'lib/html/proofer/checkable.rb', line 27 def valid? begin URI.parse url rescue false end end |