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.
44 45 46 47 48 49 50 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 44 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.
42 43 44 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 42 def children @children end |
#id ⇒ Object
Returns the value of attribute id.
42 43 44 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 42 def id @id end |
#level ⇒ Object
Returns the value of attribute level.
42 43 44 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 42 def level @level end |
#number ⇒ Object
Returns the value of attribute number.
42 43 44 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 42 def number @number end |
#text ⇒ Object
Returns the value of attribute text.
42 43 44 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 42 def text @text end |
Instance Method Details
#to_h ⇒ Hash
Convert entry to hash representation
65 66 67 68 69 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 65 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
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/coradoc/markdown/toc_generator.rb', line 52 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 |