mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 04:31:10 +08:00
FEATURE: add slug geneartion options
This commit is contained in:

committed by
fantasticfears

parent
2344aa2fdd
commit
b772ff6e13
41
lib/slug.rb
41
lib/slug.rb
@ -1,21 +1,34 @@
|
||||
# encoding: utf-8
|
||||
|
||||
|
||||
module Slug
|
||||
|
||||
def self.for(string)
|
||||
# TODO review if ja should use this
|
||||
# ko asked for it to be removed
|
||||
if ['zh_CN', 'ja'].include?(SiteSetting.default_locale)
|
||||
unless defined? Stringex
|
||||
require 'stringex_lite'
|
||||
end
|
||||
slug = string.to_url
|
||||
else
|
||||
slug = string.gsub("'", "").parameterize
|
||||
slug.gsub!("_", "-")
|
||||
end
|
||||
slug =~ /[^\d]/ ? slug : '' # Reject slugs that only contain numbers, because they would be indistinguishable from id's.
|
||||
def self.for(string, default = 'topic')
|
||||
slug = case SiteSetting.slug_generation_method.to_sym
|
||||
when :ascii then self.ascii_generator(string)
|
||||
when :encoded then self.encoded_generator(string)
|
||||
when :none then self.none_generator(string)
|
||||
else raise Discourse::SiteSettingMissing
|
||||
end
|
||||
# Reject slugs that only contain numbers, because they would be indistinguishable from id's.
|
||||
slug = (slug =~ /[^\d]/ ? slug : '')
|
||||
slug.blank? ? default : slug
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.ascii_generator(string)
|
||||
slug = string.gsub("'", "").parameterize
|
||||
slug.gsub("_", "-")
|
||||
end
|
||||
|
||||
def self.encoded_generator(string)
|
||||
# strip and sanitized RFC 3986 reserved character and blank
|
||||
string = string.strip.gsub(/\s+/, '-').gsub(/[:\/\?#\[\]@!\$\s&'\(\)\*\+,;=]+/, '')
|
||||
string = Rack::Utils.escape_path(string)
|
||||
string =~ /^-+$/ ? '' : string
|
||||
end
|
||||
|
||||
def self.none_generator(string)
|
||||
''
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user