mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 16:11:08 +08:00
Set up slug.rb for obvious method extractions.
This commit is contained in:
16
lib/slug.rb
16
lib/slug.rb
@ -6,20 +6,24 @@
|
||||
module Slug
|
||||
|
||||
def self.for(string)
|
||||
|
||||
str = string.dup
|
||||
str.gsub!(/^\s+|\s+$/, '')
|
||||
str.downcase!
|
||||
|
||||
str = string.dup.strip.downcase
|
||||
|
||||
# The characters we want to replace with a hyphen
|
||||
str.tr!("·/_,:;.", "\-")
|
||||
|
||||
# Convert to ASCII or remove if transliteration is unknown.
|
||||
str = ActiveSupport::Inflector.transliterate(str, '')
|
||||
|
||||
|
||||
# Remove everything except alphanumberic, space, and hyphen characters.
|
||||
str.gsub!(/[^a-z0-9 -]/, '')
|
||||
|
||||
# Replace multiple spaces with one hyphen.
|
||||
str.gsub!(/\s+/, '-')
|
||||
|
||||
# Replace multiple hyphens with one hyphen.
|
||||
str.gsub!(/\-+/, '-')
|
||||
|
||||
# Remove leading and trailing hyphens
|
||||
str.gsub!(/^-|-$/, '')
|
||||
|
||||
str
|
||||
|
Reference in New Issue
Block a user