Class: Mdtoc::Node

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

Direct Known Subclasses

DirNode, FileNode

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.



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

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

Class Method Details

.for_path(path, depth = 0) ⇒ Object



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

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

.render(paths) ⇒ Object



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

def render(paths)
  paths
    .flat_map { |path| for_path(path).headers }
    .join("\n")
end

Instance Method Details

#headersObject



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

def headers; end

#labelObject



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

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