Module: Fronde::Slug

Defined in:
lib/fronde/slug.rb

Overview

Contains method to generate URL compatible strings

Class Method Summary collapse

Class Method Details

.slug(title) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/fronde/slug.rb', line 7

def slug(title)
  title.downcase
       .encode('ascii', fallback: ->(k) { translit(k) })
       .encode('utf-8') # Convert back to utf-8 string
       .gsub(/[^\w-]/, '-')
       .squeeze('-')
       .delete_suffix('-')
end

.translit(char) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength



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
# File 'lib/fronde/slug.rb', line 18

def translit(char)
  case char
  when 'á', 'à', 'â', 'ä', 'ǎ', 'ã', 'å'
    'a'
  when 'é', 'è', 'ê', 'ë', 'ě', '', ''
    'e'
  when 'í', 'ì', 'î', 'ï', 'ǐ', 'ĩ'
    'i'
  when 'ó', 'ò', 'ô', 'ö', 'ǒ', 'õ', 'ø'
    'o'
  when 'ú', 'ù', 'û', 'ü', 'ǔ', 'ũ'
    'u'
  when 'ý', '', 'ŷ', 'ÿ', ''
    'y'
  when 'ç', '©', '🄯'
    'c'
  when 'ñ'
    'n'
  when 'ß'
    'ss'
  when 'œ'
    'oe'
  when 'æ'
    'ae'
  when '®'
    'r'
  when ''
    'tm'
  else
    '-'
  end
end