-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslugalizer.rb
More file actions
45 lines (40 loc) · 1.08 KB
/
slugalizer.rb
File metadata and controls
45 lines (40 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Slugalizer
# http://github.com/henrik/slugalizer
class String
def normalize(normalization_form=nil)
self
end
end
class Integer
def ordinalize
if (10...20) === self
"#{self}th"
else
g = %w{ th st nd rd th th th th th th }
a = self.to_s
c=a[-1..-1].to_i
a + g[c]
end
end
end
def returning(value)
yield(value)
value
end
module Slugalizer
extend self
SEPARATORS = %w[- _ +]
def slugalize(text, separator = "-")
unless SEPARATORS.include?(separator)
raise "Word separator must be one of #{SEPARATORS}"
end
re_separator = Regexp.escape(separator)
result = text.to_s.normalize
result.gsub!(/[^\x00-\x7F]+/, '') # Remove non-ASCII (e.g. diacritics).
result.gsub!(/[^a-z0-9\-_\+]+/i, separator) # Turn non-slug chars into the separator.
result.gsub!(/#{re_separator}{2,}/, separator) # No more than one of the separator in a row.
result.gsub!(/^#{re_separator}|#{re_separator}$/, '') # Remove leading/trailing separator.
result.downcase!
result
end
end