Class: JekyllSlugify::Slugify

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

Overview

Sets the method to be applied on String#slugify.

Constant Summary collapse

SLUGIFY_MODES =

Constants for use in #initialize‘s mode parameter.

%w[raw default pretty ascii latin].freeze
SLUGIFY_RAW_REGEXP =

The raw mode of slugifying a string.

Regexp.new('\\s+').freeze
SLUGIFY_DEFAULT_REGEXP =

The default mode of slugifying a string.

Regexp.new('[^[:alnum:]]+').freeze
SLUGIFY_PRETTY_REGEXP =

The pretty mode of slugifying a string.

Regexp.new("[^[:alnum:]._~!$&'()+,;=@]+").freeze
SLUGIFY_ASCII_REGEXP =

The ascii mode of slugifying a string.

Regexp.new('[^[A-Za-z0-9]]+').freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, mode: nil, cased: false) ⇒ Slugify

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.



71
72
73
74
75
76
77
78
# 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

Instance Attribute Details

#slugObject (readonly)

The slugified string



23
24
25
# File 'lib/jekyll_slugify.rb', line 23

def slug
  @slug
end

Instance Method Details

#to_sString

Returns the slugified string.



83
84
85
# File 'lib/jekyll_slugify.rb', line 83

def to_s
  @slug
end