2012-05-07 23:41:39 +02:00
|
|
|
module Middleman
|
|
|
|
module Sitemap
|
|
|
|
module Extensions
|
|
|
|
module Traversal
|
2016-01-05 19:58:11 +01:00
|
|
|
def traversal_root
|
|
|
|
root = if !@app.extensions[:i18n]
|
|
|
|
'/'
|
|
|
|
else
|
|
|
|
@app.extensions[:i18n].path_root(::I18n.locale)
|
|
|
|
end
|
|
|
|
|
|
|
|
root.sub(/^\//, '')
|
|
|
|
end
|
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# This resource's parent resource
|
|
|
|
# @return [Middleman::Sitemap::Resource, nil]
|
|
|
|
def parent
|
2016-01-05 19:58:11 +01:00
|
|
|
root = path.sub(/^#{::Regexp.escape(traversal_root)}/, '')
|
|
|
|
parts = root.split('/')
|
|
|
|
|
2014-04-29 15:43:33 +02:00
|
|
|
tail = parts.pop
|
2014-05-28 08:05:37 +02:00
|
|
|
is_index = (tail == @app.config[:index_file])
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2015-02-21 19:07:17 +01:00
|
|
|
if parts.empty?
|
2015-02-24 20:06:28 +01:00
|
|
|
return is_index ? nil : @store.find_resource_by_path(@app.config[:index_file])
|
2015-02-21 19:07:17 +01:00
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2014-04-29 15:43:33 +02:00
|
|
|
test_expr = parts.join('\\/')
|
2014-05-30 23:23:44 +02:00
|
|
|
# eponymous reverse-lookup
|
2014-06-14 22:08:03 +02:00
|
|
|
found = @store.resources.find do |candidate|
|
2014-06-16 18:05:24 +02:00
|
|
|
candidate.path =~ %r{^#{test_expr}(?:\.[a-zA-Z0-9]+|\/)$}
|
2014-05-30 23:23:44 +02:00
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2014-04-29 15:43:33 +02:00
|
|
|
if found
|
2014-05-30 23:23:44 +02:00
|
|
|
found
|
2014-04-29 15:43:33 +02:00
|
|
|
else
|
|
|
|
parts.pop if is_index
|
2014-06-14 22:08:03 +02:00
|
|
|
@store.find_resource_by_destination_path("#{parts.join('/')}/#{@app.config[:index_file]}")
|
2014-04-29 15:43:33 +02:00
|
|
|
end
|
2012-05-07 23:41:39 +02:00
|
|
|
end
|
2012-04-04 19:26:07 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# This resource's child resources
|
|
|
|
# @return [Array<Middleman::Sitemap::Resource>]
|
|
|
|
def children
|
|
|
|
return [] unless directory_index?
|
2012-04-04 19:26:07 +02:00
|
|
|
|
2016-01-14 20:21:42 +01:00
|
|
|
base_path = if eponymous_directory?
|
|
|
|
eponymous_directory_path
|
2012-04-04 19:26:07 +02:00
|
|
|
else
|
2016-01-14 20:21:42 +01:00
|
|
|
path.sub(@app.config[:index_file].to_s, '')
|
2012-05-07 23:41:39 +02:00
|
|
|
end
|
|
|
|
|
2016-01-14 20:21:42 +01:00
|
|
|
prefix = %r{^#{base_path.sub("/", "\\/")}}
|
|
|
|
|
2014-05-28 08:05:37 +02:00
|
|
|
@store.resources.select do |sub_resource|
|
2014-04-29 19:50:21 +02:00
|
|
|
if sub_resource.path == path || sub_resource.path !~ prefix
|
2012-05-07 23:41:39 +02:00
|
|
|
false
|
|
|
|
else
|
2013-12-28 01:26:31 +01:00
|
|
|
inner_path = sub_resource.path.sub(prefix, '')
|
|
|
|
parts = inner_path.split('/')
|
2012-05-07 23:41:39 +02:00
|
|
|
if parts.length == 1
|
|
|
|
true
|
|
|
|
elsif parts.length == 2
|
2014-05-28 08:05:37 +02:00
|
|
|
parts.last == @app.config[:index_file]
|
2012-05-07 23:41:39 +02:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
2012-04-04 19:26:07 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# This resource's sibling resources
|
|
|
|
# @return [Array<Middleman::Sitemap::Resource>]
|
|
|
|
def siblings
|
|
|
|
return [] unless parent
|
|
|
|
parent.children.reject { |p| p == self }
|
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2014-01-10 04:49:41 +01:00
|
|
|
# Whether this resource is either a directory index, or has the same name as an existing directory in the source
|
2012-05-07 23:41:39 +02:00
|
|
|
# @return [Boolean]
|
|
|
|
def directory_index?
|
2014-05-28 08:05:37 +02:00
|
|
|
path.include?(@app.config[:index_file]) || path =~ /\/$/ || eponymous_directory?
|
2012-05-07 23:41:39 +02:00
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# 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)
|
|
|
|
# @return [Boolean]
|
|
|
|
def eponymous_directory?
|
2014-05-28 08:05:37 +02:00
|
|
|
if !path.end_with?("/#{@app.config[:index_file]}") && destination_path.end_with?("/#{@app.config[:index_file]}")
|
2013-05-01 04:52:33 +02:00
|
|
|
return true
|
|
|
|
end
|
2014-07-02 19:11:52 +02:00
|
|
|
|
2014-07-16 03:01:45 +02:00
|
|
|
@app.files.by_type(:source).watchers.any? do |source|
|
|
|
|
(source.directory + Pathname(eponymous_directory_path)).directory?
|
|
|
|
end
|
2012-05-07 23:41:39 +02:00
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2012-05-07 23:41:39 +02:00
|
|
|
# The path for this resource if it were a directory, and not a file
|
|
|
|
# (e.g., for 'gallery.html' this would return 'gallery/')
|
|
|
|
# @return [String]
|
|
|
|
def eponymous_directory_path
|
2013-12-28 01:26:31 +01:00
|
|
|
path.sub(ext, '/').sub(/\/$/, '') + '/'
|
2012-05-07 23:41:39 +02:00
|
|
|
end
|
|
|
|
end
|
2012-04-04 19:26:07 +02:00
|
|
|
end
|
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
end
|