Class: HtmlElement::Utils::LinkManager

Inherits:
Object
  • Object
show all
Defined in:
lib/htmlelement/utils.rb

Constant Summary collapse

SEP =
"/".freeze
SCHEME_RE =
/^(https?|ftp):\/\//
DEFAULT_SCHEME =
'http://'

Instance Method Summary collapse

Constructor Details

#initialize(domain_name, from_host_names = [], scheme = DEFAULT_SCHEME) ⇒ LinkManager

Returns a new instance of LinkManager.



28
29
30
31
32
33
34
35
36
# File 'lib/htmlelement/utils.rb', line 28

def initialize(domain_name, from_host_names=[], scheme=DEFAULT_SCHEME)
  domain_name += SEP unless domain_name.end_with?(SEP)
  domain_name = scheme + domain_name unless SCHEME_RE =~ domain_name
  @domain_name = URI.parse(domain_name)
  @domain_name_re = Regexp.compile(Regexp.escape(domain_name))
  unless from_host_names.empty?
    @from_host_names_re = compile_from_names_re(from_host_names)
  end
end

Instance Method Details

#convert_to_relative_path(url) ⇒ Object



43
44
45
46
47
# File 'lib/htmlelement/utils.rb', line 43

def convert_to_relative_path(url)
  return url unless SCHEME_RE =~ url
  return "./".freeze if default_domain?(url)
  (URI.parse(url) - @domain_name).to_s
end

#external_link?(url) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/htmlelement/utils.rb', line 60

def external_link?(url)
  if SCHEME_RE.match(url)
    URI.parse(url).host != @domain_name.host
  end
end

#unify_host_names(url) ⇒ Object



38
39
40
41
# File 'lib/htmlelement/utils.rb', line 38

def unify_host_names(url)
  return url unless @from_host_names_re
  url.sub(@from_host_names_re, @domain_name.host)
end


49
50
51
52
53
54
55
56
57
58
# File 'lib/htmlelement/utils.rb', line 49

def use_relative_path_for_in_domain_links(html)
  links = Utils.collect_elements_by_name(html, "a".freeze)
  links.each do |a|
    href = a["href"]
    href = unify_host_names(href)
    href = convert_to_relative_path(href)
    a["href"] = href
  end
  html
end