Class: HTML::Proofer::Checkable

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

Direct Known Subclasses

Image, Link

Instance Method Summary collapse

Constructor Details

#initialize(obj, check) ⇒ Checkable

Returns a new instance of Checkable.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/html/proofer/checkable.rb', line 5

def initialize(obj, 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

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

end

Instance Method Details

#absolute_pathObject



100
101
102
103
# File 'lib/html/proofer/checkable.rb', line 100

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)


96
97
98
# File 'lib/html/proofer/checkable.rb', line 96

def exists?
  File.exist? absolute_path
end

#external?Boolean

path is external to the file

Returns:

  • (Boolean)


66
67
68
# File 'lib/html/proofer/checkable.rb', line 66

def external?
  !internal?
end

#file_pathObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/html/proofer/checkable.rb', line 75

def file_path

  return if path.nil?

  if path =~ /^\// #path relative to root
    base = @check.src
  elsif File.exist? File.expand_path path, @check.src #relative links, 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.expand_path file, @check.src

  file
end

#hashObject



42
43
44
# File 'lib/html/proofer/checkable.rb', line 42

def hash
  parts.fragment
end

#ignore?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
# File 'lib/html/proofer/checkable.rb', line 56

def ignore?
  uri = URI.parse url
  @data_ignore_proofer || %w( mailto ).include?(uri.scheme) || @check.additional_href_ignores.include?(href)
rescue URI::BadURIError
  false
rescue URI::InvalidURIError
  false
end

#internal?Boolean

path is an anchor

Returns:

  • (Boolean)


71
72
73
# File 'lib/html/proofer/checkable.rb', line 71

def internal?
  url[0] == "#"
end

#partsObject



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

def parts
   URI.parse url
end

#pathObject



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

def path
  parts.path
end

#remote?Boolean

path is to an external server

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
# File 'lib/html/proofer/checkable.rb', line 47

def remote?
  uri = URI.parse url
  %w( http https ).include?(uri.scheme)
rescue URI::BadURIError
  false
rescue URI::InvalidURIError
  false
end

#urlObject



22
23
24
# File 'lib/html/proofer/checkable.rb', line 22

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

#valid?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# File 'lib/html/proofer/checkable.rb', line 26

def valid?
  begin
    URI.parse url
  rescue
    false
  end
end