class Madman::Navigation

Generate a markdown Table of Contents

Attributes

depth[R]
dir[R]

Public Class Methods

new(dir='.', depth: 10) click to toggle source
# File lib/madman/navigation.rb, line 8
def initialize(dir='.', depth: 10)
  @dir, @depth = dir, depth
end

Public Instance Methods

markdown() click to toggle source
# File lib/madman/navigation.rb, line 16
def markdown
  @markdown ||= markdown!
end
toc() click to toggle source
# File lib/madman/navigation.rb, line 12
def toc
  @toc ||= toc!
end

Private Instance Methods

markdown!() click to toggle source
# File lib/madman/navigation.rb, line 22
def markdown!
  result = []
  toc.each do |item|
    indent = item[:level] * 4
    result.push "#{' ' * indent}1. [#{item[:item].label}](#{item[:item].href})"
  end
  result.join "\n"
end
toc!(path=dir, level=0) click to toggle source
# File lib/madman/navigation.rb, line 31
def toc!(path=dir, level=0)
  return [] unless level < depth
  list = Directory.new(path, dir).list

  result = []
  list.each do |item|
    if item.type == :dir
      result.push level: level, item: item
      result += toc! item.path, level+1
    elsif item.type == :file
      result.push level: level, item: item
    end
  end
  result
end