move util methods into their own namespace

This commit is contained in:
Thomas Reynolds 2012-04-14 13:36:24 -07:00
parent 094de61e92
commit 0bc35db4a6
8 changed files with 50 additions and 55 deletions

View file

@ -1,32 +1,18 @@
require "rbconfig" # Using a bunch of convenience methods
# Using Thor's indifferent hash access
require "thor"
require "active_support" require "active_support"
# Setup our load paths # Setup our load paths
libdir = File.expand_path(File.dirname(__FILE__)) libdir = File.expand_path(File.dirname(__FILE__))
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir) $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
class String
def camelize
self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end
end
# Simple callback library # Simple callback library
require "middleman-core/vendor/hooks-0.2.0/lib/hooks" require "middleman-core/vendor/hooks-0.2.0/lib/hooks"
require "middleman-core/version" require "middleman-core/version"
require "middleman-core/util"
# Top-level Middleman object # Top-level Middleman object
module Middleman module Middleman
WINDOWS = !!(RUBY_PLATFORM =~ /(mingw|bccwin|wince|mswin32)/i) unless const_defined?(:WINDOWS)
JRUBY = !!(RbConfig::CONFIG["RUBY_INSTALL_NAME"] =~ /^jruby/i) unless const_defined?(:JRUBY)
DARWIN = RbConfig::CONFIG['target_os'] =~ /darwin/i unless const_defined?(:DARWIN)
LINUX = RbConfig::CONFIG['target_os'] =~ /linux/i unless const_defined?(:LINUX)
# Auto-load modules on-demand # Auto-load modules on-demand
autoload :Base, "middleman-core/base" autoload :Base, "middleman-core/base"
autoload :Cache, "middleman-core/cache" autoload :Cache, "middleman-core/cache"
@ -181,36 +167,6 @@ module Middleman
EXTENSION_FILE = File.join("lib", "middleman_extension.rb") unless const_defined?(:EXTENSION_FILE) EXTENSION_FILE = File.join("lib", "middleman_extension.rb") unless const_defined?(:EXTENSION_FILE)
class << self class << self
# Recursively convert a normal Hash into a HashWithIndifferentAccess
#
# @private
# @param [Hash] data Normal hash
# @return [Thor::CoreExt::HashWithIndifferentAccess]
def 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 normalize_path(path)
path.sub(/^\//, "").gsub("%20", " ")
end
# Automatically load extensions from available RubyGems # Automatically load extensions from available RubyGems
# which contain the EXTENSION_FILE # which contain the EXTENSION_FILE
# #

View file

@ -101,7 +101,7 @@ module Middleman::CoreExtensions::Data
return return
end end
@local_data[basename] = ::Middleman.recursively_enhance(data) @local_data[basename] = ::Middleman::Util.recursively_enhance(data)
end end
# Remove a given file from the internal cache # Remove a given file from the internal cache
@ -144,7 +144,7 @@ module Middleman::CoreExtensions::Data
result = data_for_path(path) result = data_for_path(path)
if result if result
return ::Middleman.recursively_enhance(result) return ::Middleman::Util.recursively_enhance(result)
end end
end end

View file

@ -153,7 +153,7 @@ module Middleman::CoreExtensions::FrontMatter
if result if result
data, content = result data, content = result
data = ::Middleman.recursively_enhance(data).freeze data = ::Middleman::Util.recursively_enhance(data).freeze
file = file.sub(@app.source_dir, "") file = file.sub(@app.source_dir, "")
@local_data[file] = [data, content] @local_data[file] = [data, content]
path = File.join(@app.source_dir, file) path = File.join(@app.source_dir, file)

View file

@ -49,7 +49,7 @@ module Middleman::Sitemap::Extensions
if path.is_a? Regexp if path.is_a? Regexp
@ignored_callbacks << Proc.new {|p| p =~ path } @ignored_callbacks << Proc.new {|p| p =~ path }
elsif path.is_a? String 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 if path_clean.include?("*") # It's a glob
@ignored_callbacks << Proc.new {|p| File.fnmatch(path_clean, p) } @ignored_callbacks << Proc.new {|p| File.fnmatch(path_clean, p) }
else else
@ -66,7 +66,7 @@ module Middleman::Sitemap::Extensions
# @param [String] path # @param [String] path
# @return [Boolean] # @return [Boolean]
def ignored?(path) def ignored?(path)
path_clean = ::Middleman.normalize_path(path) path_clean = ::Middleman::Util.normalize_path(path)
@ignored_callbacks.any? { |b| b.call(path_clean) } @ignored_callbacks.any? { |b| b.call(path_clean) }
end end

View file

@ -75,7 +75,7 @@ module Middleman::Sitemap::Extensions
# @param [String] target # @param [String] target
# @return [void] # @return [void]
def proxy(path, target) 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) @app.sitemap.rebuild_resource_list!(:added_proxy)
end end

View file

@ -65,7 +65,7 @@ module Middleman::Sitemap
# @param [String] request_path The original path of a resource. # @param [String] request_path The original path of a resource.
# @return [Middleman::Sitemap::Resource] # @return [Middleman::Sitemap::Resource]
def find_resource_by_path(request_path) 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] @_lookup_cache[:path][request_path]
end end
@ -73,7 +73,7 @@ module Middleman::Sitemap
# @param [String] request_path The destination (output) path of a resource. # @param [String] request_path The destination (output) path of a resource.
# @return [Middleman::Sitemap::Resource] # @return [Middleman::Sitemap::Resource]
def find_resource_by_destination_path(request_path) 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] @_lookup_cache[:destination_path][request_path]
end end

View 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

View file

@ -1,6 +1,10 @@
# File changes are forwarded to the currently running app via HTTP # File changes are forwarded to the currently running app via HTTP
require "net/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 "win32/process" if ::Middleman::WINDOWS
require "fileutils" require "fileutils"