Class: Repubmark::Elems::Link

Inherits:
Text
  • Object
show all
Defined in:
lib/repubmark/elems/link.rb

Constant Summary collapse

SCHEMES =
%w[http https].freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#parent

Instance Method Summary collapse

Methods inherited from Text

#str=, #str_to_html, #to_gemtext, #to_summary_plain, #word_count

Methods inherited from Base

#absolute_url, #config, #count_words, #html_class, #own_url, parent?, parents, #relative_url, #to_gemtext, #to_summary_plain, #word_count

Constructor Details

#initialize(parent, str, uri, **kwargs) ⇒ Link

Returns a new instance of Link.



10
11
12
13
14
15
# File 'lib/repubmark/elems/link.rb', line 10

def initialize(parent, str, uri, **kwargs)
  super parent, str, **kwargs

  self.uri = uri
  validate_uri!
end

Instance Attribute Details

#uriObject (private)

Returns the value of attribute uri.



27
28
29
# File 'lib/repubmark/elems/link.rb', line 27

def uri
  @uri
end

Instance Method Details

#build_uri(ext) ⇒ Object (private)



47
48
49
50
51
52
53
# File 'lib/repubmark/elems/link.rb', line 47

def build_uri(ext)
  if uri.is_a?(URI::MailTo) || uri.absolute?
    uri
  else
    own_url "#{uri}#{ext}"
  end.to_s.freeze
end

#to_htmlObject

Basic methods #



21
22
23
# File 'lib/repubmark/elems/link.rb', line 21

def to_html
  "<a href=\"#{build_uri('.html')}\">#{str_to_html}</a>".freeze
end

#validate_uri!Object (private)



33
34
35
36
37
38
# File 'lib/repubmark/elems/link.rb', line 33

def validate_uri!
  raise 'Expected normalized URI' unless uri.normalize == uri
  raise 'Expected no userinfo'    unless uri.userinfo.nil?

  validate_uri_http_absolute!
end

#validate_uri_http_absolute!Object (private)



40
41
42
43
44
45
# File 'lib/repubmark/elems/link.rb', line 40

def validate_uri_http_absolute!
  return unless uri.is_a?(URI::HTTP) && uri.absolute?

  raise 'Invalid scheme'    unless SCHEMES.include? uri.scheme
  raise 'Expected hostname' unless uri.hostname
end