Class: Coradoc::Element::Title

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(content, level, options = {}) ⇒ Title

Returns a new instance of Title.



8
9
10
11
12
13
14
15
16
# File 'lib/coradoc/element/title.rb', line 8

def initialize(content, level, options = {})
  @level_int = level
  # @level_int = level.length - 1 if level.is_a?(String)
  @content = content
  @id = options.fetch(:id, nil)
  @anchor = @id.nil? ? nil : Inline::Anchor.new(@id)
  @line_break = options.fetch(:line_break, "")
  @style = options.fetch(:style, nil)
end

Instance Attribute Details

#contentObject Also known as: text

Returns the value of attribute content.



4
5
6
# File 'lib/coradoc/element/title.rb', line 4

def content
  @content
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/coradoc/element/title.rb', line 4

def id
  @id
end

#level_intObject

Returns the value of attribute level_int.



4
5
6
# File 'lib/coradoc/element/title.rb', line 4

def level_int
  @level_int
end

#line_breakObject

Returns the value of attribute line_break.



4
5
6
# File 'lib/coradoc/element/title.rb', line 4

def line_break
  @line_break
end

#styleObject

Returns the value of attribute style.



4
5
6
# File 'lib/coradoc/element/title.rb', line 4

def style
  @style
end

Instance Method Details

#levelObject



18
19
20
# File 'lib/coradoc/element/title.rb', line 18

def level
  @level ||= level_from_string
end

#level_strObject



31
32
33
34
35
36
37
# File 'lib/coradoc/element/title.rb', line 31

def level_str
  if @level_int <= 5
    "=" * (@level_int + 1)
  else
    "======"
  end
end

#style_strObject



39
40
41
42
43
44
45
# File 'lib/coradoc/element/title.rb', line 39

def style_str
  style = [@style]
  style << "level=#{@level_int}" if @level_int > 5
  style = style.compact.join(",")

  "[#{style}]\n" unless style.empty?
end

#to_adocObject



22
23
24
25
26
27
28
29
# File 'lib/coradoc/element/title.rb', line 22

def to_adoc
  anchor = @anchor.nil? ? "" : "#{@anchor.to_adoc}\n"
  content = Coradoc.strip_unicode(Coradoc::Generator.gen_adoc(@content))
  <<~HERE

  #{anchor}#{style_str}#{level_str} #{content}
  HERE
end