Class: Coradoc::Markdown::TocGenerator::Entry
- Inherits:
-
Object
- Object
- Coradoc::Markdown::TocGenerator::Entry
- Defined in:
- lib/coradoc/markdown/toc_generator.rb
Overview
Represents a single TOC entry
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#id ⇒ Object
Returns the value of attribute id.
-
#level ⇒ Object
Returns the value of attribute level.
-
#number ⇒ Object
Returns the value of attribute number.
-
#text ⇒ Object
Returns the value of attribute text.
Instance Method Summary collapse
-
#initialize(id:, text:, level:, number: nil) ⇒ Entry
constructor
A new instance of Entry.
-
#to_h ⇒ Hash
Convert entry to hash representation.
- #to_markdown(indent: 0) ⇒ Object
Constructor Details
#initialize(id:, text:, level:, number: nil) ⇒ Entry
Returns a new instance of Entry.
38 39 40 41 42 43 44 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 38 def initialize(id:, text:, level:, number: nil) @id = id @text = text @level = level @number = number @children = [] end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
36 37 38 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 36 def children @children end |
#id ⇒ Object
Returns the value of attribute id.
36 37 38 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 36 def id @id end |
#level ⇒ Object
Returns the value of attribute level.
36 37 38 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 36 def level @level end |
#number ⇒ Object
Returns the value of attribute number.
36 37 38 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 36 def number @number end |
#text ⇒ Object
Returns the value of attribute text.
36 37 38 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 36 def text @text end |
Instance Method Details
#to_h ⇒ Hash
Convert entry to hash representation
59 60 61 62 63 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 59 def to_h result = { id: @id, text: @text, level: @level, number: @number } result[:children] = @children.map(&:to_h) unless @children.empty? result end |
#to_markdown(indent: 0) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 46 def to_markdown(indent: 0) prefix = ' ' * indent link = @id && @id != @text ? "[#{@text}](##{@id})" : @text number_prefix = @number ? "#{@number} " : '' "#{prefix}* #{number_prefix}#{link}\n".tap do |result| @children.each do |child| result << child.to_markdown(indent: indent + 1) end end end |