2013-02-06 03:16:51 +08:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
module Slug
|
|
|
|
|
2016-10-13 03:44:26 +08:00
|
|
|
CHAR_FILTER_REGEXP = /[:\/\?#\[\]@!\$&'\(\)\*\+,;=_\.~%\\`^\s|\{\}"<>]+/ # :/?#[]@!$&'()*+,;=_.~%\`^|{}"<>
|
2017-10-03 18:52:24 +08:00
|
|
|
MAX_LENGTH = 255
|
2016-10-13 03:44:26 +08:00
|
|
|
|
2017-10-03 18:52:24 +08:00
|
|
|
def self.for(string, default = 'topic', max_length = MAX_LENGTH)
|
2017-07-28 09:20:09 +08:00
|
|
|
slug =
|
|
|
|
case (SiteSetting.slug_generation_method || :ascii).to_sym
|
|
|
|
when :ascii then self.ascii_generator(string)
|
|
|
|
when :encoded then self.encoded_generator(string)
|
|
|
|
when :none then self.none_generator(string)
|
|
|
|
end
|
2015-04-13 22:50:41 +08:00
|
|
|
# Reject slugs that only contain numbers, because they would be indistinguishable from id's.
|
|
|
|
slug = (slug =~ /[^\d]/ ? slug : '')
|
2017-10-03 18:52:24 +08:00
|
|
|
slug = self.prettify_slug(slug, max_length: max_length)
|
2015-04-13 22:50:41 +08:00
|
|
|
slug.blank? ? default : slug
|
|
|
|
end
|
|
|
|
|
2017-10-03 18:52:24 +08:00
|
|
|
def self.sanitize(string, downcase: false, max_length: MAX_LENGTH)
|
|
|
|
slug = self.encoded_generator(string, downcase: downcase)
|
|
|
|
self.prettify_slug(slug, max_length: max_length)
|
2015-05-13 16:52:48 +08:00
|
|
|
end
|
|
|
|
|
2015-04-13 22:50:41 +08:00
|
|
|
private
|
|
|
|
|
2017-10-03 18:52:24 +08:00
|
|
|
def self.prettify_slug(slug, max_length: MAX_LENGTH)
|
|
|
|
slug.tr!("_", "-")
|
|
|
|
slug = slug.squeeze('-') # squeeze continuous dashes to prettify slug
|
|
|
|
slug.gsub!(/\A-+|-+\z/, '') # remove possible trailing and preceding dashes
|
|
|
|
slug.truncate(max_length, omission: '')
|
|
|
|
end
|
|
|
|
|
2015-04-13 22:50:41 +08:00
|
|
|
def self.ascii_generator(string)
|
2017-10-03 18:52:24 +08:00
|
|
|
string.tr!("'", "")
|
|
|
|
string.parameterize
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2017-10-03 18:52:24 +08:00
|
|
|
def self.encoded_generator(string, downcase: true)
|
2015-05-04 19:48:37 +08:00
|
|
|
# This generator will sanitize almost all special characters,
|
|
|
|
# including reserved characters from RFC3986.
|
|
|
|
# See also URI::REGEXP::PATTERN.
|
2017-10-03 18:52:24 +08:00
|
|
|
string.strip!
|
|
|
|
string.gsub!(/\s+/, '-')
|
|
|
|
string.gsub!(CHAR_FILTER_REGEXP, '')
|
|
|
|
string.downcase! if downcase
|
|
|
|
string
|
2015-04-13 22:50:41 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.none_generator(string)
|
|
|
|
''
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|