Module: Middleman::Sitemap::Extensions::Traversal

Included in:
Resource
Defined in:
lib/middleman-core/sitemap/extensions/traversal.rb

Instance Method Summary collapse

Instance Method Details

#childrenArray<Middleman::Sitemap::Resource>

This resource’s child resources



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/middleman-core/sitemap/extensions/traversal.rb', line 30

def children
  return [] unless directory_index?

  if eponymous_directory?
    base_path = eponymous_directory_path
    prefix    = %r{^#{base_path.sub("/", "\\/")}}
  else
    base_path = path.sub("#{app.index_file}", '')
    prefix    = %r{^#{base_path.sub("/", "\\/")}}
  end

  store.resources.select do |sub_resource|
    if sub_resource.path == path || sub_resource.path !~ prefix
      false
    else
      inner_path = sub_resource.path.sub(prefix, '')
      parts = inner_path.split('/')
      if parts.length == 1
        true
      elsif parts.length == 2
        parts.last == app.index_file
      else
        false
      end
    end
  end
end

#directory_index?Boolean

Whether this resource either a directory index, or has the same name as an existing directory in the source



67
68
69
# File 'lib/middleman-core/sitemap/extensions/traversal.rb', line 67

def directory_index?
  path.include?(app.index_file) || path =~ /\/$/ || eponymous_directory?
end

#eponymous_directory?Boolean

Whether the resource has the same name as a directory in the source (e.g., if the resource is named ‘gallery.html’ and a path exists named ‘gallery/’, this would return true)



74
75
76
77
78
79
80
81
# File 'lib/middleman-core/sitemap/extensions/traversal.rb', line 74

def eponymous_directory?
  if !path.end_with?("/#{app.index_file}") && destination_path.end_with?("/#{app.index_file}")
    return true
  end

  full_path = File.join(app.source_dir, eponymous_directory_path)
  File.exist?(full_path) && File.directory?(full_path)
end

#eponymous_directory_pathString

The path for this resource if it were a directory, and not a file (e.g., for ‘gallery.html’ this would return ‘gallery/’)



86
87
88
# File 'lib/middleman-core/sitemap/extensions/traversal.rb', line 86

def eponymous_directory_path
  path.sub(ext, '/').sub(/\/$/, '') + '/'
end

#parentMiddleman::Sitemap::Resource?

This resource’s parent resource



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/middleman-core/sitemap/extensions/traversal.rb', line 7

def parent
  parts = path.split('/')
  tail = parts.pop
  is_index = (tail == app.index_file)

  return nil if is_index && parts.length < 1

  test_expr = parts.join('\\/')
  # A makeshift for eponymous reverse-lookup
  found = store.resources.find do |candidate|
    candidate.path =~ %r{^#{test_expr}(?:\.[a-zA-Z0-9]+|\/)$}
  end

  if found
    found
  else
    parts.pop if is_index
    store.find_resource_by_destination_path("#{parts.join('/')}/#{app.index_file}")
  end
end

#siblingsArray<Middleman::Sitemap::Resource>

This resource’s sibling resources



60
61
62
63
# File 'lib/middleman-core/sitemap/extensions/traversal.rb', line 60

def siblings
  return [] unless parent
  parent.children.reject { |p| p == self }
end