discourse/lib/slug.rb

33 lines
845 B
Ruby
Raw Normal View History

2013-02-05 14:16:51 -05:00
# encoding: utf-8
# Generates a slug. This is annoying beacuse it's duplicating what the javascript
# does, but on the other hand slugs are never matched so it's okay if they differ
# a little.
module Slug
def self.for(string)
str = string.dup.strip.downcase
2013-02-11 21:34:38 -05:00
# 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.
2013-02-05 14:16:51 -05:00
str.gsub!(/[^a-z0-9 -]/, '')
# Replace multiple spaces with one hyphen.
2013-02-05 14:16:51 -05:00
str.gsub!(/\s+/, '-')
# Replace multiple hyphens with one hyphen.
2013-02-05 14:16:51 -05:00
str.gsub!(/\-+/, '-')
# Remove leading and trailing hyphens
str.gsub!(/^-|-$/, '')
2013-02-05 14:16:51 -05:00
str
end
end