Class: Mdtoc::Node

Inherits:
Object
  • Object
show all
Extended by:
T::Helpers, T::Sig
Defined in:
lib/mdtoc/node.rb

Defined Under Namespace

Classes: DirNode, FileNode

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, depth) ⇒ Node

Returns a new instance of Node.



37
38
39
40
# File 'lib/mdtoc/node.rb', line 37

def initialize(path, depth)
  @path = path
  @depth = depth
end

Class Method Details

.for_path(path, depth = 0) ⇒ Object



20
21
22
23
24
25
# File 'lib/mdtoc/node.rb', line 20

def for_path(path, depth = 0)
  # Ensure that `path` is a relative path, so that all links are relative and therefore portable.
  pathname = Pathname.new(path)
  pathname = pathname.relative_path_from(Dir.pwd) if pathname.absolute?
  pathname.directory? ? DirNode.new(pathname, depth) : FileNode.new(pathname, depth)
end

.render(paths) ⇒ Object



28
29
30
31
32
33
# File 'lib/mdtoc/node.rb', line 28

def render(paths)
  headers = paths.flat_map { |path| for_path(path).headers }
  min_depth = headers.map(&:depth).min || 0
  headers.each { |h| h.depth -= min_depth } if min_depth.positive?
  headers.join("\n")
end

Instance Method Details

#headersObject



43
# File 'lib/mdtoc/node.rb', line 43

def headers; end

#labelObject



46
47
48
# File 'lib/mdtoc/node.rb', line 46

def label
  @path.basename(@path.extname).to_s.gsub(/_+/, ' ').gsub(/\s+/, ' ').capitalize
end