Class: Coradoc::Element::Inline::Link

Inherits:
Base
  • Object
show all
Defined in:
lib/coradoc/element/inline/link.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

access_children, #children_accessors, children_accessors, declare_children, #simplify_block_content, visit, #visit

Constructor Details

#initialize(options = {}) ⇒ Link

Returns a new instance of Link.



11
12
13
14
15
16
# File 'lib/coradoc/element/inline/link.rb', line 11

def initialize(options = {})
  @path = options.fetch(:path, nil)
  @title = options.fetch(:title, nil)
  @name = options.fetch(:name, nil)
  @right_constrain = options.fetch(:right_constrain, false)
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/coradoc/element/inline/link.rb', line 7

def name
  @name
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/coradoc/element/inline/link.rb', line 7

def path
  @path
end

#right_constrainObject

Returns the value of attribute right_constrain.



7
8
9
# File 'lib/coradoc/element/inline/link.rb', line 7

def right_constrain
  @right_constrain
end

#titleObject

Returns the value of attribute title.



7
8
9
# File 'lib/coradoc/element/inline/link.rb', line 7

def title
  @title
end

Instance Method Details

#to_adocObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/coradoc/element/inline/link.rb', line 18

def to_adoc
  link = @path
  unless @path.to_s&.match?(URI::DEFAULT_PARSER.make_regexp)
    link = "link:#{link}"
  end

  name_empty = @name.to_s.empty?
  title_empty = @title.to_s.empty?
  valid_empty_name_link = link.start_with?(%r{https?://})

  link << if name_empty && !title_empty
            "[#{@title}]"
          elsif !name_empty
            "[#{@name}]"
          elsif valid_empty_name_link && !right_constrain
            ""
          else
            "[]"
          end
  link
end