Class: HTML::Proofer::Checkable

Inherits:
Object
  • Object
show all
Defined in:
lib/html/proofer/checkable.rb

Direct Known Subclasses

Favicon, Image, Link, Script

Instance Method Summary collapse

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
22
23
24
25
26
27
28
# 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']
  @rel = obj['rel']

  @data_ignore_proofer = obj['data-proofer-ignore']
  @content = obj.content
  @check = check
  @checked_paths = {}
  @type = type

  if @href && @check.options[:href_swap]
    @check.options[:href_swap].each do |link, replace|
      @href = @href.gsub(link, replace)
    end
  end

  # fix up missing protocols
  @href.insert 0, "http:" if @href =~ /^\/\//
  @src.insert 0, "http:" if @src =~ /^\/\//

end

Instance Method Details

#absolute_pathObject


118
119
120
121
# File 'lib/html/proofer/checkable.rb', line 118

def absolute_path
  path = file_path || @check.path
  File.expand_path path, Dir.pwd
end

#exists?Boolean

checks if a file exists relative to the current pwd

Returns:

  • (Boolean)

113
114
115
116
# File 'lib/html/proofer/checkable.rb', line 113

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

Returns:

  • (Boolean)

80
81
82
# File 'lib/html/proofer/checkable.rb', line 80

def external?
  !internal?
end

#file_pathObject


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/html/proofer/checkable.rb', line 89

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.expand_path 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 support
  if File.directory? file and !unslashed_directory? file
    file = File.join file, @check.options[:directory_index_file]
  end

  file
end

#hashObject


48
49
50
# File 'lib/html/proofer/checkable.rb', line 48

def hash
  parts.fragment if !parts.nil?
end

#ignore?Boolean

Returns:

  • (Boolean)

65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/html/proofer/checkable.rb', line 65

def ignore?
  return true if @data_ignore_proofer

  case @type
  when "favicon"
    return true if url.match(/^data:image/)
  when "link"
    return true if ignores_pattern_check(@check.additional_href_ignores)
  when "image"
    return true if url.match(/^data:image/)
    return true if ignores_pattern_check(@check.additional_alt_ignores)
  end
end

#ignores_pattern_check(links) ⇒ Object


123
124
125
126
127
128
129
130
131
132
133
# File 'lib/html/proofer/checkable.rb', line 123

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

Returns:

  • (Boolean)

85
86
87
# File 'lib/html/proofer/checkable.rb', line 85

def internal?
  url.start_with? "#", "?"
end

#non_http_remote?Boolean

Returns:

  • (Boolean)

61
62
63
# File 'lib/html/proofer/checkable.rb', line 61

def non_http_remote?
  !scheme.nil? && !remote?
end

#partsObject


38
39
40
41
42
# File 'lib/html/proofer/checkable.rb', line 38

def parts
  @parts ||= Addressable::URI.parse url
rescue URI::Error
  @parts = nil
end

#pathObject


44
45
46
# File 'lib/html/proofer/checkable.rb', line 44

def path
  parts.path if !parts.nil?
end

#remote?Boolean

path is to an external server

Returns:

  • (Boolean)

57
58
59
# File 'lib/html/proofer/checkable.rb', line 57

def remote?
  %w( http https ).include? scheme
end

#schemeObject


52
53
54
# File 'lib/html/proofer/checkable.rb', line 52

def scheme
  parts.scheme if !parts.nil?
end

#unslashed_directory?(file) ⇒ Boolean

Returns:

  • (Boolean)

135
136
137
# File 'lib/html/proofer/checkable.rb', line 135

def unslashed_directory? file
  File.directory? file and !file.end_with? File::SEPARATOR and !@check.options[:followlocation]
end

#urlObject


30
31
32
# File 'lib/html/proofer/checkable.rb', line 30

def url
  @src || @href || ""
end

#valid?Boolean

Returns:

  • (Boolean)

34
35
36
# File 'lib/html/proofer/checkable.rb', line 34

def valid?
  !parts.nil?
end