Class: Indexmap::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/indexmap/parser.rb

Defined Under Namespace

Classes: Entry

Instance Method Summary collapse

Constructor Details

#initialize(path: default_path, rebase_remote_children: false, index_filename: Indexmap.configuration.index_filename, public_path: Indexmap.configuration.public_path) ⇒ Parser

Returns a new instance of Parser.



12
13
14
15
16
17
# File 'lib/indexmap/parser.rb', line 12

def initialize(path: default_path, rebase_remote_children: false, index_filename: Indexmap.configuration.index_filename, public_path: Indexmap.configuration.public_path)
  @source = path.to_s
  @rebase_remote_children = rebase_remote_children
  @index_filename = index_filename
  @public_path = public_path
end

Instance Method Details

#entries(reset: false) ⇒ Object



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

def entries(reset: false)
  return reset! && entries if reset
  return @entries if defined?(@entries)

  visited = Set.new
  @entries = parse_source(@source, visited: visited)
end

#paths(reset: false) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/indexmap/parser.rb', line 27

def paths(reset: false)
  return reset! && paths if reset
  return @paths if defined?(@paths)

  seen = Set.new
  @paths = entries.map do |entry|
    path = extract_path(entry.loc)
    next if path.nil?

    normalized = normalize_path(path)
    next if seen.include?(normalized)

    seen.add(normalized)
    normalized
  end.compact
end

#reset!Object



55
56
57
58
# File 'lib/indexmap/parser.rb', line 55

def reset!
  remove_instance_variable(:@entries) if defined?(@entries)
  remove_instance_variable(:@paths) if defined?(@paths)
end

#urls(base_url:, reset: false) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/indexmap/parser.rb', line 44

def urls(base_url:, reset: false)
  return reset! && urls(base_url: base_url) if reset

  target = URI.parse(base_url)
  port_suffix = (target.port && ![80, 443].include?(target.port)) ? ":#{target.port}" : ""

  paths.map do |path|
    "#{target.scheme}://#{target.host}#{port_suffix}#{path}"
  end
end