Module: Jekyll::Utils

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

Instance Method Summary collapse

Instance Method Details

#recursive_symbolize_hash_keys(hash) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/jekyll/patches/utils.rb', line 29

def recursive_symbolize_hash_keys(hash)
  result = {}
  hash.each do |key, value|
    new_key = key.to_s.to_sym
    result[new_key] = value.is_a?(Hash) ? recursive_symbolize_hash_keys(value) : value
  end
  result
end

#slugify(string, mode: nil, cased: false, replacement: "-") ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jekyll/patches/utils.rb', line 38

def slugify(string, mode: nil, cased: false, replacement: "-")
  mode ||= "default"
  return nil if string.nil?

  unless SLUGIFY_MODES.include?(mode)
    return cased ? string : string.downcase
  end

  # Drop accent marks from latin characters. Everything else turns to ?
  if mode == "latin"
    I18n.config.available_locales = :en if I18n.config.available_locales.empty?
    string = I18n.transliterate(string)
  end

  slug = replace_character_sequence(string, :mode => mode, :replacement => replacement)

  # Remove leading/trailing hyphen
  slug.gsub!(%r!^\-|\-$!i, "")

  slug.downcase! unless cased
  slug
end

#snake_case_keys(hash) ⇒ Object

Convert all keys to snake_case by substituting non-alphanumeric characters with an underscore. The keys are first converted to lowercase strings and then any letters with accents are replaced with their plaintext counterpart

hash - the hash to which to apply this transformation

Returns a new hash with snake_cased keys or a hash with stringified keys.



15
16
17
18
19
20
21
22
23
# File 'lib/jekyll/patches/utils.rb', line 15

def snake_case_keys(hash)
  transform_keys(hash) do |key|
    begin
      snakeify(key)
    rescue StandardError
      key.to_s
    end
  end
end

#snakeify(input) ⇒ Object



25
26
27
# File 'lib/jekyll/patches/utils.rb', line 25

def snakeify(input)
  slugify(input.to_s, :mode => "latin", :replacement => "_")
end