module DocTemplate::Objects::TocHelpers

Public Instance Methods

find_by_anchor(anchor) click to toggle source
# File lib/doc_template/objects/toc_helpers.rb, line 24
def find_by_anchor(anchor)
  l1 = children.find { |c| c.anchor == anchor }
  raise DocumentError, "Anchor #{anchor} not found at metadata" if l1.blank?

  l1
end
level1_by_title(title) click to toggle source
# File lib/doc_template/objects/toc_helpers.rb, line 8
def level1_by_title(title)
  l1 = children.find { |c| !c.handled && c.title.parameterize == title }
  raise DocumentError, "Level1 header #{title} not found at metadata" unless l1.present?

  l1.handled = true
  l1
end
level2_by_title(title) click to toggle source
# File lib/doc_template/objects/toc_helpers.rb, line 16
def level2_by_title(title)
  children.each do |c|
    l2 = c.children.find { |c1| c1.title.parameterize == title }
    return l2 if l2.present?
  end
  raise DocumentError, "Level2 header #{title} not found at metadata"
end
set_index(data, params = { idx: 0 }) click to toggle source
# File lib/doc_template/objects/toc_helpers.rb, line 41
def set_index(data, params = { idx: 0 })
  return data if data['idx'].present?

  data['idx'] = params[:idx]
  params[:idx] += 1
  (data[:children] || []).each { |c| set_index(c, params) }
  data
end