module BookLab::Toc

Constants

VERSION

Public Class Methods

parse(raw, format: :yml, strict: false) click to toggle source
# File lib/booklab-toc.rb, line 15
def parse(raw, format: :yml, strict: false)
  items = []
  datas = []

  case format
  when :yml
    datas = YAML.load(raw)
  when :json
    datas = JSON.load(raw)
  when :markdown
    datas = Format::Markdown.load(raw)
  else
    raise "format: #{format} not implement"
  end

  datas.each do |obj|
    item_hash = obj.to_hash
    item_hash.deep_symbolize_keys!
    items << ListItem.new(id: item_hash[:id], title: item_hash[:title], url: item_hash[:url], depth: item_hash[:depth])
  end
  Content.new(items)
rescue => e
  raise FormatError.new(e.message) if strict
  logger.warn "BookLab::Toc.parse error:\n#{e.inspect}"
  Content.new([])
end