class HtmlElement::Utils::LinkManager
Constants
- DEFAULT_SCHEME
- SCHEME_RE
- SEP
Public Class Methods
new(domain_name, from_host_names=[], scheme=DEFAULT_SCHEME)
click to toggle source
# 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
Public Instance Methods
convert_to_relative_path(url)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
use_relative_path_for_in_domain_links(html)
click to toggle source
# 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
Private Instance Methods
compile_from_names_re(from_host_names)
click to toggle source
# File lib/htmlelement/utils.rb, line 68 def compile_from_names_re(from_host_names) escaped_names = from_host_names.map {|name| Regexp.escape(name) } Regexp.compile(escaped_names.join("|")) end
default_domain?(url)
click to toggle source
# File lib/htmlelement/utils.rb, line 73 def default_domain?(url) url += SEP unless url.end_with?(SEP) (URI.parse(url) - @domain_name).to_s.empty? end