Use frozen regular expressions for Utils#slugify
This commit is contained in:
parent
ef2d558874
commit
aaf0ba15cc
|
@ -2,6 +2,12 @@ module Jekyll
|
||||||
module Utils
|
module Utils
|
||||||
extend self
|
extend self
|
||||||
|
|
||||||
|
# Constants for use in #slugify
|
||||||
|
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
|
||||||
|
|
||||||
# Merges a master hash with another hash, recursively.
|
# Merges a master hash with another hash, recursively.
|
||||||
#
|
#
|
||||||
# master_hash - the "parent" hash whose values will be overridden
|
# master_hash - the "parent" hash whose values will be overridden
|
||||||
|
@ -129,19 +135,18 @@ module Jekyll
|
||||||
def slugify(string, mode=nil)
|
def slugify(string, mode=nil)
|
||||||
mode ||= 'default'
|
mode ||= 'default'
|
||||||
return nil if string.nil?
|
return nil if string.nil?
|
||||||
|
return string.downcase unless SLUGIFY_MODES.include?(mode)
|
||||||
|
|
||||||
# Replace each character sequence with a hyphen
|
# Replace each character sequence with a hyphen
|
||||||
re = case mode
|
re = case mode
|
||||||
when 'raw'
|
when 'raw'
|
||||||
Regexp.new('\\s+')
|
SLUGIFY_RAW_REGEXP
|
||||||
when 'default'
|
when 'default'
|
||||||
Regexp.new('[^[:alnum:]]+')
|
SLUGIFY_DEFAULT_REGEXP
|
||||||
when 'pretty'
|
when 'pretty'
|
||||||
# "._~!$&'()+,;=@" is human readable (not URI-escaped) in URL
|
# "._~!$&'()+,;=@" is human readable (not URI-escaped) in URL
|
||||||
# and is allowed in both extN and NTFS.
|
# and is allowed in both extN and NTFS.
|
||||||
Regexp.new("[^a-zA-Z0-9._~!$&'()+,;=@]+")
|
SLUGIFY_PRETTY_REGEXP
|
||||||
else
|
|
||||||
return string.downcase
|
|
||||||
end
|
end
|
||||||
|
|
||||||
string.
|
string.
|
||||||
|
|
Loading…
Reference in New Issue