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.



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

#childrenObject

Returns the value of attribute children.



42
43
44
# File 'lib/coradoc/markdown/toc_generator.rb', line 42

def children
  @children
end

#idObject

Returns the value of attribute id.



42
43
44
# File 'lib/coradoc/markdown/toc_generator.rb', line 42

def id
  @id
end

#levelObject

Returns the value of attribute level.



42
43
44
# File 'lib/coradoc/markdown/toc_generator.rb', line 42

def level
  @level
end

#numberObject

Returns the value of attribute number.



42
43
44
# File 'lib/coradoc/markdown/toc_generator.rb', line 42

def number
  @number
end

#textObject

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_hHash

Convert entry to hash representation

Returns:

  • (Hash)

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



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