From 086f05989b1b038731c02f091be03291dec192bb Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 24 Mar 2012 20:27:55 -0700 Subject: [PATCH] Fix ignore with a regex (with a test) and consolidate ignores into a single list of callbacks. --- middleman-core/features/ignore.feature | 20 ++++++++++++++ .../lib/middleman-core/sitemap/store.rb | 26 ++++++------------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/middleman-core/features/ignore.feature b/middleman-core/features/ignore.feature index cb971207..54e06b76 100644 --- a/middleman-core/features/ignore.feature +++ b/middleman-core/features/ignore.feature @@ -33,6 +33,26 @@ Feature: Ignoring paths | build/reports/another.html | | 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) Given a fixture app "ignore-app" And a file named "config.rb" with: diff --git a/middleman-core/lib/middleman-core/sitemap/store.rb b/middleman-core/lib/middleman-core/sitemap/store.rb index 223202e2..80226c87 100644 --- a/middleman-core/lib/middleman-core/sitemap/store.rb +++ b/middleman-core/lib/middleman-core/sitemap/store.rb @@ -17,9 +17,6 @@ module Middleman::Sitemap def initialize(app) @app = app @pages = {} - @ignored_paths = [] - @ignored_globs = [] - @ignored_regexes = [] @ignored_callbacks = [] @reroute_callbacks = [] end @@ -41,14 +38,15 @@ module Middleman::Sitemap # @param [String, Regexp] path, path glob expression, or path regex # @return [void] def ignore(path=nil, &block) - if !path.nil? && path.include?("*") - path_clean = normalize_path(path) - @ignored_globs << path_clean unless @ignored_globs.include?(path_clean) + if path.is_a? Regexp + @ignored_callbacks << Proc.new {|p| p =~ path } elsif path.is_a? String path_clean = normalize_path(path) - @ignored_paths << path_clean unless @ignored_paths.include?(path_clean) - elsif path.is_a? Regexp - @ignored_regexes << path unless @ignored_regexes.include?(path) + if path_clean.include?("*") # It's a glob + @ignored_callbacks << Proc.new {|p| File.fnmatch(path_clean, p) } + else + @ignored_callbacks << Proc.new {|p| p == path_clean } + end elsif block_given? @ignored_callbacks << block end @@ -107,15 +105,7 @@ module Middleman::Sitemap # @return [Boolean] def ignored?(path) path_clean = normalize_path(path) - - 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 + @ignored_callbacks.any? { |b| b.call(path_clean) } end # Remove a file from the store