middleman/middleman-core/lib/middleman-core/sitemap/extensions/ignores.rb

53 lines
1.7 KiB
Ruby
Raw Normal View History

module Middleman
module Sitemap
module Extensions
2014-01-01 03:21:30 +01:00
# Class to handle managing ignores
class Ignores < Extension
def initialize(app, config={}, &block)
super
2014-07-16 03:01:45 +02:00
@app.add_to_config_context(:ignore, &method(:create_ignore))
@app.define_singleton_method(:ignore, &method(:create_ignore))
2014-01-01 03:21:30 +01:00
# Array of callbacks which can ass ignored
2014-07-10 21:35:47 +02:00
@ignored_callbacks = Set.new
2014-01-01 03:21:30 +01:00
2014-07-16 03:01:45 +02:00
@app.sitemap.define_singleton_method(:ignored?, &method(:ignored?))
end
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
# @return [void]
2014-07-03 04:04:34 +02:00
Contract Maybe[Or[String, Regexp]], Proc => Any
2014-01-01 03:21:30 +01:00
def create_ignore(path=nil, &block)
if path.is_a? Regexp
2014-04-29 19:50:21 +02:00
@ignored_callbacks << proc { |p| p =~ path }
2014-01-01 03:21:30 +01:00
elsif path.is_a? String
path_clean = ::Middleman::Util.normalize_path(path)
if path_clean.include?('*') # It's a glob
2014-04-29 19:50:21 +02:00
@ignored_callbacks << proc { |p| File.fnmatch(path_clean, p) }
2014-01-01 03:21:30 +01:00
else
# Add a specific-path ignore unless that path is already covered
return if ignored?(path_clean)
2014-04-29 19:50:21 +02:00
@ignored_callbacks << proc { |p| p == path_clean }
2014-01-01 03:21:30 +01:00
end
elsif block_given?
@ignored_callbacks << block
end
2014-01-01 03:21:30 +01:00
@app.sitemap.invalidate_resources_not_ignored_cache!
end
2014-01-01 03:21:30 +01:00
# Whether a path is ignored
# @param [String] path
# @return [Boolean]
2014-07-03 04:04:34 +02:00
Contract String => Bool
2014-01-01 03:21:30 +01:00
def ignored?(path)
path_clean = ::Middleman::Util.normalize_path(path)
@ignored_callbacks.any? { |b| b.call(path_clean) }
end
end
2012-04-04 19:26:07 +02:00
end
end
end