Fix ignore with a regex (with a test) and consolidate ignores into a single list of callbacks.

This commit is contained in:
Ben Hollis 2012-03-24 20:27:55 -07:00
parent d51056cb26
commit 086f05989b
2 changed files with 28 additions and 18 deletions

View file

@ -33,6 +33,26 @@ Feature: Ignoring paths
| build/reports/another.html | | build/reports/another.html |
| build/images/icons/messages.png | | build/images/icons/messages.png |
Scenario: Ignore a regex
Given a fixture app "ignore-app"
And a file named "config.rb" with:
"""
ignore /^.*\.erb/
ignore /^reports\/.*/
ignore /^images\.*\.png/
"""
And a successfully built app at "ignore-app"
Then the following files should exist:
| build/plain.html |
| build/images/portrait.jpg |
| build/images/pic.png |
And the following files should not exist:
| build/about.html |
| build/index.html |
| build/reports/index.html |
| build/reports/another.html |
| build/images/icons/messages.png |
Scenario: Ignore with directory indexes (source file) Scenario: Ignore with directory indexes (source file)
Given a fixture app "ignore-app" Given a fixture app "ignore-app"
And a file named "config.rb" with: And a file named "config.rb" with:

View file

@ -17,9 +17,6 @@ module Middleman::Sitemap
def initialize(app) def initialize(app)
@app = app @app = app
@pages = {} @pages = {}
@ignored_paths = []
@ignored_globs = []
@ignored_regexes = []
@ignored_callbacks = [] @ignored_callbacks = []
@reroute_callbacks = [] @reroute_callbacks = []
end end
@ -41,14 +38,15 @@ module Middleman::Sitemap
# @param [String, Regexp] path, path glob expression, or path regex # @param [String, Regexp] path, path glob expression, or path regex
# @return [void] # @return [void]
def ignore(path=nil, &block) def ignore(path=nil, &block)
if !path.nil? && path.include?("*") if path.is_a? Regexp
path_clean = normalize_path(path) @ignored_callbacks << Proc.new {|p| p =~ path }
@ignored_globs << path_clean unless @ignored_globs.include?(path_clean)
elsif path.is_a? String elsif path.is_a? String
path_clean = normalize_path(path) path_clean = normalize_path(path)
@ignored_paths << path_clean unless @ignored_paths.include?(path_clean) if path_clean.include?("*") # It's a glob
elsif path.is_a? Regexp @ignored_callbacks << Proc.new {|p| File.fnmatch(path_clean, p) }
@ignored_regexes << path unless @ignored_regexes.include?(path) else
@ignored_callbacks << Proc.new {|p| p == path_clean }
end
elsif block_given? elsif block_given?
@ignored_callbacks << block @ignored_callbacks << block
end end
@ -107,15 +105,7 @@ module Middleman::Sitemap
# @return [Boolean] # @return [Boolean]
def ignored?(path) def ignored?(path)
path_clean = normalize_path(path) path_clean = normalize_path(path)
@ignored_callbacks.any? { |b| b.call(path_clean) }
return true if @ignored_paths.include?(path_clean)
return true if @ignored_globs.any? { |g| File.fnmatch(g, path_clean) }
return true if @ignored_regexes.any? { |r| r.match(path_clean) }
return true if @ignored_callbacks.any? { |b| b.call(path_clean) }
# TODO: We should also check ignored_sitemap_matchers here
false
end end
# Remove a file from the store # Remove a file from the store