babosa 0.1.1 → 0.2.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.
- data/README.md +59 -20
- data/Rakefile +2 -0
- data/init.rb +2 -2
- data/lib/babosa.rb +4 -3
- data/lib/babosa/characters.rb +10 -8
- data/lib/babosa/{slug_string.rb → identifier.rb} +90 -47
- data/lib/babosa/version.rb +1 -1
- data/test/babosa_test.rb +33 -8
- metadata +7 -5
data/README.md
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
# Babosa
|
2
2
|
|
3
|
-
Babosa is a library for creating
|
4
|
-
|
5
|
-
|
3
|
+
Babosa is a library for creating human-friendly identifiers. Its primary
|
4
|
+
intended purpose is for creating URL slugs, but can also be useful for
|
5
|
+
normalizing and sanitizing data.
|
6
|
+
|
7
|
+
It is an extraction and improvement of the string code from
|
8
|
+
[FriendlyId](http://github.com/norman/friendly_id). I have released this as a
|
9
|
+
separate library to help developers who want to create libraries similar to
|
10
|
+
FriendlyId.
|
6
11
|
|
7
12
|
## Features / Usage
|
8
13
|
|
@@ -15,8 +20,8 @@ intended to help developers create similar libraries and plugins.
|
|
15
20
|
"Jürgen Müller".to_slug.approximate_ascii.to_s #=> "Jurgen Muller"
|
16
21
|
"Jürgen Müller".to_slug.approximate_ascii(:german).to_s #=> "Juergen Mueller"
|
17
22
|
|
18
|
-
|
19
|
-
contributions and support more languages.
|
23
|
+
Supported language currently include Danish, German, Serbian and Spanish. I'll
|
24
|
+
gladly accept contributions and support more languages.
|
20
25
|
|
21
26
|
### Non-ASCII removal
|
22
27
|
|
@@ -41,17 +46,47 @@ whose length is limited by bytes rather than UTF-8 characters.
|
|
41
46
|
|
42
47
|
"Gölcük, Turkey".to_slug.normalize.to_s #=> "golcuk-turkey"
|
43
48
|
|
49
|
+
### Other stuff
|
50
|
+
|
51
|
+
Babosa can also generate strings for Ruby method names. (Yes, Ruby 1.9 can use UTF-8 chars
|
52
|
+
in method names, but you may not want to):
|
53
|
+
|
54
|
+
|
55
|
+
"this is a method".to_slug.to_ruby_method! #=> this_is_a_method
|
56
|
+
"über cool stuff!".to_slug.to_ruby_method! #=> uber_cool_stuff!
|
57
|
+
|
58
|
+
# You can also disallow trailing punctuation chars
|
59
|
+
"über cool stuff!".to_slug.to_ruby_method(false) #=> uber_cool_stuff
|
60
|
+
|
61
|
+
|
62
|
+
You can add not only transliterations, but expansions for some characters if you want:
|
63
|
+
|
64
|
+
Babosa::Characters.add_approximations(:user, {
|
65
|
+
"0" => "oh",
|
66
|
+
"1" => "one",
|
67
|
+
"2" => "two",
|
68
|
+
"3" => "three",
|
69
|
+
"." => " dot "
|
70
|
+
})
|
71
|
+
"Web 2.0".to_slug.normalize!(:transliterations => :user) #=> "web-two-dot-oh"
|
44
72
|
|
45
73
|
### UTF-8 support
|
46
74
|
|
47
75
|
Babosa has no hard dependencies, but if you have either the Unicode or
|
48
76
|
ActiveSupport gems installed and required prior to requiring "babosa", these
|
49
77
|
will be used to perform upcasing and downcasing on UTF-8 strings. On JRuby 1.5
|
50
|
-
and above, Java's native Unicode support will be used.
|
78
|
+
and above, Java's native Unicode support will be used instead. Unless you're on
|
79
|
+
JRuby, which already has excellent support for Unicode via Java's Standard
|
80
|
+
Library, I recommend using the Unicode gem because it's the fastest Ruby
|
81
|
+
Unicode library available.
|
51
82
|
|
52
83
|
If none of these libraries are available, Babosa falls back to a simple module
|
53
|
-
which
|
54
|
-
|
84
|
+
which only supports Latin characters.
|
85
|
+
|
86
|
+
This default module is fast and can do very naive Unicode composition to ensure
|
87
|
+
that, for example, "é" will always be composed to a single codepoint rather
|
88
|
+
than an "e" and a "´" - making it safe to use as a hash key. But seriously -
|
89
|
+
save yourself the headache and install a real Unicode library.
|
55
90
|
|
56
91
|
|
57
92
|
### Rails 3
|
@@ -59,15 +94,17 @@ using the Unicode gem where possible since it's a C extension and is very fast.
|
|
59
94
|
Most of Babosa's functionality is already present in Active Support/Rails 3.
|
60
95
|
Babosa exists primarily to support non-Rails applications, and Rails apps prior
|
61
96
|
to 3.0. Most of the code here was originally written for FriendlyId. Several
|
62
|
-
things, like tidy_bytes and ASCII transliteration, were later added to Rails
|
97
|
+
things, like `tidy_bytes` and ASCII transliteration, were later added to Rails
|
98
|
+
and I18N.
|
63
99
|
|
64
100
|
Babosa differs from ActiveSupport primarily in that it supports non-Latin
|
65
|
-
strings by default
|
66
|
-
take a look at
|
101
|
+
strings by default, and has per-locale ASCII transliterations already baked-in. If
|
102
|
+
you are considering using Babosa with Rails 3, you should first take a look at
|
103
|
+
Active Support's
|
67
104
|
[transliterate](http://edgeapi.rubyonrails.org/classes/ActiveSupport/Inflector.html#M000565)
|
68
105
|
and
|
69
106
|
[parameterize](http://edgeapi.rubyonrails.org/classes/ActiveSupport/Inflector.html#M000566)
|
70
|
-
because it
|
107
|
+
because it may already do what you need.
|
71
108
|
|
72
109
|
### More info
|
73
110
|
|
@@ -81,8 +118,8 @@ Babosa can be installed via Rubygems:
|
|
81
118
|
|
82
119
|
You can get the source code from its [Github repository](http://github.com/norman/babosa).
|
83
120
|
|
84
|
-
Babosa is tested to be compatible with Ruby 1.8.6-1.9.2, JRuby 1.4-1.5,
|
85
|
-
Rubinius 1.0
|
121
|
+
Babosa is tested to be compatible with Ruby 1.8.6-1.9.2, JRuby 1.4-1.5, and
|
122
|
+
Rubinius 1.0.x. It's probably compatible with other Rubies as well.
|
86
123
|
|
87
124
|
## Reporting bugs
|
88
125
|
|
@@ -99,11 +136,13 @@ Please use Babosa's [Github issue tracker](http://github.com/norman/babosa/issue
|
|
99
136
|
|
100
137
|
## Contributors
|
101
138
|
|
139
|
+
* [Molte Emil Strange Andersen](http://github.com/molte) - Danish support
|
102
140
|
* [Milan Dobrota](http://github.com/milandobrota) - Serbian support
|
103
141
|
|
104
142
|
|
105
143
|
## Changelog
|
106
144
|
|
145
|
+
* 0.2.0 - Added support for Danish. Added method to generate Ruby identifiers. Improved performance.
|
107
146
|
* 0.1.1 - Added support for Serbian.
|
108
147
|
* 0.1.0 - Initial extraction from FriendlyId.
|
109
148
|
|
@@ -111,12 +150,12 @@ Please use Babosa's [Github issue tracker](http://github.com/norman/babosa/issue
|
|
111
150
|
|
112
151
|
Copyright (c) 2010 Norman Clarke
|
113
152
|
|
114
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
153
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
154
|
+
this software and associated documentation files (the "Software"), to deal in
|
155
|
+
the Software without restriction, including without limitation the rights to
|
156
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
157
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
158
|
+
so, subject to the following conditions:
|
120
159
|
|
121
160
|
The above copyright notice and this permission notice shall be included in all
|
122
161
|
copies or substantial portions of the Software.
|
data/Rakefile
CHANGED
data/init.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
3
|
require "babosa"
|
data/lib/babosa.rb
CHANGED
@@ -5,9 +5,10 @@ module Babosa
|
|
5
5
|
end
|
6
6
|
|
7
7
|
class String
|
8
|
-
def
|
9
|
-
Babosa::
|
8
|
+
def to_identifier
|
9
|
+
Babosa::Identifier.new self
|
10
10
|
end
|
11
|
+
alias to_slug to_identifier
|
11
12
|
|
12
13
|
# Compatibility with 1.8.6
|
13
14
|
if !public_method_defined? :bytesize
|
@@ -19,4 +20,4 @@ end
|
|
19
20
|
|
20
21
|
require "babosa/characters"
|
21
22
|
require "babosa/utf8/proxy"
|
22
|
-
require "babosa/
|
23
|
+
require "babosa/identifier"
|
data/lib/babosa/characters.rb
CHANGED
@@ -26,21 +26,23 @@ module Babosa
|
|
26
26
|
# @param [#to_sym] name The name of the approximations to add.
|
27
27
|
# @param Hash hash The approximations to add.
|
28
28
|
def add_approximations(name, hash)
|
29
|
-
@approximations
|
30
|
-
|
29
|
+
approximations = @approximations ? @approximations.dup : {}
|
30
|
+
approximations[name.to_sym] = hash.inject({}) do |memo, object|
|
31
31
|
key = object[0].unpack("U").shift
|
32
32
|
value = object[1].unpack("C*")
|
33
33
|
memo[key] = value.length == 1 ? value[0] : value
|
34
34
|
memo
|
35
|
-
end
|
35
|
+
end.freeze
|
36
|
+
@approximations = approximations.freeze
|
36
37
|
end
|
37
38
|
|
38
|
-
add_approximations :
|
39
|
-
add_approximations :german,
|
39
|
+
add_approximations :danish, "æ" => "ae", "ø" => "oe", "å" => "aa", "Ø" => "Oe", "Å" => "Aa"
|
40
|
+
add_approximations :german, "ä" => "ae", "ö" => "oe", "ü" => "ue", "Ä" => "Ae", "Ö" => "Oe", "Ü" => "Ue"
|
40
41
|
add_approximations :serbian, "Ð" => "Dj", "đ" => "dj" ,"Č" => "Ch", "č" => "ch", "Š" => "Sh", "š" => "sh"
|
42
|
+
add_approximations :spanish, "ñ" => "ni", "Ñ" => "Ni"
|
41
43
|
add_approximations :latin, {
|
42
44
|
"À" => "A", "Á" => "A", "Â" => "A", "Ã" => "A", "Ä" => "A", "Å" => "A",
|
43
|
-
"Æ" => "
|
45
|
+
"Æ" => "Ae", "Ç" => "C", "È" => "E", "É" => "E", "Ê" => "E", "Ë" => "E",
|
44
46
|
"Ì" => "I", "Í" => "I", "Î" => "I", "Ï" => "I", "Ð" => "D", "Ñ" => "N",
|
45
47
|
"Ò" => "O", "Ó" => "O", "Ô" => "O", "Õ" => "O", "Ö" => "O", "Ø" => "O",
|
46
48
|
"Ù" => "U", "Ú" => "U", "Û" => "U", "Ü" => "U", "Ý" => "Y", "Þ" => "Th",
|
@@ -57,11 +59,11 @@ module Babosa
|
|
57
59
|
"Ĝ" => "G", "ĝ" => "g", "Ğ" => "G", "ğ" => "g", "Ġ" => "G", "ġ" => "g",
|
58
60
|
"Ģ" => "G", "ģ" => "g", "Ĥ" => "H", "ĥ" => "h", "Ħ" => "H", "ħ" => "h",
|
59
61
|
"Ĩ" => "I", "ĩ" => "i", "Ī" => "I", "ī" => "i", "Ĭ" => "I", "ĭ" => "i",
|
60
|
-
"Į" => "I", "į" => "i", "İ" => "I", "ı" => "i", "IJ" => "
|
62
|
+
"Į" => "I", "į" => "i", "İ" => "I", "ı" => "i", "IJ" => "Ij", "ij" => "ij",
|
61
63
|
"Ĵ" => "J", "ĵ" => "j", "Ķ" => "K", "ķ" => "k", "ĸ" => "k", "Ĺ" => "L",
|
62
64
|
"ĺ" => "l", "Ļ" => "L", "ļ" => "l", "Ľ" => "L", "ľ" => "l", "Ŀ" => "L",
|
63
65
|
"ŀ" => "l", "Ł" => "L", "ł" => "l", "Ń" => "N", "ń" => "n", "Ņ" => "N",
|
64
|
-
"ņ" => "n", "Ň" => "N", "ň" => "n", "ʼn" => "n", "Ŋ" => "
|
66
|
+
"ņ" => "n", "Ň" => "N", "ň" => "n", "ʼn" => "n", "Ŋ" => "Ng", "ŋ" => "ng",
|
65
67
|
"Ō" => "O", "ō" => "o", "Ŏ" => "O", "ŏ" => "o", "Ő" => "O", "ő" => "o",
|
66
68
|
"Œ" => "OE", "œ" => "oe", "Ŕ" => "R", "ŕ" => "r", "Ŗ" => "R", "ŗ" => "r",
|
67
69
|
"Ř" => "R", "ř" => "r", "Ś" => "S", "ś" => "s", "Ŝ" => "S", "ŝ" => "s",
|
@@ -1,25 +1,24 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
|
3
2
|
module Babosa
|
4
3
|
|
5
4
|
# This class provides some string-manipulation methods specific to slugs.
|
6
5
|
#
|
7
6
|
# Note that this class includes many "bang methods" such as {#clean!} and
|
8
7
|
# {#normalize!} that perform actions on the string in-place. Each of these
|
9
|
-
# methods has a corresponding "bangless" method (i.e., +
|
10
|
-
# and +
|
8
|
+
# methods has a corresponding "bangless" method (i.e., +Identifier#clean!+
|
9
|
+
# and +Identifier#clean+) which does not appear in the documentation because
|
11
10
|
# it is generated dynamically.
|
12
11
|
#
|
13
12
|
# All of the bang methods return an instance of String, while the bangless
|
14
|
-
# versions return an instance of Babosa::
|
13
|
+
# versions return an instance of Babosa::Identifier, so that calls to methods
|
15
14
|
# specific to this class can be chained:
|
16
15
|
#
|
17
|
-
# string =
|
18
|
-
# string.
|
19
|
-
# string.
|
16
|
+
# string = Identifier.new("hello world")
|
17
|
+
# string.with_separators! # => "hello-world"
|
18
|
+
# string.with_separators # => <Babosa::Identifier:0x000001013e1590 @wrapped_string="hello-world">
|
20
19
|
#
|
21
20
|
# @see http://www.utf8-chartable.de/unicode-utf8-table.pl?utf8=dec Unicode character table
|
22
|
-
class
|
21
|
+
class Identifier
|
23
22
|
|
24
23
|
attr_reader :wrapped_string
|
25
24
|
alias to_s wrapped_string
|
@@ -50,7 +49,7 @@ module Babosa
|
|
50
49
|
@wrapped_string.__send__(symbol, *args, &block)
|
51
50
|
end
|
52
51
|
|
53
|
-
# @param string [#to_s] The string to use as the basis of the
|
52
|
+
# @param string [#to_s] The string to use as the basis of the Identifier.
|
54
53
|
def initialize(string)
|
55
54
|
@wrapped_string = string.to_s
|
56
55
|
tidy_bytes!
|
@@ -61,21 +60,21 @@ module Babosa
|
|
61
60
|
# characters that are Roman-alphabet characters + diacritics. Non-letter
|
62
61
|
# characters are left unmodified.
|
63
62
|
#
|
64
|
-
# string =
|
65
|
-
# string.
|
66
|
-
# string =
|
67
|
-
# string.
|
63
|
+
# string = Identifier.new "Łódź, Poland"
|
64
|
+
# string.transliterate # => "Lodz, Poland"
|
65
|
+
# string = Identifier.new "日本"
|
66
|
+
# string.transliterate # => "日本"
|
68
67
|
#
|
69
68
|
# You can pass any key(s) from +Characters.approximations+ as arguments. This allows
|
70
|
-
# for contextual approximations.
|
71
|
-
#
|
69
|
+
# for contextual approximations. Danish, German, Serbian and Spanish are currently
|
70
|
+
# supported.
|
72
71
|
#
|
73
|
-
# string =
|
74
|
-
# string.
|
75
|
-
# string.
|
76
|
-
# string =
|
77
|
-
# string.
|
78
|
-
# string.
|
72
|
+
# string = Identifier.new "Jürgen Müller"
|
73
|
+
# string.transliterate # => "Jurgen Muller"
|
74
|
+
# string.transliterate :german # => "Juergen Mueller"
|
75
|
+
# string = Identifier.new "¡Feliz año!"
|
76
|
+
# string.transliterate # => "¡Feliz ano!"
|
77
|
+
# string.transliterate :spanish # => "¡Feliz anio!"
|
79
78
|
#
|
80
79
|
# You can modify the built-in approximations, or add your own:
|
81
80
|
#
|
@@ -85,13 +84,17 @@ module Babosa
|
|
85
84
|
# Notice that this method does not simply convert to ASCII; if you want
|
86
85
|
# to remove non-ASCII characters such as "¡" and "¿", use {#to_ascii!}:
|
87
86
|
#
|
88
|
-
# string.
|
89
|
-
# string.
|
87
|
+
# string.transliterate!(:spanish) # => "¡Feliz anio!"
|
88
|
+
# string.transliterate! # => "Feliz anio!"
|
90
89
|
# @param *args <Symbol>
|
91
90
|
# @return String
|
92
|
-
def
|
93
|
-
|
94
|
-
|
91
|
+
def transliterate!(transliterations = {})
|
92
|
+
if transliterations.kind_of? Symbol
|
93
|
+
transliterations = Characters.approximations[transliterations]
|
94
|
+
else
|
95
|
+
transliterations ||= {}
|
96
|
+
end
|
97
|
+
@wrapped_string = unpack("U*").map { |char| approx_char(char, transliterations) }.flatten.pack("U*")
|
95
98
|
end
|
96
99
|
|
97
100
|
# Converts dashes to spaces, removes leading and trailing spaces, and
|
@@ -108,22 +111,47 @@ module Babosa
|
|
108
111
|
@wrapped_string = (unpack("U*") - Characters.strippable).pack("U*")
|
109
112
|
end
|
110
113
|
|
111
|
-
# Normalize the string for use as a slug. Note that in this context,
|
114
|
+
# Normalize the string for use as a URL slug. Note that in this context,
|
112
115
|
# +normalize+ means, strip, remove non-letters/numbers, downcasing,
|
113
116
|
# truncating to 255 bytes and converting whitespace to dashes.
|
114
|
-
# @param
|
117
|
+
# @param Options
|
115
118
|
# @return String
|
116
|
-
def normalize!(
|
117
|
-
|
118
|
-
|
119
|
-
|
119
|
+
def normalize!(options = nil)
|
120
|
+
# Handle deprecated usage
|
121
|
+
if options == true
|
122
|
+
warn "#normalize! now takes a hash of options rather than a boolean"
|
123
|
+
options = default_normalize_options.merge(:to_ascii => true)
|
124
|
+
else
|
125
|
+
options = default_normalize_options.merge(options || {})
|
126
|
+
end
|
127
|
+
if options[:transliterate]
|
128
|
+
transliterate!(*options[:transliterations])
|
120
129
|
end
|
130
|
+
to_ascii! if options[:to_ascii]
|
121
131
|
clean!
|
122
132
|
word_chars!
|
123
133
|
clean!
|
124
134
|
downcase!
|
125
|
-
truncate_bytes!(
|
126
|
-
|
135
|
+
truncate_bytes!(options[:max_length])
|
136
|
+
with_separators!(options[:separator])
|
137
|
+
end
|
138
|
+
|
139
|
+
# Normalize a string so that it can safely be used as a Ruby method name.
|
140
|
+
def to_ruby_method!(allow_bangs = true)
|
141
|
+
leader, trailer = @wrapped_string.strip.scan(/\A(.+)(.)\z/).flatten
|
142
|
+
if allow_bangs
|
143
|
+
trailer.downcase.gsub!(/[^a-z0-9!=\\\\?]/, '')
|
144
|
+
else
|
145
|
+
trailer.downcase.gsub!(/[^a-z0-9]/, '')
|
146
|
+
end
|
147
|
+
id = leader.to_identifier
|
148
|
+
id.transliterate!
|
149
|
+
id.to_ascii!
|
150
|
+
id.clean!
|
151
|
+
id.word_chars!
|
152
|
+
id.clean!
|
153
|
+
@wrapped_string = id.to_s + trailer
|
154
|
+
with_separators!("_")
|
127
155
|
end
|
128
156
|
|
129
157
|
# Delete any non-ascii characters.
|
@@ -134,7 +162,7 @@ module Babosa
|
|
134
162
|
|
135
163
|
# Truncate the string to +max+ characters.
|
136
164
|
# @example
|
137
|
-
# "üéøá".
|
165
|
+
# "üéøá".to_identifier.truncate(3) #=> "üéø"
|
138
166
|
# @return String
|
139
167
|
def truncate!(max)
|
140
168
|
@wrapped_string = unpack("U*")[0...max].pack("U*")
|
@@ -145,7 +173,7 @@ module Babosa
|
|
145
173
|
# byte length. The resulting string may be less than +max+ if the string must
|
146
174
|
# be truncated at a multibyte character boundary.
|
147
175
|
# @example
|
148
|
-
# "üéøá".
|
176
|
+
# "üéøá".to_identifier.truncate_bytes(3) #=> "ü"
|
149
177
|
# @return String
|
150
178
|
def truncate_bytes!(max)
|
151
179
|
return @wrapped_string if @wrapped_string.bytesize <= max
|
@@ -164,8 +192,8 @@ module Babosa
|
|
164
192
|
|
165
193
|
# Replaces whitespace with dashes ("-").
|
166
194
|
# @return String
|
167
|
-
def
|
168
|
-
@wrapped_string = @wrapped_string.gsub(/\s/u,
|
195
|
+
def with_separators!(char = "-")
|
196
|
+
@wrapped_string = @wrapped_string.gsub(/\s/u, char)
|
169
197
|
end
|
170
198
|
|
171
199
|
# Perform UTF-8 sensitive upcasing.
|
@@ -193,31 +221,46 @@ module Babosa
|
|
193
221
|
@wrapped_string = @@utf8_proxy.tidy_bytes(@wrapped_string)
|
194
222
|
end
|
195
223
|
|
196
|
-
%w[
|
197
|
-
tidy_bytes to_ascii truncate truncate_bytes upcase
|
198
|
-
class_eval(<<-EOM)
|
224
|
+
%w[transliterate clean downcase word_chars normalize normalize_utf8
|
225
|
+
tidy_bytes to_ascii truncate truncate_bytes upcase with_separators].each do |method|
|
226
|
+
class_eval(<<-EOM, __FILE__, __LINE__ +1)
|
199
227
|
def #{method}(*args)
|
200
228
|
send_to_new_instance(:#{method}!, *args)
|
201
229
|
end
|
202
230
|
EOM
|
203
231
|
end
|
204
232
|
|
205
|
-
def
|
233
|
+
def to_identifier
|
206
234
|
self
|
207
235
|
end
|
208
236
|
|
237
|
+
# The default options for {#normalize!}. Override to set your own defaults.
|
238
|
+
def default_normalize_options
|
239
|
+
{:transliterate => true, :max_length => 255, :separator => "-"}
|
240
|
+
end
|
241
|
+
|
242
|
+
alias approximate_ascii transliterate
|
243
|
+
alias approximate_ascii! transliterate!
|
244
|
+
alias with_dashes with_separators
|
245
|
+
alias with_dashes! with_separators!
|
246
|
+
alias to_slug to_identifier
|
247
|
+
|
209
248
|
private
|
210
249
|
|
211
250
|
# Look up the character's approximation in the configured maps.
|
212
|
-
def approx_char(char,
|
213
|
-
|
251
|
+
def approx_char(char, transliterations = {})
|
252
|
+
transliterations[char] or Characters.approximations[:latin][char] or char
|
214
253
|
end
|
215
254
|
|
216
255
|
# Used as the basis of the bangless methods.
|
217
256
|
def send_to_new_instance(*args)
|
218
|
-
|
219
|
-
|
220
|
-
|
257
|
+
id = Identifier.allocate
|
258
|
+
id.instance_variable_set :@wrapped_string, to_s
|
259
|
+
id.send(*args)
|
260
|
+
id
|
221
261
|
end
|
222
262
|
end
|
263
|
+
|
264
|
+
# Identifier is aliased as SlugString to support older versions of FriendlyId.
|
265
|
+
SlugString = Identifier
|
223
266
|
end
|
data/lib/babosa/version.rb
CHANGED
data/test/babosa_test.rb
CHANGED
@@ -1,11 +1,8 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
$KCODE = 'UTF8' if RUBY_VERSION < '1.9'
|
3
|
-
$LOAD_PATH << File.expand_path("../../lib", __FILE__)
|
4
|
-
$LOAD_PATH.uniq!
|
5
3
|
|
6
4
|
require "rubygems"
|
7
|
-
require "bundler"
|
8
|
-
Bundler.setup
|
5
|
+
require "bundler/setup"
|
9
6
|
require "test/unit"
|
10
7
|
require "babosa"
|
11
8
|
|
@@ -106,7 +103,11 @@ class BabosaTest < Test::Unit::TestCase
|
|
106
103
|
end
|
107
104
|
|
108
105
|
test "should do special approximations for German" do
|
109
|
-
|
106
|
+
{
|
107
|
+
"Jürgen" => "Juergen",
|
108
|
+
"böse" => "boese",
|
109
|
+
"Männer" => "Maenner"
|
110
|
+
}.each {|given, expected| assert_equal expected, given.to_slug.approximate_ascii!(:german)}
|
110
111
|
end
|
111
112
|
|
112
113
|
test "should do special approximations for Spanish" do
|
@@ -114,7 +115,21 @@ class BabosaTest < Test::Unit::TestCase
|
|
114
115
|
end
|
115
116
|
|
116
117
|
test "should do special approximations for Serbian" do
|
117
|
-
|
118
|
+
{
|
119
|
+
"Ðorđe" => "Djordje",
|
120
|
+
"Inđija" => "Indjija",
|
121
|
+
"Četiri" => "Chetiri",
|
122
|
+
"četiri" => "chetiri",
|
123
|
+
"Škola" => "Shkola",
|
124
|
+
"škola" => "shkola"
|
125
|
+
}.each {|given, expected| assert_equal expected, given.to_slug.approximate_ascii!(:serbian)}
|
126
|
+
end
|
127
|
+
|
128
|
+
test "should do special approximations for Danish" do
|
129
|
+
{
|
130
|
+
"Ærøskøbing" => "Aeroeskoebing",
|
131
|
+
"Årslev" => "Aarslev"
|
132
|
+
}.each {|given, expected| assert_equal expected, given.to_slug.approximate_ascii!(:danish)}
|
118
133
|
end
|
119
134
|
|
120
135
|
test "should work with non roman chars" do
|
@@ -156,9 +171,19 @@ class BabosaTest < Test::Unit::TestCase
|
|
156
171
|
assert_equal " a bc ".bytesize, " a bc ".to_slug.with_dashes.bytesize
|
157
172
|
end
|
158
173
|
|
159
|
-
test "normalize! with ascii should approximate and strip non ascii" do
|
174
|
+
test "normalize! with ascii option should approximate and strip non ascii" do
|
160
175
|
ss = "カタカナ: katakana is über cool".to_slug
|
161
|
-
assert_equal "katakana-is-uber-cool", ss.normalize!(true)
|
176
|
+
assert_equal "katakana-is-uber-cool", ss.normalize!(:to_ascii => true)
|
162
177
|
end
|
163
178
|
|
179
|
+
test "normalize should use transliterations" do
|
180
|
+
assert_equal "juergen", "Jürgen".to_slug.normalize(:transliterations => :german).to_s
|
181
|
+
end
|
182
|
+
|
183
|
+
test "should get a string suitable for use as a ruby method" do
|
184
|
+
assert_equal "hello_world?", "¿¿¿hello... world???".to_slug.to_ruby_method!
|
185
|
+
assert_equal "katakana_is_uber_cool", "カタカナ: katakana is über cool".to_slug.to_ruby_method!
|
186
|
+
assert_equal "katakana_is_uber_cool!", "カタカナ: katakana is über cool!".to_slug.to_ruby_method!
|
187
|
+
assert_equal "katakana_is_uber_cool", "カタカナ: katakana is über cool".to_slug.to_ruby_method!(false)
|
188
|
+
end
|
164
189
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Norman Clarke
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-08-
|
17
|
+
date: 2010-08-30 00:00:00 -03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -28,7 +28,7 @@ extra_rdoc_files: []
|
|
28
28
|
|
29
29
|
files:
|
30
30
|
- lib/babosa/characters.rb
|
31
|
-
- lib/babosa/
|
31
|
+
- lib/babosa/identifier.rb
|
32
32
|
- lib/babosa/utf8/active_support_proxy.rb
|
33
33
|
- lib/babosa/utf8/dumb_proxy.rb
|
34
34
|
- lib/babosa/utf8/java_proxy.rb
|
@@ -56,6 +56,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
56
56
|
requirements:
|
57
57
|
- - ">="
|
58
58
|
- !ruby/object:Gem::Version
|
59
|
+
hash: 1210160351012131207
|
59
60
|
segments:
|
60
61
|
- 0
|
61
62
|
version: "0"
|
@@ -64,6 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
65
|
requirements:
|
65
66
|
- - ">="
|
66
67
|
- !ruby/object:Gem::Version
|
68
|
+
hash: 1210160351012131207
|
67
69
|
segments:
|
68
70
|
- 0
|
69
71
|
version: "0"
|