srbovanje 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +33 -0
- data/VERSION.yml +4 -0
- data/lib/srbovanje.rb +3 -0
- data/lib/srbovanje/string_ext.rb +153 -0
- data/test/string_ext_test.rb +62 -0
- data/test/test_helper.rb +3 -0
- metadata +61 -0
data/README.rdoc
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
= srbovanje
|
2
|
+
|
3
|
+
Esencijalni Ruby Gem za Ruby aplikaciju na srpskom. Pruža mogućnosti:
|
4
|
+
|
5
|
+
* transliteracije ćirilice na latinicu (npr. "Ђурђевак" u "Đurđevak")
|
6
|
+
* transliteracije ćirilice na ascii latinicu (npr. "Ђурђевак" u "Djurdjevak")
|
7
|
+
* slugifikacije (parametrizaciju) stringa na srpskom (npr: "Železnica Srbije" u "zeleznica-srbije")
|
8
|
+
* ispisa relativnih vremena u rečima, pravopisno ispravno (npr. "3 sata"; Rails lokalizacija ne razlikuje različite oblike u zavisnosti od količine i ispisao bi pogrešno "3 sati")
|
9
|
+
|
10
|
+
|
11
|
+
== Upotreba
|
12
|
+
|
13
|
+
=== Transliteracije ćirilice na latinicu
|
14
|
+
|
15
|
+
>> "Ђурђевак".latin
|
16
|
+
=> "Đurđevak"
|
17
|
+
|
18
|
+
=== Transliteracije ćirilice na ascii latinicu
|
19
|
+
|
20
|
+
>> "Ђурђевак".ascii_latin
|
21
|
+
=> "Djurdjevak"
|
22
|
+
|
23
|
+
=== slugifikacije (parametrizaciju) stringa na srpskom
|
24
|
+
|
25
|
+
>> "Železnica Srbije".ascii_latin
|
26
|
+
=> "zeleznica-srbije"
|
27
|
+
|
28
|
+
=== ispisa relativnih vremena u rečima (npr. "3 sata")
|
29
|
+
|
30
|
+
|
31
|
+
== Autor
|
32
|
+
|
33
|
+
{Dejan Simić}[http://github.com/dejan]
|
data/VERSION.yml
ADDED
data/lib/srbovanje.rb
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
class String
|
2
|
+
|
3
|
+
@@latin_map = {
|
4
|
+
'а' => 'a',
|
5
|
+
'А' => 'A',
|
6
|
+
'б' => 'b',
|
7
|
+
'Б' => 'B',
|
8
|
+
'в' => 'v',
|
9
|
+
'В' => 'V',
|
10
|
+
'г' => 'g',
|
11
|
+
'Г' => 'G',
|
12
|
+
'д' => 'd',
|
13
|
+
'Д' => 'D',
|
14
|
+
'ђ' => 'đ',
|
15
|
+
'Ђ' => 'Đ',
|
16
|
+
'е' => 'e',
|
17
|
+
'Е' => 'E',
|
18
|
+
'ж' => 'ž',
|
19
|
+
'Ж' => 'Ž',
|
20
|
+
'з' => 'z',
|
21
|
+
'З' => 'Z',
|
22
|
+
'и' => 'i',
|
23
|
+
'И' => 'I',
|
24
|
+
'ј' => 'j',
|
25
|
+
'Ј' => 'J',
|
26
|
+
'к' => 'k',
|
27
|
+
'К' => 'K',
|
28
|
+
'л' => 'l',
|
29
|
+
'Л' => 'L',
|
30
|
+
'љ' => 'lj',
|
31
|
+
'Љ' => 'LJ',
|
32
|
+
'м' => 'm',
|
33
|
+
'М' => 'M',
|
34
|
+
'н' => 'n',
|
35
|
+
'Н' => 'N',
|
36
|
+
'њ' => 'nj',
|
37
|
+
'Њ' => 'NJ',
|
38
|
+
'о' => 'o',
|
39
|
+
'О' => 'O',
|
40
|
+
'п' => 'p',
|
41
|
+
'П' => 'P',
|
42
|
+
'р' => 'r',
|
43
|
+
'Р' => 'R',
|
44
|
+
'с' => 's',
|
45
|
+
'С' => 'S',
|
46
|
+
'т' => 't',
|
47
|
+
'Т' => 'T',
|
48
|
+
'ћ' => 'ć',
|
49
|
+
'Ћ' => 'Ć',
|
50
|
+
'ц' => 'c',
|
51
|
+
'Ц' => 'C',
|
52
|
+
'у' => 'u',
|
53
|
+
'У' => 'U',
|
54
|
+
'ф' => 'f',
|
55
|
+
'Ф' => 'F',
|
56
|
+
'х' => 'h',
|
57
|
+
'Х' => 'H',
|
58
|
+
'ч' => 'č',
|
59
|
+
'Ч' => 'Č',
|
60
|
+
'џ' => 'dž',
|
61
|
+
'Џ' => 'DŽ',
|
62
|
+
'ш' => 'š',
|
63
|
+
'Ш' => 'Š' }
|
64
|
+
|
65
|
+
@@ascii_latin_map =
|
66
|
+
@@latin_map.merge(
|
67
|
+
'ћ' => 'c',
|
68
|
+
'ć' => 'c',
|
69
|
+
'ч' => 'c',
|
70
|
+
'č' => 'c',
|
71
|
+
'Ћ' => 'C',
|
72
|
+
'Ć' => 'C',
|
73
|
+
'Ч' => 'C',
|
74
|
+
'Č' => 'C',
|
75
|
+
'ж' => 'z',
|
76
|
+
'ž' => 'z',
|
77
|
+
'Ж' => 'Z',
|
78
|
+
'Ž' => 'Z',
|
79
|
+
'š' => 's',
|
80
|
+
'ш' => 's',
|
81
|
+
'Ш' => 'S',
|
82
|
+
'Š' => 'S',
|
83
|
+
'đ' => 'dj',
|
84
|
+
'ђ' => 'dj',
|
85
|
+
'Ђ' => 'DJ',
|
86
|
+
'Đ' => 'DJ',
|
87
|
+
'џ' => 'dz',
|
88
|
+
'Џ' => 'DZ')
|
89
|
+
|
90
|
+
def latin
|
91
|
+
self.class.recode(self.dup, @@latin_map)
|
92
|
+
end
|
93
|
+
|
94
|
+
def latin!
|
95
|
+
self.class.recode(self, @@latin_map)
|
96
|
+
end
|
97
|
+
|
98
|
+
def ascii_latin
|
99
|
+
self.class.recode(self.dup, @@ascii_latin_map)
|
100
|
+
end
|
101
|
+
|
102
|
+
def ascii_latin!
|
103
|
+
self.class.recode(self, @@ascii_latin_map)
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.abeceda
|
107
|
+
@@abeceda ||= %w(A B C Č Ć D DŽ Đ E F G H I J K L LJ M N NJ O P R S Š T U V Z Ž)
|
108
|
+
end
|
109
|
+
|
110
|
+
def self.abeceda
|
111
|
+
@@abeceda ||= %w(A B C Č Ć D DŽ Đ E F G H I J K L LJ M N NJ O P R S Š T U V Z Ž)
|
112
|
+
end
|
113
|
+
|
114
|
+
def slugify(separator = '-', return_separator_if_empty = true)
|
115
|
+
s = parameterize(self.ascii_latin, separator)
|
116
|
+
s.empty? && return_separator_if_empty ? separator : s
|
117
|
+
end
|
118
|
+
|
119
|
+
private
|
120
|
+
|
121
|
+
# NOTE: slighlty modified after porting from ActiveSupport
|
122
|
+
#
|
123
|
+
# Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
|
124
|
+
#
|
125
|
+
# ==== Examples
|
126
|
+
#
|
127
|
+
# @person = Person.find(1)
|
128
|
+
# # => #<Person id: 1, name: "Donald E. Knuth">
|
129
|
+
#
|
130
|
+
# <%= link_to(@person.name, person_path(@person)) %>
|
131
|
+
# # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
|
132
|
+
#
|
133
|
+
def parameterize(string, sep = '-')
|
134
|
+
# nil is same as blank
|
135
|
+
sep = sep || ''
|
136
|
+
parameterized_string = string.gsub(/[^a-z0-9\-_\+]+/i, sep)
|
137
|
+
# Turn unwanted chars into the seperator
|
138
|
+
unless sep.empty?
|
139
|
+
re_sep = Regexp.escape(sep)
|
140
|
+
# No more than one of the separator in a row.
|
141
|
+
parameterized_string.gsub!(/#{re_sep}{2,}/, sep)
|
142
|
+
# Remove leading/trailing separator.
|
143
|
+
parameterized_string.gsub!(/^#{re_sep}|#{re_sep}$/i, '')
|
144
|
+
end
|
145
|
+
parameterized_string.downcase
|
146
|
+
end
|
147
|
+
|
148
|
+
def self.recode(str, mapping)
|
149
|
+
str.gsub!(/.+?/) do |chr|
|
150
|
+
mapping[chr] || chr
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class TestStringExt < Test::Unit::TestCase
|
4
|
+
def test_latin
|
5
|
+
# don't change argument
|
6
|
+
str = "ЊЕГОШ"
|
7
|
+
assert_equal("NJEGOŠ", str.latin)
|
8
|
+
assert_equal("ЊЕГОШ", str)
|
9
|
+
|
10
|
+
# plain
|
11
|
+
assert_equal("NJEGOŠ", "ЊЕГОШ".latin)
|
12
|
+
assert_equal("Šaban Šaulić", "Шабан Шаулић".latin)
|
13
|
+
assert_equal("elektrodistribucija", "електродистрибуција".latin)
|
14
|
+
assert_equal("Đurđevak", "Ђурђевак".latin)
|
15
|
+
|
16
|
+
# non_convertive_characters
|
17
|
+
assert_equal("Latinica", "Latinica".latin)
|
18
|
+
assert_equal('[]2#}(,.);2@$"', '[]2#}(,.);2@$"'.latin)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_latin!
|
22
|
+
str = "ноћ"
|
23
|
+
assert_equal("noć", str.latin!)
|
24
|
+
assert_equal("noć", str)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_ascii_latin
|
28
|
+
str = "Железница Србије"
|
29
|
+
assert_equal("Zeleznica Srbije", str.ascii_latin)
|
30
|
+
assert_equal("Железница Србије", str)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_latin!
|
34
|
+
str = "ноћ"
|
35
|
+
assert_equal("noc", str.ascii_latin!)
|
36
|
+
assert_equal("noc", str)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_abeceda
|
40
|
+
assert_equal(30, String.abeceda.size)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_slugify
|
44
|
+
assert_equal("zeleznica-srbije", "Железница Србије".slugify)
|
45
|
+
assert_equal("kako-je", "Kako je?".slugify)
|
46
|
+
assert_equal("mis-zaba", "miš,žaba".slugify)
|
47
|
+
assert_equal("mis-zaba", "миш, жаба".slugify)
|
48
|
+
assert_equal("agencija", " aGenCIJa ".slugify)
|
49
|
+
assert_equal("vamo-tamo", "vamo-tamo".slugify)
|
50
|
+
assert_equal("vamo-tamo", "vamo tamo".slugify)
|
51
|
+
assert_equal("pokvarenspejs", "Pokvaren Spejs".slugify(nil))
|
52
|
+
assert_equal("abcdeavioni", "a b c d e avioni".slugify(''))
|
53
|
+
assert_equal("-", "び諸芸や奇抜な衣装".slugify)
|
54
|
+
assert_equal("-", "び諸芸や奇 抜な衣 装 び諸芸や 奇抜な衣装".slugify)
|
55
|
+
assert_equal("", "び諸芸や奇抜な衣装".slugify('-', false))
|
56
|
+
assert_equal("-", "び諸芸や奇抜な衣装".slugify('-', true))
|
57
|
+
assert_equal("+", "び諸芸や奇抜な衣装 び諸芸や奇抜な衣装".slugify('+'))
|
58
|
+
assert_equal("ako-ako", 'ako, ako'.slugify)
|
59
|
+
assert_equal("ako-ako", 'ako,ako'.slugify)
|
60
|
+
assert_equal("ako-ako", 'ako ako'.slugify)
|
61
|
+
end
|
62
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: srbovanje
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dejan Simic
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-14 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: "Ruby gem za transliteraciju \xC4\x87irilice na latinicu i tzv. ASCII latinicu"
|
17
|
+
email: desimic@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- README.rdoc
|
26
|
+
- VERSION.yml
|
27
|
+
- lib/srbovanje/string_ext.rb
|
28
|
+
- lib/srbovanje.rb
|
29
|
+
- test/string_ext_test.rb
|
30
|
+
- test/test_helper.rb
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: http://github.com/dejan/srbovanje
|
33
|
+
licenses: []
|
34
|
+
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options:
|
37
|
+
- --inline-source
|
38
|
+
- --charset=UTF-8
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.3.5
|
57
|
+
signing_key:
|
58
|
+
specification_version: 2
|
59
|
+
summary: "Ruby gem za transliteraciju \xC4\x87irilice na latinicu i tzv. ASCII latinicu"
|
60
|
+
test_files: []
|
61
|
+
|