Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/to_slug.rb

Overview

This reopns the string class

Instance Method Summary collapse

Instance Method Details

#to_slug(options = {}) ⇒ Object

Define a new method to convert the string to be url friendly


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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/to_slug.rb', line 5

def to_slug(options={})
  # This is the input as there are no arguments passed.
  string = self
  
  delimiter = options[:delimiter].nil? ? "-" : options[:delimiter]

  # Define which accents map to which ascii characters
  accents = {
    # Uppercase
    'A'   => %w(À Á Â Ã Ā Ă Ȧ  Ä Å Ǎ Ȁ Ȃ Ą           Ǡ Ǟ Ǻ  ),
    'AE'  => %w(Æ Ǽ Ǣ),
    'B'   => %w( Ɓ   Ƃ Ƅ),
    'C'   => %w(C Ć Ĉ Ċ Č Ƈ Ç ),
    'D'   => %w(D  Ɗ     Ď Đ Ɖ Ƌ),
    'E'   => %w(È É Ê  Ē Ĕ Ė Ë  Ě Ȅ Ȇ  Ȩ Ę           Ǝ Ɛ),
    'F'   => %w( Ƒ),
    'G'   => %w(Ǵ Ĝ  Ğ Ġ Ǧ Ɠ Ģ Ǥ),
    'H'   => %w(Ĥ   Ȟ Ƕ    Ħ),
    'I'   => %w(Ì Í Î Ĩ Ī Ĭ İ Ï  Ǐ  Į Ȉ Ȋ  Ɨ ),
    'IJ'  => %w(IJ),
    'J'   => %w(Ĵ),
    'K'   => %w( Ǩ  Ƙ  Ķ),
    'L'   => %w(Ĺ   Ļ  Ľ Ŀ Ł ),
    'M'   => %w(   Ɯ),
    'N'   => %w(Ǹ Ń Ñ  Ň Ŋ Ɲ  Ņ   Ƞ),
    'O'   => %w(Ò Ó Ô Õ Ō Ŏ Ȯ Ö  Ő Ǒ Ȍ Ȏ Ơ Ǫ  Ɵ Ø     Ȱ Ȫ Ȭ          Ǭ  Ǿ Ɔ),
    'OE'  => %w(Œ),
    'P'   => %w(  Ƥ),
    'R'   => %w(Ŕ  Ř Ȑ Ȓ  Ŗ   Ʀ),
    'S'   => %w(Ś Ŝ  Š  Ș Ş   ),
    'T'   => %w( Ť Ƭ Ʈ  Ț Ţ   Ŧ),
    'U'   => %w(Ù Ú Û Ũ Ū Ŭ Ü  Ů Ű Ǔ Ȕ Ȗ Ư   Ų     Ǜ Ǘ Ǖ Ǚ     ),
    'V'   => %w(  Ʋ),
    'W'   => %w(  Ŵ   ),
    'X'   => %w( ),
    'Y'   => %w( Ý Ŷ  Ȳ  Ÿ  Ƴ ),
    'Z'   => %w(Ź  Ż Ž Ȥ   Ƶ),

    # Lowercase
    'a'   => %w(à á â ã ā ă ȧ ä  å ǎ ȁ ȃ ą            ǡ ǟ ǻ  ),
    'ae'  => %w(æ ǽ ǣ),
    'b'   => %w( ɓ   ƀ ƃ ƅ),
    'c'   => %w(ć ĉ ċ č ƈ ç ),
    'd'   => %w( ɗ     ď đ ƌ ȡ),
    'e'   => %w(è é ê  ē ĕ ė ë  ě ȅ ȇ  ȩ ę    ế       ɛ),
    'f'   => %w( ƒ),
    'g'   => %w(ǵ ĝ  ğ ġ ǧ ɠ ģ ǥ),
    'h'   => %w(ĥ   ȟ ƕ    ħ),
    'i'   => %w(ì í î ĩ ī ĭ ı ï  ǐ į į ȋ  ɨ ),
    'ij'  => %w(ij),
    'j'   => %w(ĵ ǰ),
    'k'   => %w( ǩ ƙ  ķ),
    'l'   => %w(ĺ   ļ  ľ ŀ ł ƚ  ȴ),
    'm'   => %w(ḿ   ɯ),
    'n'   => %w(ǹ ń ñ  ň ŋ ɲ  ņ   ʼn ȵ),
    'o'   => %w(ò ó ô õ ō ŏ ȯ ö  ő ǒ ȍ ȏ ơ ǫ  ɵ ø     ȱ ȫ ȭ          ǭ  ǿ ɔ),
    'oe'  => %w(œ),
    'p'   => %w(  ƥ),
    'r'   => %w(ŕ  ř ȑ ȓ  ŗ  ),
    's'   => %w(ś ŝ  š  ș ş    ß ſ ),
    't'   => %w(  ť ƭ ʈ ƫ  ț ţ   ŧ ȶ),
    'u'   => %w(ù ú û ũ ū ŭ ü  ů ű ǔ ȕ ȗ ư   ų     ǖ ǜ ǘ ǖ ǚ     ),
    'v'   => %w( ṿ),
    'w'   => %w(  ŵ    ),
    'x'   => %w( ),
    'y'   => %w( ý ŷ  ȳ  ÿ   ƴ ),
    'z'   => %w(ź  ż ž ȥ   ƶ),
    
    # Not sure what to do with these
    ''    => %w(Ð Þ Ə Ɣ Ɩ Ƣ Ƨ Ʃ Ʊ Ʒ Ǯ Ƹ Ȝ ƿ Ȣ ð þ ə ɣ ɩ ƣ ƨ ʃ ƪ ʊ ʒ ǯ ƹ ƺ ȝ Ƿ ȣ DZ Dz dz DŽ Dž dž LJ Lj lj NJ Nj nj ĸ ƍ ƛ ƾ ƻ Ƽ ƽ)
  }


  # Replace each accent in the string
  #accents.each do |replacement, accent|
  #  accent.each do |character|
  #    string = string.gsub(character, replacement)
  #  end
  #end

  accents.each do |replacement, accent|
    regex = Regexp.new("[#{accent.join("|")}]")
    string = string.gsub(regex, replacement)
  end
  
  # Strip any HTML decimal/hexadecimal entites
  string = string.gsub(
    /           # begin matching a string
      &         # an ampersand
      [^ ]     # Match any character except whitespace -- enables matching of hex or decimal
      [0-9A-F]  # a hex digit
      {1,4}     # 1 to 4 of them
      ;         # a semicolon
    /xi,        # / = end matching; x = allow spaces/comments; i = ignore case; , = end argument
    ''          # replace matches with nothing (remove matches)
  )

  # Convert underscores and periods to dashs
  string = string.gsub(/[_|.]/,"-")

  # Remove any characters that aren't alphanumeric (or a dash)
  string = string.gsub(/[^a-zA-Z0-9 \-]/,"")

  # Convert multiple spaces to a single space
  string = string.gsub(/[ ]+/," ")

  # Give a little trim around the edges (leading/trailing whitespace)
  string.strip!

  # Replace single spaces with a URL friendly dash (-)
  string = string.gsub(/ /,"#{delimiter}")

  # Do a greedy replace on multiple dashes
  string = string.gsub(/-+/,"-")

  # CASE. EVERYTHING. DOWN. (and return since it's the last line)
  string = string.downcase

end