Module: Jekyll::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/jekyll/utils.rb

Instance Method Summary collapse

Instance Method Details

#deep_merge_hashes(master_hash, other_hash) ⇒ Object

Merges a master hash with another hash, recursively.

master_hash - the “parent” hash whose values will be overridden other_hash - the other hash whose values will be persisted after the merge

This code was lovingly stolen from some random gem: gemjack.com/gems/tartan-0.1.1/classes/Hash.html

Thanks to whoever made it.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/jekyll/utils.rb', line 14

def deep_merge_hashes(master_hash, other_hash)
  target = master_hash.dup

  other_hash.each_key do |key|
    if other_hash[key].is_a? Hash and target[key].is_a? Hash
      target[key] = Utils.deep_merge_hashes(target[key], other_hash[key])
      next
    end

    target[key] = other_hash[key]
  end

  target
end

#has_yaml_header?(file) ⇒ Boolean

Determines whether a given file has

Returns true if the YAML front matter is present.

Returns:

  • (Boolean)


101
102
103
# File 'lib/jekyll/utils.rb', line 101

def has_yaml_header?(file)
  !!(File.open(file, 'rb') { |f| f.read(5) } =~ /\A---\r?\n/)
end

#parse_date(input, msg = "Input could not be parsed.") ⇒ Object

Parse a date/time and throw an error if invalid

input - the date/time to parse msg - (optional) the error message to show the user

Returns the parsed date if successful, throws a FatalException if not



92
93
94
95
96
# File 'lib/jekyll/utils.rb', line 92

def parse_date(input, msg = "Input could not be parsed.")
  Time.parse(input)
rescue ArgumentError
  raise Errors::FatalException.new("Invalid date '#{input}': " + msg)
end

#pluralized_array_from_hash(hash, singular_key, plural_key) ⇒ Object

Read array from the supplied hash favouring the singular key and then the plural key, and handling any nil entries.

hash - the hash to read from singular_key - the singular key plural_key - the plural key

Returns an array



37
38
39
40
41
# File 'lib/jekyll/utils.rb', line 37

def pluralized_array_from_hash(hash, singular_key, plural_key)
  [].tap do |array|
    array << (value_from_singular_key(hash, singular_key) || value_from_plural_key(hash, plural_key))
  end.flatten.compact
end

#slugify(string) ⇒ Object

Slugify a filename or title.

name - the filename or title to slugify

Returns the given filename or title in lowercase, with every sequence of spaces and non-alphanumeric characters replaced with a hyphen.



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/jekyll/utils.rb', line 112

def slugify(string)
  unless string.nil?
    string \
      # Replace each non-alphanumeric character sequence with a hyphen
      .gsub(/[^a-z0-9]+/i, '-') \
      # Remove leading/trailing hyphen
      .gsub(/^\-|\-$/i, '') \
      # Downcase it
      .downcase
  end
end

#stringify_hash_keys(hash) ⇒ Object

Apply #to_s to all keys in the Hash

hash - the hash to which to apply this transformation

Returns a new hash with stringified keys



81
82
83
# File 'lib/jekyll/utils.rb', line 81

def stringify_hash_keys(hash)
  transform_keys(hash) { |key| key.to_s rescue key }
end

#symbolize_hash_keys(hash) ⇒ Object

Apply #to_sym to all keys in the hash

hash - the hash to which to apply this transformation

Returns a new hash with symbolized keys



72
73
74
# File 'lib/jekyll/utils.rb', line 72

def symbolize_hash_keys(hash)
  transform_keys(hash) { |key| key.to_sym rescue key }
end

#transform_keys(hash) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/jekyll/utils.rb', line 59

def transform_keys(hash)
  result = {}
  hash.each_key do |key|
    result[yield(key)] = hash[key]
  end
  result
end

#value_from_plural_key(hash, key) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jekyll/utils.rb', line 47

def value_from_plural_key(hash, key)
  if hash.key?(key) || (hash.default_proc && hash[key])
    val = hash[key]
    case val
    when String
      val.split
    when Array
      val.compact
    end
  end
end

#value_from_singular_key(hash, key) ⇒ Object



43
44
45
# File 'lib/jekyll/utils.rb', line 43

def value_from_singular_key(hash, key)
  hash[key] if (hash.key?(key) || (hash.default_proc && hash[key]))
end