move util methods into their own namespace
This commit is contained in:
parent
094de61e92
commit
0bc35db4a6
8 changed files with 50 additions and 55 deletions
|
@ -101,7 +101,7 @@ module Middleman::CoreExtensions::Data
|
|||
return
|
||||
end
|
||||
|
||||
@local_data[basename] = ::Middleman.recursively_enhance(data)
|
||||
@local_data[basename] = ::Middleman::Util.recursively_enhance(data)
|
||||
end
|
||||
|
||||
# Remove a given file from the internal cache
|
||||
|
@ -144,7 +144,7 @@ module Middleman::CoreExtensions::Data
|
|||
result = data_for_path(path)
|
||||
|
||||
if result
|
||||
return ::Middleman.recursively_enhance(result)
|
||||
return ::Middleman::Util.recursively_enhance(result)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -153,7 +153,7 @@ module Middleman::CoreExtensions::FrontMatter
|
|||
|
||||
if result
|
||||
data, content = result
|
||||
data = ::Middleman.recursively_enhance(data).freeze
|
||||
data = ::Middleman::Util.recursively_enhance(data).freeze
|
||||
file = file.sub(@app.source_dir, "")
|
||||
@local_data[file] = [data, content]
|
||||
path = File.join(@app.source_dir, file)
|
||||
|
|
|
@ -49,7 +49,7 @@ module Middleman::Sitemap::Extensions
|
|||
if path.is_a? Regexp
|
||||
@ignored_callbacks << Proc.new {|p| p =~ path }
|
||||
elsif path.is_a? String
|
||||
path_clean = ::Middleman.normalize_path(path)
|
||||
path_clean = ::Middleman::Util.normalize_path(path)
|
||||
if path_clean.include?("*") # It's a glob
|
||||
@ignored_callbacks << Proc.new {|p| File.fnmatch(path_clean, p) }
|
||||
else
|
||||
|
@ -66,7 +66,7 @@ module Middleman::Sitemap::Extensions
|
|||
# @param [String] path
|
||||
# @return [Boolean]
|
||||
def ignored?(path)
|
||||
path_clean = ::Middleman.normalize_path(path)
|
||||
path_clean = ::Middleman::Util.normalize_path(path)
|
||||
@ignored_callbacks.any? { |b| b.call(path_clean) }
|
||||
end
|
||||
|
||||
|
|
|
@ -75,7 +75,7 @@ module Middleman::Sitemap::Extensions
|
|||
# @param [String] target
|
||||
# @return [void]
|
||||
def proxy(path, target)
|
||||
@proxy_paths[::Middleman.normalize_path(path)] = ::Middleman.normalize_path(target)
|
||||
@proxy_paths[::Middleman::Util.normalize_path(path)] = ::Middleman::Util.normalize_path(target)
|
||||
@app.sitemap.rebuild_resource_list!(:added_proxy)
|
||||
end
|
||||
|
||||
|
|
|
@ -65,7 +65,7 @@ module Middleman::Sitemap
|
|||
# @param [String] request_path The original path of a resource.
|
||||
# @return [Middleman::Sitemap::Resource]
|
||||
def find_resource_by_path(request_path)
|
||||
request_path = ::Middleman.normalize_path(request_path)
|
||||
request_path = ::Middleman::Util.normalize_path(request_path)
|
||||
@_lookup_cache[:path][request_path]
|
||||
end
|
||||
|
||||
|
@ -73,7 +73,7 @@ module Middleman::Sitemap
|
|||
# @param [String] request_path The destination (output) path of a resource.
|
||||
# @return [Middleman::Sitemap::Resource]
|
||||
def find_resource_by_destination_path(request_path)
|
||||
request_path = ::Middleman.normalize_path(request_path)
|
||||
request_path = ::Middleman::Util.normalize_path(request_path)
|
||||
@_lookup_cache[:destination_path][request_path]
|
||||
end
|
||||
|
||||
|
|
35
middleman-core/lib/middleman-core/util.rb
Normal file
35
middleman-core/lib/middleman-core/util.rb
Normal file
|
@ -0,0 +1,35 @@
|
|||
# Using Thor's indifferent hash access
|
||||
require "thor"
|
||||
|
||||
module Middleman
|
||||
module Util
|
||||
# Recursively convert a normal Hash into a HashWithIndifferentAccess
|
||||
#
|
||||
# @private
|
||||
# @param [Hash] data Normal hash
|
||||
# @return [Thor::CoreExt::HashWithIndifferentAccess]
|
||||
def self.recursively_enhance(data)
|
||||
if data.is_a? Hash
|
||||
data = ::Thor::CoreExt::HashWithIndifferentAccess.new(data)
|
||||
data.each do |key, val|
|
||||
data[key] = recursively_enhance(val)
|
||||
end
|
||||
data
|
||||
elsif data.is_a? Array
|
||||
data.each_with_index do |val, i|
|
||||
data[i] = recursively_enhance(val)
|
||||
end
|
||||
data
|
||||
else
|
||||
data
|
||||
end
|
||||
end
|
||||
|
||||
# Normalize a path to not include a leading slash
|
||||
# @param [String] path
|
||||
# @return [String]
|
||||
def self.normalize_path(path)
|
||||
path.sub(/^\//, "").gsub("%20", " ")
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,6 +1,10 @@
|
|||
# File changes are forwarded to the currently running app via HTTP
|
||||
require "net/http"
|
||||
|
||||
module Middleman
|
||||
WINDOWS = !!(RUBY_PLATFORM =~ /(mingw|bccwin|wince|mswin32)/i) unless const_defined?(:WINDOWS)
|
||||
end
|
||||
|
||||
require "win32/process" if ::Middleman::WINDOWS
|
||||
|
||||
require "fileutils"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue