the_string_to_slug 1.1 → 1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +45 -13
- data/lib/the_string_to_slug/version.rb +1 -1
- data/lib/the_string_to_slug.rb +22 -8
- data/spec/dummy_app/spec/helpers/string.rb +43 -21
- data/the_string_to_slug.gemspec +2 -2
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 05c1fda589a9fd82d7b6ca75d4d3143c31bbd986
|
4
|
+
data.tar.gz: 4a64890bbc945bd88981a75ef57541cfe9b0f885
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2e88afcd87802f6556925d7f0564a9ac883d4201feb1182798e75255df314ff2aad34e9aa117a340e0710ff8975ba2e133b7e079374c3ae85339aa35a5c0f1e7
|
7
|
+
data.tar.gz: 423d241fb30aee3902460fd36efbd460453af2538b71541f1a3dff751a537d81805a375ef58866d0921d0ea20d77afd9d4c7547bdd2b5eceb50f809071d0c40c
|
data/README.md
CHANGED
@@ -1,19 +1,31 @@
|
|
1
1
|
### TheStringToSlug
|
2
2
|
|
3
|
-
Convert
|
3
|
+
Convert strings and symbols to slug param
|
4
4
|
|
5
|
-
|
5
|
+
Transliteration + Parameterization for slugs building
|
6
6
|
|
7
7
|
#### Install
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem "the_string_to_slug", "~>
|
10
|
+
gem "the_string_to_slug", "~> 1.2"
|
11
11
|
```
|
12
12
|
|
13
13
|
<a href="http://rubygems.org/gems/the_string_to_slug">RubyGems/the_string_to_slug</a>
|
14
14
|
|
15
15
|
#### Using
|
16
16
|
|
17
|
+
For russian transliteration by default
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
I18n.enforce_available_locales = true
|
21
|
+
|
22
|
+
module DummyApp
|
23
|
+
class Application < Rails::Application
|
24
|
+
config.i18n.default_locale = :ru
|
25
|
+
end
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
17
29
|
```ruby
|
18
30
|
"Привет Мир! Hello world!".to_slug_param
|
19
31
|
# => "privet-mir-hello-world"
|
@@ -39,21 +51,41 @@ String.slugged_filename("/доки/dir/тест/документ.doc") #=> "doku
|
|
39
51
|
For full file path:
|
40
52
|
|
41
53
|
```ruby
|
42
|
-
"/доки/dir/тест/документ.doc".
|
43
|
-
String.
|
54
|
+
"/доки/dir/тест/документ.doc".slugged_filepath #=> "/доки/dir/тест/dokument.doc"
|
55
|
+
String.slugged_filepath("/доки/dir/тест/документ.doc") #=> "/доки/dir/тест/dokument.doc"
|
44
56
|
```
|
45
57
|
|
46
58
|
Params
|
47
59
|
|
48
60
|
```ruby
|
49
|
-
"Документ.doc".to_slug_param(locale: :
|
50
|
-
# => "doc"
|
51
|
-
|
52
|
-
"Документ.doc".to_slug_param(locale: :en)
|
53
|
-
# => "dokument-doc"
|
61
|
+
"Документ.doc".to_slug_param(locale: :ru) # => "dokument-doc"
|
62
|
+
"Документ.doc".to_slug_param(locale: :en) # => "doc"
|
54
63
|
```
|
55
64
|
|
56
65
|
```ruby
|
57
|
-
"Документ.doc".to_slug_param(
|
58
|
-
# => "
|
59
|
-
```
|
66
|
+
"Документ.doc".to_slug_param(sep: '_', locale: :ru) # => "dokument_doc"
|
67
|
+
"Документ.doc".to_slug_param(sep: '_', locale: :en) # => "doc"
|
68
|
+
```
|
69
|
+
|
70
|
+
### MIT-LICENSE
|
71
|
+
|
72
|
+
##### Copyright (c) 2013-2014 [Ilya N.Zykin]
|
73
|
+
|
74
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
75
|
+
a copy of this software and associated documentation files (the
|
76
|
+
"Software"), to deal in the Software without restriction, including
|
77
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
78
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
79
|
+
permit persons to whom the Software is furnished to do so, subject to
|
80
|
+
the following conditions:
|
81
|
+
|
82
|
+
The above copyright notice and this permission notice shall be
|
83
|
+
included in all copies or substantial portions of the Software.
|
84
|
+
|
85
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
86
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
87
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
88
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
89
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
90
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
91
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/the_string_to_slug.rb
CHANGED
@@ -12,8 +12,8 @@ class String
|
|
12
12
|
self.class.slugged_filename(self, opts)
|
13
13
|
end
|
14
14
|
|
15
|
-
def
|
16
|
-
self.class.
|
15
|
+
def slugged_filepath opts = {}
|
16
|
+
self.class.slugged_filepath(self, opts)
|
17
17
|
end
|
18
18
|
|
19
19
|
# -----------------------------------
|
@@ -21,13 +21,13 @@ class String
|
|
21
21
|
# -----------------------------------
|
22
22
|
class << self
|
23
23
|
def to_slug_param str, opts = {}
|
24
|
-
|
24
|
+
sep = opts.delete(:sep) || '-'
|
25
25
|
str = str.gsub(/\-{2,}/, '-').mb_chars
|
26
26
|
str = I18n::transliterate(str, opts)
|
27
|
-
.gsub('_',
|
28
|
-
.gsub('-',
|
29
|
-
.parameterize(
|
30
|
-
.
|
27
|
+
.gsub('_', sep)
|
28
|
+
.gsub('-', sep)
|
29
|
+
.parameterize(sep)
|
30
|
+
.to_s
|
31
31
|
end
|
32
32
|
|
33
33
|
def file_ext file_name, opts = {}
|
@@ -49,9 +49,23 @@ class String
|
|
49
49
|
[fname, ext].join('.')
|
50
50
|
end
|
51
51
|
|
52
|
-
def
|
52
|
+
def slugged_filepath file_full_path, opts = {}
|
53
53
|
fname = slugged_filename file_full_path, opts
|
54
54
|
file_full_path.split('/')[0...-1].push(fname).join '/'
|
55
55
|
end
|
56
56
|
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class Symbol
|
60
|
+
def to_slug_param opts = {}
|
61
|
+
String.to_slug_param(self.to_s, opts)
|
62
|
+
end
|
63
|
+
|
64
|
+
def slugged_filename opts = {}
|
65
|
+
String.slugged_filename(self.to_s, opts)
|
66
|
+
end
|
67
|
+
|
68
|
+
def slugged_filepath opts = {}
|
69
|
+
String.slugged_file(self.to_s, opts)
|
70
|
+
end
|
57
71
|
end
|
@@ -2,6 +2,22 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
4
|
describe 'StringToSlug' do
|
5
|
+
context "readme" do
|
6
|
+
it "testcase 1" do
|
7
|
+
"Документ.doc".to_slug_param(locale: :ru).should eq "dokument-doc"
|
8
|
+
"Документ.doc".to_slug_param(locale: :en).should eq "doc"
|
9
|
+
|
10
|
+
:"Документ.doc".to_slug_param(locale: :ru).should eq "dokument-doc"
|
11
|
+
:"Документ.doc".to_slug_param(locale: :en).should eq "doc"
|
12
|
+
|
13
|
+
"Документ.doc".to_slug_param(sep: '_', locale: :ru).should eq "dokument_doc"
|
14
|
+
"Документ.doc".to_slug_param(sep: '_', locale: :en).should eq "doc"
|
15
|
+
|
16
|
+
:"Документ.doc".to_slug_param(sep: '_', locale: :ru).should eq "dokument_doc"
|
17
|
+
:"Документ.doc".to_slug_param(sep: '_', locale: :en).should eq "doc"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
5
21
|
context 'String tests' do
|
6
22
|
it 'should be true' do
|
7
23
|
"Привет Мир! Hello world!".to_slug_param.should eq "privet-mir-hello-world"
|
@@ -17,6 +33,7 @@ describe 'StringToSlug' do
|
|
17
33
|
context 'Filename test' do
|
18
34
|
it 'should be true' do
|
19
35
|
"/doc/dir/test/document.doc".to_slug_param.should eq "doc-dir-test-document-doc"
|
36
|
+
|
20
37
|
"/doc/dir/test/document.doc".slugged_filename.should eq "document.doc"
|
21
38
|
"/доки/dir/тест/документ.doc".slugged_filename.should eq "dokument.doc"
|
22
39
|
"/доки/dir/тест/документ".slugged_filename.should eq "dokument"
|
@@ -39,33 +56,33 @@ describe 'StringToSlug' do
|
|
39
56
|
|
40
57
|
context 'Full path to file' do
|
41
58
|
it 'should be true' do
|
42
|
-
"/doc/dir/test/document.doc".
|
43
|
-
"/доки/dir/тест/документ.doc".
|
44
|
-
"/доки/dir/тест/документ".
|
45
|
-
"/доки/dir/тест/доку мент".
|
59
|
+
"/doc/dir/test/document.doc".slugged_filepath.should eq "/doc/dir/test/document.doc"
|
60
|
+
"/доки/dir/тест/документ.doc".slugged_filepath.should eq "/доки/dir/тест/dokument.doc"
|
61
|
+
"/доки/dir/тест/документ".slugged_filepath.should eq "/доки/dir/тест/dokument"
|
62
|
+
"/доки/dir/тест/доку мент".slugged_filepath.should eq "/доки/dir/тест/doku-ment"
|
46
63
|
|
47
|
-
String.
|
64
|
+
String.slugged_filepath("/доки/dir/тест/доку мент").should eq "/доки/dir/тест/doku-ment"
|
48
65
|
end
|
49
66
|
|
50
67
|
it 'When wrong translation' do
|
51
|
-
"/doc/dir/test/document.doc".
|
52
|
-
"/доки/dir/тест/документ.doc".
|
68
|
+
"/doc/dir/test/document.doc".slugged_filepath(locale: :en).should eq "/doc/dir/test/document.doc"
|
69
|
+
"/доки/dir/тест/документ.doc".slugged_filepath(locale: :en).should eq "/доки/dir/тест/.doc"
|
53
70
|
|
54
|
-
"/доки/dir/тест/документ".
|
55
|
-
"/доки/dir/тест/доку мент".
|
71
|
+
"/доки/dir/тест/документ".slugged_filepath(locale: :en).should eq "/доки/dir/тест/"
|
72
|
+
"/доки/dir/тест/доку мент".slugged_filepath(locale: :en).should eq "/доки/dir/тест/"
|
56
73
|
|
57
|
-
String.
|
74
|
+
String.slugged_filepath("/доки/dir/тест/доку мент", locale: :en).should eq "/доки/dir/тест/"
|
58
75
|
end
|
59
76
|
end
|
60
77
|
|
61
|
-
context '
|
78
|
+
context 'sep' do
|
62
79
|
it 'should be true' do
|
63
|
-
"Привет Мир! Hello world!".to_slug_param(
|
64
|
-
"Документ.doc".to_slug_param(
|
80
|
+
"Привет Мир! Hello world!".to_slug_param(sep: '_').should eq "privet_mir_hello_world"
|
81
|
+
"Документ.doc".to_slug_param(sep: '_').should eq "dokument_doc"
|
65
82
|
|
66
|
-
"/доки/dir/тест/документ".
|
67
|
-
"/доки/dir/тест/доку мент".
|
68
|
-
"/доки/dir/тест/доку мент".slugged_filename(
|
83
|
+
"/доки/dir/тест/документ".slugged_filepath(sep: '_').should eq "/доки/dir/тест/dokument"
|
84
|
+
"/доки/dir/тест/доку мент".slugged_filepath(sep: '_').should eq "/доки/dir/тест/doku_ment"
|
85
|
+
"/доки/dir/тест/доку мент".slugged_filename(sep: '_').should eq "doku_ment"
|
69
86
|
end
|
70
87
|
end
|
71
88
|
|
@@ -80,20 +97,25 @@ describe 'StringToSlug' do
|
|
80
97
|
end
|
81
98
|
|
82
99
|
it "should works" do
|
83
|
-
"HELLO---WorlD".to_slug_param(
|
100
|
+
"HELLO---WorlD".to_slug_param(sep: '_').should eq 'hello_world'
|
84
101
|
end
|
85
102
|
|
86
103
|
it "should works" do
|
87
104
|
str = "__...HELLO-___--+++--WorlD----__&&***...__.---"
|
88
105
|
|
89
106
|
str.to_slug_param.should eq 'hello-world'
|
90
|
-
str.to_slug_param(
|
91
|
-
str.to_slug_param(
|
107
|
+
str.to_slug_param(sep: '_').should eq 'hello_world'
|
108
|
+
str.to_slug_param(sep: '+').should eq 'hello+world'
|
92
109
|
end
|
93
110
|
|
94
111
|
it "should works" do
|
95
112
|
str = "Ilya zykin aka Killich, $$$ aka the-teacher"
|
96
|
-
str.to_slug_param(
|
113
|
+
str.to_slug_param(sep: '+').should eq "ilya+zykin+aka+killich+aka+the+teacher"
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should works" do
|
117
|
+
str = "Илья Николаевич, прекратите кодить по выходным!".to_sym
|
118
|
+
str.to_slug_param.should eq "ilya-nikolaevich-prekratite-kodit-po-vyhodnym"
|
97
119
|
end
|
98
120
|
end
|
99
121
|
|
@@ -115,7 +137,7 @@ describe 'StringToSlug' do
|
|
115
137
|
module Admin; end
|
116
138
|
class Admin::PagesController < ApplicationController; end
|
117
139
|
ctrl = Admin::PagesController.new
|
118
|
-
params = {
|
140
|
+
params = { sep: '_' }
|
119
141
|
ctrl.controller_path.to_slug_param(params).should eq 'admin_pages'
|
120
142
|
end
|
121
143
|
end
|
data/the_string_to_slug.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = TheStringToSlug::VERSION
|
9
9
|
spec.authors = ['Ilya N. Zykin']
|
10
10
|
spec.email = ['zykin-ilya@ya.ru']
|
11
|
-
spec.description = %q{ Convert
|
12
|
-
spec.summary = %q{
|
11
|
+
spec.description = %q{ Convert strings and symbols to slug param }
|
12
|
+
spec.summary = %q{ Transliteration + Parameterization for slugs building }
|
13
13
|
spec.homepage = 'https://github.com/the-teacher/the_string_to_slug'
|
14
14
|
spec.license = 'MIT'
|
15
15
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: the_string_to_slug
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '1.
|
4
|
+
version: '1.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ilya N. Zykin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails-i18n
|
@@ -52,7 +52,7 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
description: " Convert
|
55
|
+
description: " Convert strings and symbols to slug param "
|
56
56
|
email:
|
57
57
|
- zykin-ilya@ya.ru
|
58
58
|
executables: []
|
@@ -146,7 +146,7 @@ rubyforge_project:
|
|
146
146
|
rubygems_version: 2.1.11
|
147
147
|
signing_key:
|
148
148
|
specification_version: 4
|
149
|
-
summary:
|
149
|
+
summary: Transliteration + Parameterization for slugs building
|
150
150
|
test_files:
|
151
151
|
- spec/dummy_app/.gitignore
|
152
152
|
- spec/dummy_app/.rspec
|