module Middleman::Sitemap::Extensions::Traversal

def children

Returns:
  • (Array) -
def children
  return [] unless directory_index?
  base_path = if eponymous_directory?
    eponymous_directory_path
  else
    path.sub(@app.config[:index_file].to_s, '')
  end
  prefix = %r{^#{base_path.sub("/", "\\/")}}
  @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.config[:index_file]
      else
        false
      end
    end
  end
end

def directory_index?

Returns:
  • (Boolean) -
def directory_index?
  path.include?(@app.config[:index_file]) || path =~ /\/$/ || eponymous_directory?
end

def eponymous_directory?

Returns:
  • (Boolean) -
def eponymous_directory?
  if !path.end_with?("/#{@app.config[:index_file]}") && destination_path.end_with?("/#{@app.config[:index_file]}")
    return true
  end
  @app.files.by_type(:source).watchers.any? do |source|
    (source.directory + Pathname(eponymous_directory_path)).directory?
  end
end

def eponymous_directory_path

Returns:
  • (String) -
def eponymous_directory_path
  path.sub(ext, '/').sub(/\/$/, '') + '/'
end

def parent

Returns:
  • (Middleman::Sitemap::Resource, nil) -
def parent
  root = path.sub(/^#{::Regexp.escape(traversal_root)}/, '')
  parts = root.split('/')
  tail = parts.pop
  is_index = (tail == @app.config[:index_file])
  if parts.empty?
    return is_index ? nil : @store.find_resource_by_path(@app.config[:index_file])
  end
  test_expr = parts.join('\\/')
  test_expr = %r{^#{test_expr}(?:\.[a-zA-Z0-9]+|\/)$}
  # eponymous reverse-lookup
  found = @store.resources.find do |candidate|
    candidate.path =~ test_expr
  end
  if found
    found
  else
    parts.pop if is_index
    @store.find_resource_by_destination_path("#{parts.join('/')}/#{@app.config[:index_file]}")
  end
end

def siblings

Returns:
  • (Array) -
def siblings
  return [] unless parent
  parent.children.reject { |p| p == self }
end

def traversal_root

def traversal_root
  root = if !@app.extensions[:i18n]
    '/'
  else
    @app.extensions[:i18n].path_root(::I18n.locale)
  end
  root.sub(/^\//, '')
end