class RTFDoc::Generator

Attributes

config[R]
renderer[R]

Public Class Methods

new(config_path) click to toggle source
# File lib/rtfdoc.rb, line 461
def initialize(config_path)
  @config       = YAML.load_file(config_path)
  @content_dir  = @config['content_dir']
  @parts        = {}
end

Public Instance Methods

run() click to toggle source
# File lib/rtfdoc.rb, line 467
def run
  @tree = build_content_tree
  nodes = build_nodes(config['resources'])

  out = File.new("#{Dir.tmpdir}/rtfdoc_output.html", 'w')
  out.write(Template.new(nodes, config).output)
  out.close
end

Private Instance Methods

build_content_tree() click to toggle source
# File lib/rtfdoc.rb, line 499
def build_content_tree
  tree        = {}
  slicer      = (@content_dir.length + 1)..-1
  ext_slicer  = -3..-1

  Dir.glob("#{@content_dir}/**/*.md").each do |path|
    str       = path.slice(slicer)
    parts     = str.split('/')
    filename  = parts.pop
    filename.slice!(ext_slicer)

    leaf = parts.reduce(tree) { |h, part| h[part] || h[part] = {} }
    leaf[filename] = path
  end

  tree
end
build_nodes(ary, allow_groups: true) click to toggle source
# File lib/rtfdoc.rb, line 478
def build_nodes(ary, allow_groups: true)
  ary.map do |rs|
    if rs.is_a?(Hash)
      name, values = rs.each_pair.first

      if name.start_with?('group|')
        raise 'Nested groups are not yet supported' if !allow_groups

        group_name = values.key?('title') ? values['title'] : name.slice(6..-1)
        Group.new(group_name, build_nodes(values['resources'], allow_groups: false), values)
      else
        paths = @tree[name]
        Resource.build(name, paths, endpoints: values)
      end
    else
      paths = @tree[rs]
      paths.is_a?(Hash) ? Resource.build(rs, paths) : Section.new(rs, File.read(paths))
    end
  end
end