class TocList

Constants

HashMethod
VERSION

Attributes

parent[RW]
sections_tmpl[W]
title_tmpl[W]

Public Class Methods

new(toc_hash, tmpl={}) click to toggle source
# File lib/toc_list.rb, line 7
def initialize toc_hash, tmpl={}
  @toc = toc_hash

  @title_tmpl_path = optional tmpl, :title
  @sections_tmpl_path = optional tmpl, :sections
  @container_tmpl_path = optional tmpl, :container

  @hash_method = block_given? ? hash_method : HashMethod
end

Public Instance Methods

render() click to toggle source
# File lib/toc_list.rb, line 25
def render
  @sections_tmpl ||= parse_tmpl @sections_tmpl_path
  toc = @sections_tmpl.result binding
  toc = wrap toc if parent.nil?
  toc
end
sections() click to toggle source
# File lib/toc_list.rb, line 21
def sections
  @toc.values[0]
end
title() click to toggle source
# File lib/toc_list.rb, line 17
def title
  @toc.keys[0]
end

Private Instance Methods

def_tmpl(filename) click to toggle source
# File lib/toc_list.rb, line 73
def def_tmpl filename
  sub_lib_dir = File.expand_path(__FILE__).sub(/\.rb$/, '')
  File.join sub_lib_dir, filename
end
optional(tmpl, key) click to toggle source
# File lib/toc_list.rb, line 39
def optional tmpl, key
  tmpl.fetch(key, def_tmpl(key.to_s + '.erb'))
end
parse_tmpl(path) click to toggle source
# File lib/toc_list.rb, line 69
def parse_tmpl path
  ERB.new File.read path
end
render_item(item) click to toggle source
# File lib/toc_list.rb, line 48
def render_item item
  @title_tmpl ||= parse_tmpl @title_tmpl_path
  @title_tmpl.result binding
end
render_section(section) click to toggle source
# File lib/toc_list.rb, line 57
def render_section section
  if section.is_a? String
    render_item(section)
  else
    sec = self.class.new section
    sec.title_tmpl = @title_tmpl
    sec.sections_tmpl = @sections_tmpl
    sec.parent = self
    sec.render
  end
end
wrap(toc) click to toggle source
# File lib/toc_list.rb, line 43
def wrap toc
  ctnr_tmpl = parse_tmpl @container_tmpl_path
  ctnr_tmpl.result binding
end