ruby-slugify 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5354609d5f05b94f8ad516d552b55640505751562e3aac60c43af274d823dacb
4
+ data.tar.gz: 56e4f89879eff6d902d5a469bdafde8382fc4e52a722c07ca96bfe788b080275
5
+ SHA512:
6
+ metadata.gz: eccd017ef1bca3fbc0845d7d7f8b71e0451d8d0bc169a8e22366d4d8e8fa371081e257862dfe6c1d6e4810785f5a8db411d26974f43eee0afd8b0a8f2c02b4e8
7
+ data.tar.gz: 8fbcb453f631e5498b71c2b94b4efc5bdad1973c9c95b814a2642d8c2f18811cb1461c5d092a7240660cb94754dbd30664745e4038571ce9c9bea262f8763c10
@@ -0,0 +1,94 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $defaultMap = {
4
+ '"' => "",
5
+ '\'' => "",
6
+ '’' => "",
7
+ '‒' => "-",
8
+ '–' => "-",
9
+ '—' => "-",
10
+ '―' => "-",
11
+ }
12
+
13
+ $deutscheMap = {
14
+ '&' => "und",
15
+ '@' => "an",
16
+ 'ä' => "ae",
17
+ 'Ä' => "Ae",
18
+ 'ö' => "oe",
19
+ 'Ö' => "Oe",
20
+ 'ü' => "ue",
21
+ 'Ü' => "Ue",
22
+ }
23
+
24
+ $englishMap = {
25
+ '&' => "and",
26
+ '@' => "at",
27
+ }
28
+
29
+ $spanishMap = {
30
+ '&' => "y",
31
+ '@' => "en",
32
+ }
33
+
34
+ $finnishMap = {
35
+ '&' => "ja",
36
+ '@' => "at",
37
+ }
38
+
39
+ $greekMap = {
40
+ '&' => "kai",
41
+ 'η' => "i",
42
+ 'ή' => "i",
43
+ 'Η' => "i",
44
+ 'ι' => "i",
45
+ 'ί' => "i",
46
+ 'ϊ' => "i",
47
+ 'Ι' => "i",
48
+ 'χ' => "x",
49
+ 'Χ' => "x",
50
+ 'ω' => "w",
51
+ 'ώ' => "w",
52
+ 'Ω' => "w",
53
+ 'ϋ' => "u",
54
+ }
55
+
56
+ $dutchMap = {
57
+ '&' => "en",
58
+ '@' => "at",
59
+ }
60
+
61
+ $polishMap = {
62
+ '&' => "i",
63
+ '@' => "na",
64
+ }
65
+
66
+ $turkishMap = {
67
+ '&' => "ve",
68
+ '@' => "et",
69
+ 'ş' => "s",
70
+ 'Ş' => "S",
71
+ 'ü' => "u",
72
+ 'Ü' => "U",
73
+ 'ö' => "o",
74
+ 'Ö' => "O",
75
+ 'İ' => "I",
76
+ 'ı' => "i",
77
+ 'ğ' => "g",
78
+ 'Ğ' => "G",
79
+ 'ç' => "c",
80
+ 'Ç' => "C",
81
+ }
82
+
83
+ $referenceTable = {
84
+ "en" => $englishMap,
85
+ "es" => $spanishMap,
86
+ "de" => $deutscheMap,
87
+ "fi" => $finnishMap,
88
+ "nl" => $dutchMap,
89
+ "po" => $polishMap,
90
+ "tr" => $turkishMap,
91
+ "gr" => $greekMap,
92
+ "el" => $greekMap,
93
+ "ell" => $greekMap
94
+ }
@@ -0,0 +1,149 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.dirname(__FILE__) + "/language-support"
4
+ require 'stringex/unidecoder'
5
+
6
+ class RubySlugify
7
+
8
+ @inputString = nil
9
+ @language = "en"
10
+ @maxLength = 100
11
+ @toLower = true
12
+ @customMap = nil
13
+ @slugString = nil
14
+
15
+ # Initializes the object with all necessary instance variables
16
+ # Parameters :
17
+ # 1. inputString - The string to be slugified
18
+ # 2. language - If any language translations are required. Available options : ["en", "es", "de", "nl", "gr", "el", "ell", "po", "fi"]. Default is 'en' or english
19
+ # 3, customMap - If any custom mappings are required i.e custom substituions for words in the slugString
20
+ # 4. maxLength - Limits the size of the slugified string. Default is 100 characters
21
+ # 5. toLower - Whether the slugified string needs to be lower cased. Default is true, i.e it is lower cased.
22
+ def initialize(inputString, language = "en", customMap = nil, maxLength = 100, toLower = true)
23
+ # Initialise all the instance variables
24
+ @inputString = inputString
25
+ @maxLength = maxLength
26
+ @toLower = toLower
27
+ @customMap = customMap
28
+
29
+ # If the language is not supported use default as english
30
+ if $referenceTable.key?(language)
31
+ @language = language
32
+ else
33
+ @language = "en"
34
+ end
35
+ end
36
+
37
+ # Creates the slugified string from the given input string
38
+ # Returns : slugified string
39
+ def createSlug()
40
+ # Strip leading and trailing spaces
41
+ @slugString = @inputString.strip()
42
+
43
+ # If custom mapping is given, first substitute these key, value pairs
44
+ unless @customMap.nil?
45
+ substituteCustom(@customMap)
46
+ end
47
+
48
+ # Substitute all the common characters present in the default map
49
+ substituteChars($defaultMap)
50
+
51
+ # Substitute all the characters present in the given language
52
+ substituteChars($referenceTable[@language])
53
+
54
+ # Process all non ASCII characters and convert them to nearest ASCII
55
+ @slugString = Stringex::Unidecoder.decode(@slugString)
56
+
57
+ # If required in lowercase, convert it. Always coverts to lowercase unless specified otherwise
58
+ unless @toLower == false
59
+ @slugString = @slugString.downcase
60
+ end
61
+
62
+ # Any character other than a-z A-Z 0-9 _ and - will be replaced with -
63
+ @slugString = @slugString.gsub(/[^a-zA-Z0-9\-\_]/, '-')
64
+
65
+ # Replace multiple '-'s with a single '-' and multiple '_'s with a single '_'
66
+ @slugString = @slugString.gsub(/-+/, '-')
67
+ @slugString = @slugString.gsub(/_+/, '_')
68
+
69
+ # Remove any leading and trailing '-'s and '_'s
70
+ @slugString = @slugString.delete_prefix('-')
71
+ @slugString = @slugString.delete_prefix('_')
72
+ @slugString = @slugString.delete_suffix('-')
73
+ @slugString = @slugString.delete_suffix('_')
74
+
75
+ # If the size of slug string is greater than the maxLength, trim it
76
+ if @slugString.length > @maxLength
77
+ @slugString = trimString(@slugString, @maxLength)
78
+ end
79
+
80
+ @slugString
81
+ end
82
+
83
+ # Substitutes the characters in slugString with their mapped values
84
+ # Parameters :
85
+ # 1. mapping - The hashmap that contains the one to one mapping of characters to be replaced
86
+ def substituteChars(mapping)
87
+ len = @slugString.length - 1
88
+ for i in 0..len
89
+ if mapping.key?(@slugString[i])
90
+ @slugString[i] = mapping[@slugString[i]]
91
+ end
92
+ end
93
+ end
94
+
95
+ # Substitutes the patterns in slugString with their mapped values given by the user
96
+ # Parameters :
97
+ # 1. mapping - The hashmap that contains the one to one mapping of character runs to be replaced
98
+ def substituteCustom(mapping)
99
+ mapping.each do |key, value|
100
+ find = key.to_s
101
+ replace = value.to_s
102
+ @slugString = @slugString.gsub(find, replace)
103
+ end
104
+ end
105
+
106
+ # Truncates a string if it is longer than a given length
107
+ # Parameters :
108
+ # 1. longString - the string that is to be truncated
109
+ # 2. mlen - maximum length of the string
110
+ # Returns :
111
+ # 1. Truncated string
112
+ def trimString(longString, mlen)
113
+
114
+ # Split at '-' to obtain all the words in the slug
115
+ splitString = longString.split('-')
116
+
117
+ # If the first word is longer than maxLength, return the slice as the trimmed string
118
+ if splitString[0].length > mlen
119
+ return splitString[0][0..mlen-1]
120
+ end
121
+
122
+ # Ensure the truncated version is below maxLength but has whole words in the slug
123
+ trimmedString = ''
124
+ for string in splitString
125
+ if string.length + trimmedString.length + 1 <= mlen
126
+ trimmedString = trimmedString + '-' + string
127
+ else
128
+ break
129
+ end
130
+ end
131
+
132
+ # Remove leading '-' due to concatenation
133
+ trimmedString = trimmedString.delete_prefix('-')
134
+ return trimmedString
135
+ end
136
+
137
+ # Returns true if the given string is a slug. Can be invoked without an object.
138
+ # Parameters :
139
+ # 1. inputString - The string that is to be checked
140
+ # Returns :
141
+ # 1. Boolean value (true / false) - If the string is a slug or not
142
+ def self.isSlug(inputString)
143
+ if inputString == "" || inputString.length > @maxLength || inputString[0] == '-' || inputString[0] == '_' || inputString[inputString.length - 1] == '-' || inputString[inputString.length - 1] == '_'
144
+ return false
145
+ else
146
+ return inputString.match(/[^a-zA-Z0-9\-\_]/).nil?
147
+ end
148
+ end
149
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-slugify
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Aditi Srinivas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-08-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: stringex
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - aditisrinivas97@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/language-support.rb
35
+ - lib/ruby-slugify.rb
36
+ homepage:
37
+ licenses: []
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.0.3
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: URL-friendly slugify in Ruby
58
+ test_files: []