Class: RubySlugify
- Inherits:
-
Object
- Object
- RubySlugify
- Defined in:
- lib/ruby-slugify.rb
Class Method Summary collapse
-
.isSlug(inputString) ⇒ Object
Returns true if the given string is a slug.
Instance Method Summary collapse
-
#createSlug ⇒ Object
Creates the slugified string from the given input string Returns : slugified string.
-
#initialize(inputString, language = "en", customMap = nil, maxLength = 100, toLower = true) ⇒ RubySlugify
constructor
Initializes the object with all necessary instance variables Parameters : 1.
-
#substituteChars(mapping) ⇒ Object
Substitutes the characters in slugString with their mapped values Parameters : 1.
-
#substituteCustom(mapping) ⇒ Object
Substitutes the patterns in slugString with their mapped values given by the user Parameters : 1.
-
#trimString(longString, mlen) ⇒ Object
Truncates a string if it is longer than a given length Parameters : 1.
Constructor Details
#initialize(inputString, language = "en", customMap = nil, maxLength = 100, toLower = true) ⇒ RubySlugify
Initializes the object with all necessary instance variables Parameters :
1. inputString - The string to be slugified
2. language - If any language translations are required. Available options : ["en", "es", "de", "nl", "gr", "el", "ell", "po", "fi"]. Default is 'en' or english
3, customMap - If any custom mappings are required i.e custom substituions for words in the slugString
4. maxLength - Limits the size of the slugified string. Default is 100 characters
5. toLower - Whether the slugified string needs to be lower cased. Default is true, i.e it is lower cased.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ruby-slugify.rb', line 22 def initialize(inputString, language = "en", customMap = nil, maxLength = 100, toLower = true) # Initialise all the instance variables @inputString = inputString @maxLength = maxLength @toLower = toLower @customMap = customMap # If the language is not supported use default as english if $referenceTable.key?(language) @language = language else @language = "en" end end |
Class Method Details
.isSlug(inputString) ⇒ Object
Returns true if the given string is a slug. Can be invoked without an object. Parameters :
1. inputString - The string that is to be checked
Returns :
1. Boolean value (true / false) - If the string is a slug or not
142 143 144 145 146 147 148 |
# File 'lib/ruby-slugify.rb', line 142 def self.isSlug(inputString) if inputString == "" || inputString.length > @maxLength || inputString[0] == '-' || inputString[0] == '_' || inputString[inputString.length - 1] == '-' || inputString[inputString.length - 1] == '_' return false else return inputString.match(/[^a-zA-Z0-9\-\_]/).nil? end end |
Instance Method Details
#createSlug ⇒ Object
Creates the slugified string from the given input string Returns : slugified string
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 |
# File 'lib/ruby-slugify.rb', line 39 def createSlug() # Strip leading and trailing spaces @slugString = @inputString.strip() # If custom mapping is given, first substitute these key, value pairs unless @customMap.nil? substituteCustom(@customMap) end # Substitute all the common characters present in the default map substituteChars($defaultMap) # Substitute all the characters present in the given language substituteChars($referenceTable[@language]) # Process all non ASCII characters and convert them to nearest ASCII @slugString = Stringex::Unidecoder.decode(@slugString) # If required in lowercase, convert it. Always coverts to lowercase unless specified otherwise unless @toLower == false @slugString = @slugString.downcase end # Any character other than a-z A-Z 0-9 _ and - will be replaced with - @slugString = @slugString.gsub(/[^a-zA-Z0-9\-\_]/, '-') # Replace multiple '-'s with a single '-' and multiple '_'s with a single '_' @slugString = @slugString.gsub(/-+/, '-') @slugString = @slugString.gsub(/_+/, '_') # Remove any leading and trailing '-'s and '_'s @slugString = @slugString.delete_prefix('-') @slugString = @slugString.delete_prefix('_') @slugString = @slugString.delete_suffix('-') @slugString = @slugString.delete_suffix('_') # If the size of slug string is greater than the maxLength, trim it if @slugString.length > @maxLength @slugString = trimString(@slugString, @maxLength) end @slugString end |
#substituteChars(mapping) ⇒ Object
Substitutes the characters in slugString with their mapped values Parameters :
1. mapping - The hashmap that contains the one to one mapping of characters to be replaced
86 87 88 89 90 91 92 93 |
# File 'lib/ruby-slugify.rb', line 86 def substituteChars(mapping) len = @slugString.length - 1 for i in 0..len if mapping.key?(@slugString[i]) @slugString[i] = mapping[@slugString[i]] end end end |
#substituteCustom(mapping) ⇒ Object
Substitutes the patterns in slugString with their mapped values given by the user Parameters :
1. mapping - The hashmap that contains the one to one mapping of character runs to be replaced
98 99 100 101 102 103 104 |
# File 'lib/ruby-slugify.rb', line 98 def substituteCustom(mapping) mapping.each do |key, value| find = key.to_s replace = value.to_s @slugString = @slugString.gsub(find, replace) end end |
#trimString(longString, mlen) ⇒ Object
Truncates a string if it is longer than a given length Parameters :
1. longString - the string that is to be truncated
2. mlen - maximum length of the string
Returns :
1. Truncated string
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/ruby-slugify.rb', line 112 def trimString(longString, mlen) # Split at '-' to obtain all the words in the slug splitString = longString.split('-') # If the first word is longer than maxLength, return the slice as the trimmed string if splitString[0].length > mlen return splitString[0][0..mlen-1] end # Ensure the truncated version is below maxLength but has whole words in the slug trimmedString = '' for string in splitString if string.length + trimmedString.length + 1 <= mlen trimmedString = trimmedString + '-' + string else break end end # Remove leading '-' due to concatenation trimmedString = trimmedString.delete_prefix('-') return trimmedString end |