lib/jekyll/utils.rb in jekyll-3.1.0.pre.beta1 vs lib/jekyll/utils.rb in jekyll-3.1.0.pre.rc1
- old
+ new
@@ -1,12 +1,13 @@
module Jekyll
- module Utils extend self
+ module Utils
+ extend self
autoload :Platforms, 'jekyll/utils/platforms'
autoload :Ansi, "jekyll/utils/ansi"
# Constants for use in #slugify
- SLUGIFY_MODES = %w{raw default pretty}
+ SLUGIFY_MODES = %w(raw default pretty)
SLUGIFY_RAW_REGEXP = Regexp.new('\\s+').freeze
SLUGIFY_DEFAULT_REGEXP = Regexp.new('[^[:alnum:]]+').freeze
SLUGIFY_PRETTY_REGEXP = Regexp.new("[^[:alnum:]._~!$&'()+,;=@]+").freeze
def strip_heredoc(str)
@@ -28,27 +29,29 @@
# This code was lovingly stolen from some random gem:
# http://gemjack.com/gems/tartan-0.1.1/classes/Hash.html
#
# Thanks to whoever made it.
def deep_merge_hashes!(target, overwrite)
- overwrite.each_key do |key|
- if (overwrite[key].is_a?(Hash) or overwrite[key].is_a?(Drops::Drop)) and
- (target[key].is_a?(Hash) or target[key].is_a?(Drops::Drop))
- target[key] = Utils.deep_merge_hashes(target[key], overwrite[key])
- next
+ target.merge!(overwrite) do |key, old_val, new_val|
+ if new_val.nil?
+ old_val
+ else
+ mergable?(old_val) && mergable?(new_val) ? deep_merge_hashes(old_val, new_val) : new_val
end
-
- target[key] = overwrite[key]
end
if target.respond_to?(:default_proc) && overwrite.respond_to?(:default_proc) && target.default_proc.nil?
target.default_proc = overwrite.default_proc
end
target
end
+ def mergable?(value)
+ value.is_a?(Hash) || value.is_a?(Drops::Drop)
+ end
+
# 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
@@ -60,11 +63,11 @@
array << (value_from_singular_key(hash, singular_key) || value_from_plural_key(hash, plural_key))
end.flatten.compact
end
def value_from_singular_key(hash, key)
- hash[key] if (hash.key?(key) || (hash.default_proc && hash[key]))
+ hash[key] if hash.key?(key) || (hash.default_proc && hash[key])
end
def value_from_plural_key(hash, key)
if hash.key?(key) || (hash.default_proc && hash[key])
val = hash[key]
@@ -118,11 +121,13 @@
# Determines whether a given file has
#
# Returns true if the YAML front matter is present.
def has_yaml_header?(file)
- !!(File.open(file, 'rb') { |f| f.read(5) } =~ /\A---\r?\n/)
+ !!(File.open(file, 'rb') { |f| f.readline } =~ /\A---\s*\r?\n/)
+ rescue EOFError
+ false
end
# Slugify a filename or title.
#
# string - the filename or title to slugify
@@ -162,20 +167,21 @@
unless SLUGIFY_MODES.include?(mode)
return cased ? string : string.downcase
end
# Replace each character sequence with a hyphen
- re = case mode
- when 'raw'
- SLUGIFY_RAW_REGEXP
- when 'default'
- SLUGIFY_DEFAULT_REGEXP
- when 'pretty'
- # "._~!$&'()+,;=@" is human readable (not URI-escaped) in URL
- # and is allowed in both extN and NTFS.
- SLUGIFY_PRETTY_REGEXP
- end
+ re =
+ case mode
+ when 'raw'
+ SLUGIFY_RAW_REGEXP
+ when 'default'
+ SLUGIFY_DEFAULT_REGEXP
+ when 'pretty'
+ # "._~!$&'()+,;=@" is human readable (not URI-escaped) in URL
+ # and is allowed in both extN and NTFS.
+ SLUGIFY_PRETTY_REGEXP
+ end
# Strip according to the mode
slug = string.gsub(re, '-')
# Remove leading/trailing hyphen
@@ -222,10 +228,9 @@
template << "/" if permalink_style.to_s.end_with?("/")
template << ":output_ext" if permalink_style.to_s.end_with?(":output_ext")
end
template
end
-
# Work the same way as Dir.glob but seperating the input into two parts
# ('dir' + '/' + 'pattern') to make sure the first part('dir') does not act
# as a pattern.
#