Class: Coradoc::Markdown::TocGenerator::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/markdown/toc_generator.rb

Overview

Represents a single TOC entry

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#childrenObject

Returns the value of attribute children.



36
37
38
# File 'lib/coradoc/markdown/toc_generator.rb', line 36

def children
  @children
end

#idObject

Returns the value of attribute id.



36
37
38
# File 'lib/coradoc/markdown/toc_generator.rb', line 36

def id
  @id
end

#levelObject

Returns the value of attribute level.



36
37
38
# File 'lib/coradoc/markdown/toc_generator.rb', line 36

def level
  @level
end

#numberObject

Returns the value of attribute number.



36
37
38
# File 'lib/coradoc/markdown/toc_generator.rb', line 36

def number
  @number
end

#textObject

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_hHash

Convert entry to hash representation

Returns:

  • (Hash)

    Hash with id, text, level, number, and optional children



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