Module: Middleman::Util

Defined in:
middleman-core/lib/middleman-core/util.rb

Defined Under Namespace

Classes: Cache

Class Method Summary (collapse)

Class Method Details

+ (String) extract_response_text(response)

Extract the text of a Rack response as a string. Useful for extensions implemented as Rack middleware.

Parameters:

  • response

    The response from #call

Returns:

  • (String)

    The whole response as a string.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'middleman-core/lib/middleman-core/util.rb', line 40

def self.extract_response_text(response)
  case(response)
  when String
    response
  when Array
    response.join
  when Rack::Response
    response.body.join
  when Rack::File
    File.read(response.path)
  else
    response.to_s
  end
end

+ (String) normalize_path(path)

Normalize a path to not include a leading slash

Parameters:

  • path (String)

Returns:

  • (String)


31
32
33
34
# File 'middleman-core/lib/middleman-core/util.rb', line 31

def self.normalize_path(path)
  # The tr call works around a bug in Ruby's Unicode handling
  path.sub(/^\//, "").tr('','') 
end

+ (Thor::CoreExt::HashWithIndifferentAccess) recursively_enhance(data)

Recursively convert a normal Hash into a HashWithIndifferentAccess

Parameters:

  • data (Hash)

    Normal hash

Returns:

  • (Thor::CoreExt::HashWithIndifferentAccess)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'middleman-core/lib/middleman-core/util.rb', line 11

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