Add mode parameter to slugify Liquid filter
This commit is contained in:
parent
25d77cb05a
commit
46e76bde3f
|
@ -50,12 +50,12 @@ module Jekyll
|
||||||
# Slugify a filename or title.
|
# Slugify a filename or title.
|
||||||
#
|
#
|
||||||
# input - The filename or title to slugify.
|
# input - The filename or title to slugify.
|
||||||
|
# mode - how string is slugified
|
||||||
#
|
#
|
||||||
# Returns the given filename or title as a lowercase String, with every
|
# Returns the given filename or title as a lowercase URL String.
|
||||||
# sequence of spaces and non-alphanumeric characters replaced with a
|
# See Utils.slugify for more detail.
|
||||||
# hyphen.
|
def slugify(input, mode=nil)
|
||||||
def slugify(input)
|
Utils.slugify(input, mode)
|
||||||
Utils.slugify(input)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Format a date in short format e.g. "27 Jan 2011".
|
# Format a date in short format e.g. "27 Jan 2011".
|
||||||
|
|
|
@ -104,20 +104,51 @@ module Jekyll
|
||||||
|
|
||||||
# Slugify a filename or title.
|
# Slugify a filename or title.
|
||||||
#
|
#
|
||||||
# name - the filename or title to slugify
|
# string - the filename or title to slugify
|
||||||
|
# mode - how string is slugified
|
||||||
#
|
#
|
||||||
# Returns the given filename or title in lowercase, with every
|
# When mode is "none", return the given string in lowercase.
|
||||||
# sequence of spaces and non-alphanumeric characters replaced with a
|
#
|
||||||
# hyphen.
|
# When mode is "raw", return the given string in lowercase,
|
||||||
def slugify(string)
|
# with every sequence of spaces characters replaced with a hyphen.
|
||||||
unless string.nil?
|
#
|
||||||
# Replace each non-alphanumeric character sequence with a hyphen
|
# When mode is "default" or nil, non-alphabetic characters are
|
||||||
slug = string.gsub(/[^a-z0-9]+/i, '-')
|
# replaced with a hyphen too.
|
||||||
|
#
|
||||||
|
# When mode is "pretty", some non-alphabetic characters (._~!$&'()+,;=@)
|
||||||
|
# are not replaced with hyphen.
|
||||||
|
#
|
||||||
|
# Examples:
|
||||||
|
# slugify("The _config.yml file")
|
||||||
|
# # => "the-config-yml-file"
|
||||||
|
#
|
||||||
|
# slugify("The _config.yml file", "pretty")
|
||||||
|
# # => "the-_config.yml-file"
|
||||||
|
#
|
||||||
|
# Returns the slugified string.
|
||||||
|
def slugify(string, mode=nil)
|
||||||
|
mode ||= 'default'
|
||||||
|
return nil if string.nil?
|
||||||
|
|
||||||
|
# Replace each character sequence with a hyphen
|
||||||
|
re = case mode
|
||||||
|
when 'raw'
|
||||||
|
Regexp.new('\\s+')
|
||||||
|
when 'default'
|
||||||
|
Regexp.new('[^a-zA-Z0-9]+')
|
||||||
|
when 'pretty'
|
||||||
|
# "._~!$&'()+,;=@" is human readable (not URI-escaped) in URL
|
||||||
|
# and is allowed in both extN and NTFS.
|
||||||
|
Regexp.new("[^a-zA-Z0-9._~!$&'()+,;=@]+")
|
||||||
|
else
|
||||||
|
return string.downcase
|
||||||
|
end
|
||||||
|
slug = string.gsub(re, '-')
|
||||||
|
|
||||||
# Remove leading/trailing hyphen
|
# Remove leading/trailing hyphen
|
||||||
slug.gsub!(/^\-|\-$/i, '')
|
slug.gsub!(/^\-|\-$/i, '')
|
||||||
slug.downcase
|
slug.downcase
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue