class JekyllSlugify::Slugify
Sets the method to be applied on {String#slugify}.
Constants
- SLUGIFY_ASCII_REGEXP
The ascii mode of slugifying a string.
- SLUGIFY_DEFAULT_REGEXP
The default mode of slugifying a string.
- SLUGIFY_MODES
Constants for use in {#initialize}'s mode parameter.
- SLUGIFY_PRETTY_REGEXP
The pretty mode of slugifying a string.
- SLUGIFY_RAW_REGEXP
The raw mode of slugifying a string.
Attributes
The slugified string
Public Class Methods
Slugify
a filename or title.
string - the filename or title to slugify mode - how string is slugified cased - whether to replace all uppercase letters with their lowercase counterparts
When mode is “none”, return the given string.
When mode is “raw”, return the given string, with every sequence of spaces characters replaced with a hyphen.
When mode is “default” or nil, non-alphabetic characters are replaced with a hyphen too.
When mode is “pretty”, some non-alphabetic characters (._~!$&'()+,;=@) are not replaced with hyphen.
When mode is “ascii”, some everything else except ASCII characters a-z (lowercase), A-Z (uppercase) and 0-9 (numbers) are not replaced with hyphen.
When mode is “latin”, the input string is first preprocessed so that any letters with accents are replaced with the plain letter. Afterwards, it follows the “default” mode of operation.
If cased is true, all uppercase letters in the result string are replaced with their lowercase counterparts.
Examples:
slugify("The _config.yml file") # => "the-config-yml-file" slugify("The _config.yml file", "pretty") # => "the-_config.yml-file" slugify("The _config.yml file", "pretty", true) # => "The-_config.yml file" slugify("The _config.yml file", "ascii") # => "the-config-yml-file" slugify("The _config.yml file", "latin") # => "the-config-yml-file"
Returns the slugified string.
# File lib/jekyll_slugify.rb, line 71 def initialize(string, mode: nil, cased: false) string, mode = check_params(string, mode, cased) string = deal_with_locales(mode, string) @slug = replace_character_sequence_with_hyphen(string, mode: mode) @slug = process_slug(@slug, cased) slug_empty?(@slug) end
Public Instance Methods
Returns the slugified string.
@return [String] The slugified string.
# File lib/jekyll_slugify.rb, line 83 def to_s @slug end
Private Instance Methods
# File lib/jekyll_slugify.rb, line 89 def check_params(string, mode, cased) mode ||= 'default' msg = 'String must be a non-nil String' raise ArgumentError, msg if string.nil? raise ArgumentError, msg unless string.is_a? String mode = 'latin' unless SLUGIFY_MODES.include?(mode) string = cased ? string : string.downcase [string, mode] end
# File lib/jekyll_slugify.rb, line 114 def deal_with_locales(mode, string) # Drop accent marks from latin characters. Everything else turns to ? if mode == 'latin' if I18n.config.available_locales.empty? I18n.config.available_locales = :en end string = I18n.transliterate(string) end string end
# File lib/jekyll_slugify.rb, line 101 def process_slug(slug, cased) # Remove leading/trailing hyphen slug.gsub!(/^\-|\-$/i, '') slug.downcase! unless cased slug end
# File lib/jekyll_slugify.rb, line 130 def replace_character(mode) case mode when 'raw' SLUGIFY_RAW_REGEXP when 'pretty' SLUGIFY_PRETTY_REGEXP when 'ascii' SLUGIFY_ASCII_REGEXP else SLUGIFY_DEFAULT_REGEXP end end
# File lib/jekyll_slugify.rb, line 125 def replace_character_sequence_with_hyphen(string, mode: 'default') replaceable_char = replace_character(mode) string.gsub(replaceable_char, '-') end
# File lib/jekyll_slugify.rb, line 109 def slug_empty?(slug) msg = 'Empty `slug` generated for given String' raise msg if slug.empty? end