Module: Middleman::Util
- Defined in:
- lib/middleman-core/util.rb
Defined Under Namespace
Classes: Cache
Class Method Summary collapse
-
.all_files_under(*paths) ⇒ Array
Get a recusive list of files inside a set of paths.
-
.binary?(filename) ⇒ Boolean
Whether the source file is binary.
-
.extract_response_text(response) ⇒ String
Extract the text of a Rack response as a string.
-
.instrument(name, payload = {}, &block) ⇒ Object
Facade for ActiveSupport/Notification.
-
.logger(*args) ⇒ Middleman::Logger
The logger.
-
.normalize_path(path) ⇒ String
Normalize a path to not include a leading slash.
-
.path_match(matcher, path) ⇒ Boolean
Takes a matcher, which can be a literal string or a string containing glob expressions, or a regexp, or a proc, or anything else that responds to #match or #call, and returns whether or not the given path matches that matcher.
-
.recursively_enhance(data) ⇒ Thor::CoreExt::HashWithIndifferentAccess
Recursively convert a normal Hash into a HashWithIndifferentAccess.
Class Method Details
.all_files_under(*paths) ⇒ Array
Get a recusive list of files inside a set of paths. Works with symlinks.
119 120 121 122 123 124 125 126 127 |
# File 'lib/middleman-core/util.rb', line 119 def self.all_files_under(*paths) paths.flatten! paths.map! { |p| Pathname(p) } files = paths.select { |p| p.file? } paths.select {|p| p.directory? }.each do |dir| files << all_files_under(dir.children) end files.flatten end |
.binary?(filename) ⇒ Boolean
Whether the source file is binary.
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/middleman-core/util.rb', line 21 def self.binary?(filename) ext = File.extname(filename) return false if Tilt.registered?(ext.sub('.','')) ext = ".#{ext}" unless ext.to_s[0] == ?. mime = ::Rack::Mime.mime_type(ext, nil) return false unless mime return false if mime.start_with?('text/') return false if mime.include?('xml') return false if mime.include?('json') return false if mime.include?('javascript') true end |
.extract_response_text(response) ⇒ String
Extract the text of a Rack response as a string. Useful for extensions implemented as Rack middleware.
86 87 88 89 90 91 92 93 |
# File 'lib/middleman-core/util.rb', line 86 def self.extract_response_text(response) # The rack spec states all response bodies must respond to each result = '' response.each do |part, s| result << part end result end |
.instrument(name, payload = {}, &block) ⇒ Object
Facade for ActiveSupport/Notification
47 48 49 50 |
# File 'lib/middleman-core/util.rb', line 47 def self.instrument(name, payload={}, &block) name << ".middleman" unless name =~ /\.middleman$/ ::ActiveSupport::Notifications.instrument(name, payload, &block) end |
.logger(*args) ⇒ Middleman::Logger
The logger
38 39 40 41 42 43 44 |
# File 'lib/middleman-core/util.rb', line 38 def self.logger(*args) if !@_logger || args.length > 0 @_logger = ::Middleman::Logger.new(*args) end @_logger end |
.normalize_path(path) ⇒ String
Normalize a path to not include a leading slash
77 78 79 80 |
# File 'lib/middleman-core/util.rb', line 77 def self.normalize_path(path) # The tr call works around a bug in Ruby's Unicode handling path.sub(/^\//, "").tr('','') end |
.path_match(matcher, path) ⇒ Boolean
Takes a matcher, which can be a literal string or a string containing glob expressions, or a regexp, or a proc, or anything else that responds to #match or #call, and returns whether or not the given path matches that matcher.
104 105 106 107 108 109 110 111 112 |
# File 'lib/middleman-core/util.rb', line 104 def self.path_match(matcher, path) if matcher.respond_to? :match matcher.match path elsif matcher.respond_to? :call matcher.call path else File.fnmatch(matcher.to_s, path) end end |
.recursively_enhance(data) ⇒ Thor::CoreExt::HashWithIndifferentAccess
Recursively convert a normal Hash into a HashWithIndifferentAccess
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/middleman-core/util.rb', line 57 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 |