Refactor protected method normalize_path out of Sitemap::Store

This commit is contained in:
Ben Hollis 2012-01-31 22:38:39 -08:00
parent 1fc97d9d7b
commit 2eec7e11ba

View file

@ -22,7 +22,7 @@ module Middleman::Sitemap
# @param [String] path # @param [String] path
# @return [Boolean] # @return [Boolean]
def exists?(path) def exists?(path)
@pages.has_key?(path.sub(/^\//, "")) @pages.has_key?(normalize_path(path))
end end
# Ignore a path or add an ignore callback # Ignore a path or add an ignore callback
@ -30,10 +30,10 @@ module Middleman::Sitemap
# @return [void] # @return [void]
def ignore(path=nil, &block) def ignore(path=nil, &block)
if !path.nil? && path.include?("*") if !path.nil? && path.include?("*")
path_clean = path.sub(/^\//, "") path_clean = normalize_path(path)
@ignored_globs << path_clean unless @ignored_globs.include?(path_clean) @ignored_globs << path_clean unless @ignored_globs.include?(path_clean)
elsif path.is_a? String elsif path.is_a? String
path_clean = path.sub(/^\//, "") path_clean = normalize_path(path)
@ignored_paths << path_clean unless @ignored_paths.include?(path_clean) @ignored_paths << path_clean unless @ignored_paths.include?(path_clean)
elsif path.is_a? Regexp elsif path.is_a? Regexp
@ignored_regexes << path unless @ignored_regexes.include?(path) @ignored_regexes << path unless @ignored_regexes.include?(path)
@ -47,7 +47,7 @@ module Middleman::Sitemap
# @param [String] target # @param [String] target
# @return [void] # @return [void]
def proxy(path, target) def proxy(path, target)
page(path).proxy_to(target.sub(%r{^/}, "")) page(path).proxy_to(normalize_path(target))
app.cache.remove(:proxied_paths) app.cache.remove(:proxied_paths)
end end
@ -55,13 +55,13 @@ module Middleman::Sitemap
# @param [String] path # @param [String] path
# @return [Middleman::Sitemap::Page] # @return [Middleman::Sitemap::Page]
def page(path) def page(path)
path = path.sub(/^\//, "").gsub("%20", " ") path = normalize_path(path)
@pages.fetch(path) { @pages[path] = ::Middleman::Sitemap::Page.new(self, path) } @pages.fetch(path) { @pages[path] = ::Middleman::Sitemap::Page.new(self, path) }
end end
# Loop over known pages # Loop over known pages
# @return [void] # @return [void]
def each(&block) def each
@pages.each do |k, v| @pages.each do |k, v|
yield k, v yield k, v
end end
@ -77,9 +77,7 @@ module Middleman::Sitemap
# @param [String] path # @param [String] path
# @return [Boolean] # @return [Boolean]
def ignored?(path) def ignored?(path)
path_clean = path.sub(/^\//, "") path_clean = normalize_path(path)
# $stderr.puts path_clean, @ignored_globs, @ignored_paths
return true if @ignored_paths.include?(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_globs.any? { |g| File.fnmatch(g, path_clean) }
@ -99,7 +97,7 @@ module Middleman::Sitemap
# @param [String] path # @param [String] path
# @return [Boolean] # @return [Boolean]
def generic?(path) def generic?(path)
generic_paths.include?(path.sub(/^\//, "")) generic_paths.include?(normalize_path(path))
end end
# Get a list of generic paths # Get a list of generic paths
@ -114,7 +112,7 @@ module Middleman::Sitemap
# @param [String] path # @param [String] path
# @return [Boolean] # @return [Boolean]
def proxied?(path) def proxied?(path)
proxied_paths.include?(path.sub(/^\//, "")) proxied_paths.include?(normalize_path(path))
end end
# Get a list of proxied paths # Get a list of proxied paths
@ -132,7 +130,7 @@ module Middleman::Sitemap
path = file_to_path(file) path = file_to_path(file)
return false unless path return false unless path
path = path.sub(/^\//, "") path = normalize_path(path)
if @pages.has_key?(path) if @pages.has_key?(path)
page(path).delete() page(path).delete()
@pages.delete(path) @pages.delete(path)
@ -187,7 +185,7 @@ module Middleman::Sitemap
# Get a path without templating extensions # Get a path without templating extensions
# @param [String] file # @param [String] file
# @param [String] # @return [String]
def extensionless_path(file) def extensionless_path(file)
app.cache.fetch(:extensionless_path, file) do app.cache.fetch(:extensionless_path, file) do
path = file.dup path = file.dup
@ -204,5 +202,12 @@ module Middleman::Sitemap
path path
end end
end end
# Normalize a path to not include a leading slash
# @param [String] path
# @return [String]
def normalize_path(path)
path.sub(/^\//, "").gsub("%20", " ")
end
end end
end end