| Class | FriendlyId::SlugString |
| In: |
lib/friendly_id/slug_string.rb
|
| Parent: | ActiveSupport::Multibyte::Chars |
This class provides some string-manipulation methods specific to slugs. Its Unicode support is provided by ActiveSupport::Multibyte::Chars; this is needed primarily for Unicode encoding normalization and proper calculation of string lengths.
Note that this class includes many "bang methods" such as {clean!} and {normalize!} that perform actions on the string in-place. Each of these methods has a corresponding "bangless" method (i.e., +SlugString#clean!+ and +SlugString#clean+) which does not appear in the documentation because it is generated dynamically.
All of the bang methods return an instance of String, while the bangless versions return an instance of FriendlyId::SlugString, so that calls to methods specific to this class can be chained:
string = SlugString.new("hello world")
string.with_dashes! # => "hello-world"
string.with_dashes # => <FriendlyId::SlugString:0x000001013e1590 @wrapped_string="hello-world">
@see www.utf8-chartable.de/unicode-utf8-table.pl?utf8=dec Unicode character table @see FriendlyId::SlugString::dump_approximations
| APPROXIMATIONS | = | { :common => Hash[ 192, 65, 193, 65, 194, 65, 195, 65, 196, 65, 197, 65, 198, [65, 69], 199, 67, 200, 69, 201, 69, 202, 69, 203, 69, 204, 73, 205, 73, 206, 73, 207, 73, 208, 68, 209, 78, 210, 79, 211, 79, 212, 79, 213, 79, 214, 79, 215, 120, 216, 79, 217, 85, 218, 85, 219, 85, 220, 85, 221, 89, 222, [84, 104], 223, [115, 115], 224, 97, 225, 97, 226, 97, 227, 97, 228, 97, 229, 97, 230, [97, 101], 231, 99, 232, 101, 233, 101, 234, 101, 235, 101, 236, 105, 237, 105, 238, 105, 239, 105, 240, 100, 241, 110, 242, 111, 243, 111, 244, 111, 245, 111, 246, 111, 248, 111, 249, 117, 250, 117, 251, 117, 252, 117, 253, 121, 254, [116, 104], 255, 121, 256, 65, 257, 97, 258, 65, 259, 97, 260, 65, 261, 97, 262, 67, 263, 99, 264, 67, 265, 99, 266, 67, 267, 99, 268, 67, 269, 99, 270, 68, 271, 100, 272, 68, 273, 100, 274, 69, 275, 101, 276, 69, 277, 101, 278, 69, 279, 101, 280, 69, 281, 101, 282, 69, 283, 101, 284, 71, 285, 103, 286, 71, 287, 103, 288, 71, 289, 103, 290, 71, 291, 103, 292, 72, 293, 104, 294, 72, 295, 104, 296, 73, 297, 105, 298, 73, 299, 105, 300, 73, 301, 105, 302, 73, 303, 105, 304, 73, 305, 105, 306, [73, 74], 307, [105, 106], 308, 74, 309, 106, 310, 75, 311, 107, 312, 107, 313, 76, 314, 108, 315, 76, 316, 108, 317, 76, 318, 108, 319, 76, 320, 108, 321, 76, 322, 108, 323, 78, 324, 110, 325, 78, 326, 110, 327, 78, 328, 110, 329, [39, 110], 330, [78, 71], 331, [110, 103], 332, 79, 333, 111, 334, 79, 335, 111, 336, 79, 337, 111, 338, [79, 69], 339, [111, 101], 340, 82, 341, 114, 342, 82, 343, 114, 344, 82, 345, 114, 346, 83, 347, 115, 348, 83, 349, 115, 350, 83, 351, 115, 352, 83, 353, 115, 354, 84, 355, 116, 356, 84, 357, 116, 358, 84, 359, 116, 360, 85, 361, 117, 362, 85, 363, 117, 364, 85, 365, 117, 366, 85, 367, 117, 368, 85, 369, 117, 370, 85, 371, 117, 372, 87, 373, 119, 374, 89, 375, 121, 376, 89, 377, 90, 378, 122, 379, 90, 380, 122, 381, 90, 382, 122 | All values are Unicode decimal characters or character arrays. |
| normalize | -> | normalize_utf8 |
This method can be used by developers wishing to debug the {APPROXIMATIONS} hashes, which are written in a hard-to-read format. @return Hash @example
> ruby -rrubygems -rlib/friendly_id -e 'p FriendlyId::SlugString.dump_approximations'
{:common => {"À"=>"A", "Á"=>"A", "Â"=>"A", "Ã"=>"A", "Ä"=>"A", "Å"=>"A", "Æ"=>"AE", "Ç"=>"C", "È"=>"E", "É"=>"E", "Ê"=>"E", "Ë"=>"E", "Ì"=>"I", "Í"=>"I", "Î"=>"I", "Ï"=>"I", "Ð"=>"D", "Ñ"=>"N", "Ò"=>"O", "Ó"=>"O", "Ô"=>"O", "Õ"=>"O", "Ö"=>"O", "×"=>"x", "Ø"=>"O", "Ù"=>"U", "Ú"=>"U", "Û"=>"U", "Ü"=>"U", "Ý"=>"Y", "Þ"=>"Th", "ß"=>"ss", "à"=>"a", "á"=>"a", "â"=>"a", "ã"=>"a", "ä"=>"a", "å"=>"a", "æ"=>"ae", "ç"=>"c", "è"=>"e", "é"=>"e", "ê"=>"e", "ë"=>"e", "ì"=>"i", "í"=>"i", "î"=>"i", "ï"=>"i", "ð"=>"d", "ñ"=>"n", "ò"=>"o", "ó"=>"o", "ô"=>"o", "õ"=>"o", "ö"=>"o", "ø"=>"o", "ù"=>"u", "ú"=>"u", "û"=>"u", "ü"=>"u", "ý"=>"y", "þ"=>"th", "ÿ"=>"y", "Ā"=>"A", "ā"=>"a", "Ă"=>"A", "ă"=>"a", "Ą"=>"A", "ą"=>"a", "Ć"=>"C", "ć"=>"c", "Ĉ"=>"C", "ĉ"=>"c", "Ċ"=>"C", "ċ"=>"c", "Č"=>"C", "č"=>"c", "Ď"=>"D", "ď"=>"d", "Đ"=>"D", "đ"=>"d", "Ē"=>"E", "ē"=>"e", "Ĕ"=>"E", "ĕ"=>"e", "Ė"=>"E", "ė"=>"e", "Ę"=>"E", "ę"=>"e", "Ě"=>"E", "ě"=>"e", "Ĝ"=>"G", "ĝ"=>"g", "Ğ"=>"G", "ğ"=>"g", "Ġ"=>"G", "ġ"=>"g", "Ģ"=>"G", "ģ"=>"g", "Ĥ"=>"H", "ĥ"=>"h", "Ħ"=>"H", "ħ"=>"h", "Ĩ"=>"I", "ĩ"=>"i", "Ī"=>"I", "ī"=>"i", "Ĭ"=>"I", "ĭ"=>"i", "Į"=>"I", "į"=>"i", "İ"=>"I", "ı"=>"i", "IJ"=>"IJ", "ij"=>"ij", "Ĵ"=>"J", "ĵ"=>"j", "Ķ"=>"K", "ķ"=>"k", "ĸ"=>"k", "Ĺ"=>"L", "ĺ"=>"l", "Ļ"=>"L", "ļ"=>"l", "Ľ"=>"L", "ľ"=>"l", "Ŀ"=>"L", "ŀ"=>"l", "Ł"=>"L", "ł"=>"l", "Ń"=>"N", "ń"=>"n", "Ņ"=>"N", "ņ"=>"n", "Ň"=>"N", "ň"=>"n", "ʼn"=>"’n", "Ŋ"=>"NG", "ŋ"=>"ng", "Ō"=>"O", "ō"=>"o", "Ŏ"=>"O", "ŏ"=>"o", "Ő"=>"O", "ő"=>"o", "Œ"=>"OE", "œ"=>"oe", "Ŕ"=>"R", "ŕ"=>"r", "Ŗ"=>"R", "ŗ"=>"r", "Ř"=>"R", "ř"=>"r", "Ś"=>"S", "ś"=>"s", "Ŝ"=>"S", "ŝ"=>"s", "Ş"=>"S", "ş"=>"s", "Š"=>"S", "š"=>"s", "Ţ"=>"T", "ţ"=>"t", "Ť"=>"T", "ť"=>"t", "Ŧ"=>"T", "ŧ"=>"t", "Ũ"=>"U", "ũ"=>"u", "Ū"=>"U", "ū"=>"u", "Ŭ"=>"U", "ŭ"=>"u", "Ů"=>"U", "ů"=>"u", "Ű"=>"U", "ű"=>"u", "Ų"=>"U", "ų"=>"u", "Ŵ"=>"W", "ŵ"=>"w", "Ŷ"=>"Y", "ŷ"=>"y", "Ÿ"=>"Y", "Ź"=>"Z", "ź"=>"z", "Ż"=>"Z", "ż"=>"z", "Ž"=>"Z", "ž"=>"z"}, :german => {"ü"=>"ue", "ö"=>"oe", "ä"=>"ae"}, :spanish => {"Ñ"=>"Nn", "ñ"=>"nn"}}
Approximate an ASCII string. This works only for Western strings using characters that are Roman-alphabet characters + diacritics. Non-letter characters are left unmodified.
string = SlugString.new "Łódź, Poland" string.approximate_ascii # => "Lodz, Poland" string = SlugString.new "日本" string.approximate_ascii # => "日本"
You can pass any key(s) from {APPROXIMATIONS} as arguments. This allows for contextual approximations. By default; +:spanish+ and +:german+ are provided:
string = SlugString.new "Jürgen Müller" string.approximate_ascii # => "Jurgen Muller" string.approximate_ascii :german # => "Juergen Mueller" string = SlugString.new "¡Feliz año!" string.approximate_ascii # => "¡Feliz ano!" string.approximate_ascii :spanish # => "¡Feliz anno!"
You can modify the built-in approximations, or add your own:
# Make Spanish use "nh" rather than "nn"
FriendlyId::SlugString::APPROXIMATIONS[:spanish] = {
# Ñ => "Nh"
209 => [78, 104],
# ñ => "nh"
241 => [110, 104]
}
It‘s also possible to use a custom approximation for all strings:
FriendlyId::SlugString.approximations << :german
Notice that this method does not simply convert to ASCII; if you want to remove non-ASCII characters such as "¡" and "¿", use {to_ascii!}:
string.approximate_ascii!(:spanish) # => "¡Feliz anno!" string.to_ascii! # => "Feliz anno!"
@param *args <Symbol> @return String
Removes leading and trailing spaces or dashses, and replaces multiple whitespace characters with a single space. @return String
Lowercases the string. Note that this works for Unicode strings, though your milage may vary with Greek and Turkic strings. @return String
Normalize the string for use as a FriendlyId. Note that in this context, normalize means, strip, remove non-letters/numbers, downcasing and converting whitespace to dashes. ActiveSupport::Multibyte::Chars#normalize is aliased to +normalize_utf8+ in this subclass. @return String
Normalize the string for a given {FriendlyId::Configuration}. @param config [FriendlyId::Configuration] @return String
Upper-cases the string. Note that this works for Unicode strings, though your milage may vary with Greek and Turkic strings. @return String
Validate that the slug string is not blank or reserved, and truncate it to the max length if necessary. @param config [FriendlyId::Configuration] @return String @raise FriendlyId::BlankError @raise FriendlyId::ReservedError