2012-05-07 23:41:39 +02:00
|
|
|
module Middleman
|
|
|
|
module Sitemap
|
|
|
|
module Extensions
|
2014-01-01 03:21:30 +01:00
|
|
|
# Class to handle managing ignores
|
2016-01-03 02:37:11 +01:00
|
|
|
class Ignores < ConfigExtension
|
2015-09-15 02:37:35 +02:00
|
|
|
self.resource_list_manipulator_priority = 0
|
|
|
|
|
2015-05-02 20:48:21 +02:00
|
|
|
expose_to_config ignore: :create_ignore
|
|
|
|
|
2014-01-01 03:21:30 +01:00
|
|
|
# Ignore a path or add an ignore callback
|
|
|
|
# @param [String, Regexp] path Path glob expression, or path regex
|
2016-01-03 02:37:11 +01:00
|
|
|
# @return [IgnoreDescriptor]
|
|
|
|
Contract Maybe[Or[String, Regexp]], Maybe[Proc] => RespondTo[:execute_descriptor]
|
2014-01-01 03:21:30 +01:00
|
|
|
def create_ignore(path=nil, &block)
|
2016-01-03 02:37:11 +01:00
|
|
|
@app.sitemap.invalidate_resources_not_ignored_cache!
|
|
|
|
IgnoreDescriptor.new(path, block)
|
|
|
|
end
|
|
|
|
|
|
|
|
IgnoreDescriptor = Struct.new(:path, :block) do
|
|
|
|
def execute_descriptor(_app, resources)
|
|
|
|
resources.map do |r|
|
|
|
|
# Ignore based on the source path (without template extensions)
|
|
|
|
if ignored?(r.path)
|
|
|
|
r.ignore!
|
2012-05-07 23:41:39 +02:00
|
|
|
else
|
2016-01-03 02:37:11 +01:00
|
|
|
# This allows files to be ignored by their source file name (with template extensions)
|
|
|
|
r.ignore! if !r.is_a?(ProxyResource) && r.file_descriptor && ignored?(r.file_descriptor[:relative_path].to_s)
|
2012-05-07 23:41:39 +02:00
|
|
|
end
|
2016-01-03 02:37:11 +01:00
|
|
|
|
|
|
|
r
|
2014-01-01 03:21:30 +01:00
|
|
|
end
|
2012-05-07 23:41:39 +02:00
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2016-01-03 02:37:11 +01:00
|
|
|
def ignored?(match_path)
|
|
|
|
match_path = ::Middleman::Util.normalize_path(match_path)
|
|
|
|
|
|
|
|
if path.is_a? Regexp
|
|
|
|
match_path =~ path
|
|
|
|
elsif path.is_a? String
|
|
|
|
path_clean = ::Middleman::Util.normalize_path(path)
|
2012-08-14 00:39:06 +02:00
|
|
|
|
2016-01-03 02:37:11 +01:00
|
|
|
if path_clean.include?('*') # It's a glob
|
|
|
|
if defined?(::File::FNM_EXTGLOB)
|
|
|
|
::File.fnmatch(path_clean, match_path, ::File::FNM_EXTGLOB)
|
|
|
|
else
|
|
|
|
::File.fnmatch(path_clean, match_path)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
match_path == path_clean
|
|
|
|
end
|
|
|
|
elsif block_given?
|
|
|
|
block.call(match_path)
|
|
|
|
end
|
|
|
|
end
|
2014-01-01 03:21:30 +01:00
|
|
|
end
|
|
|
|
end
|
2012-04-04 19:26:07 +02:00
|
|
|
end
|
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
end
|