slugg 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +3 -0
- data/.travis.yml +13 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +63 -0
- data/Rakefile +1 -0
- data/lib/slugg.rb +13 -0
- data/lib/slugg/chars.rb +72 -0
- data/lib/slugg/sanitizer.rb +25 -0
- data/lib/slugg/version.rb +3 -0
- data/slugg.gemspec +20 -0
- data/spec/slugg/sanitizer_spec.rb +70 -0
- data/spec/slugg_spec.rb +26 -0
- data/spec/spec_helper.rb +1 -0
- metadata +63 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 27f8a91d281fb6ba75bcb503d666a86dd39c84f4
|
4
|
+
data.tar.gz: 37540b10705932b43dfad8f7921d039e8551e85f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 34cd62b2980a64d106f080139bde8d0cf81d93c95abf2a89e0d9847300d80032555af5436ed7d015cb44cdab237c1c5bfdaaf5f03027c1c26fde7c40c986d4b3
|
7
|
+
data.tar.gz: e23e7722ae46335027eaf86811f19d3207adf8a40eba3f7689d2b7572031a422da22706f47ab0e675c7977a16b24c76f57e40b0b7b06ff3862a166b871427bd1
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Yuri Artemev
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Slugg
|
2
|
+
|
3
|
+
Make safety urls from strings
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'slugg'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install slugg
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require 'slug'
|
23
|
+
|
24
|
+
Slugg.new "generate url, please"
|
25
|
+
# => "generate-url-please"
|
26
|
+
|
27
|
+
# Slugg.make is alias to Slugg.new
|
28
|
+
Slugg.make "generate url, please"
|
29
|
+
# => "generate-url-please"
|
30
|
+
|
31
|
+
# you may provide some options
|
32
|
+
Slugg.make "generate url, please", separator: "_"
|
33
|
+
# => "generate_url_please"
|
34
|
+
|
35
|
+
Slugg.make "generate url, please", stripper: "please"
|
36
|
+
# => "generate-url"
|
37
|
+
|
38
|
+
Slugg.make "strip 100$", stripper: /\d/
|
39
|
+
# => "strip"
|
40
|
+
|
41
|
+
# and you may use instance
|
42
|
+
slugg = Slugg::Sanitizer.new separator: "_"
|
43
|
+
slugg.safe "make url"
|
44
|
+
# => "make_url"
|
45
|
+
|
46
|
+
slugg.options[:separator] = "-"
|
47
|
+
slugg.safe "make url"
|
48
|
+
# => "make-url"
|
49
|
+
|
50
|
+
# options
|
51
|
+
{
|
52
|
+
separator: '-', # define custom separator
|
53
|
+
stripper: '' # strip text, allows String and Regexp
|
54
|
+
}
|
55
|
+
```
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
1. Fork it
|
60
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
61
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
62
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
63
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/slugg.rb
ADDED
data/lib/slugg/chars.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Slugg
|
3
|
+
CHARS = {
|
4
|
+
# latin
|
5
|
+
'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A',
|
6
|
+
'Å' => 'A', 'Æ' => 'AE', 'Ç' => 'C', 'È' => 'E', 'É' => 'E',
|
7
|
+
'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I',
|
8
|
+
'Ï' => 'I', 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O',
|
9
|
+
'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ő' => 'O', 'Ø' => 'O',
|
10
|
+
'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ű' => 'U',
|
11
|
+
'Ý' => 'Y', 'Þ' => 'TH', 'ß' => 'ss', 'à' => 'a', 'á' => 'a',
|
12
|
+
'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'ae',
|
13
|
+
'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e',
|
14
|
+
'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'd',
|
15
|
+
'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o',
|
16
|
+
'ö' => 'o', 'ő' => 'o', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u',
|
17
|
+
'û' => 'u', 'ü' => 'u', 'ű' => 'u', 'ý' => 'y', 'þ' => 'th',
|
18
|
+
'ÿ' => 'y', 'ẞ' => 'SS',
|
19
|
+
# greek
|
20
|
+
'α' => 'a', 'β' => 'b', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e',
|
21
|
+
'ζ' => 'z', 'η' => 'h', 'θ' => '8', 'ι' => 'i', 'κ' => 'k',
|
22
|
+
'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => '3', 'ο' => 'o',
|
23
|
+
'π' => 'p', 'ρ' => 'r', 'σ' => 's', 'τ' => 't', 'υ' => 'y',
|
24
|
+
'φ' => 'f', 'χ' => 'x', 'ψ' => 'ps', 'ω' => 'w', 'ά' => 'a',
|
25
|
+
'έ' => 'e', 'ί' => 'i', 'ό' => 'o', 'ύ' => 'y', 'ή' => 'h',
|
26
|
+
'ώ' => 'w', 'ς' => 's', 'ϊ' => 'i', 'ΰ' => 'y', 'ϋ' => 'y',
|
27
|
+
'ΐ' => 'i',
|
28
|
+
'Α' => 'A', 'Β' => 'B', 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E',
|
29
|
+
'Ζ' => 'Z', 'Η' => 'H', 'Θ' => '8', 'Ι' => 'I', 'Κ' => 'K',
|
30
|
+
'Λ' => 'L', 'Μ' => 'M', 'Ν' => 'N', 'Ξ' => '3', 'Ο' => 'O',
|
31
|
+
'Π' => 'P', 'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'Y',
|
32
|
+
'Φ' => 'F', 'Χ' => 'X', 'Ψ' => 'PS', 'Ω' => 'W', 'Ά' => 'A',
|
33
|
+
'Έ' => 'E', 'Ί' => 'I', 'Ό' => 'O', 'Ύ' => 'Y', 'Ή' => 'H',
|
34
|
+
'Ώ' => 'W', 'Ϊ' => 'I', 'Ϋ' => 'Y',
|
35
|
+
#turkish
|
36
|
+
'ş' => 's', 'Ş' => 'S', 'ı' => 'i', 'İ' => 'I', 'ğ' => 'g',
|
37
|
+
'Ğ' => 'G',
|
38
|
+
# russian
|
39
|
+
'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
|
40
|
+
'е' => 'e', 'ё' => 'yo', 'ж' => 'zh', 'з' => 'z', 'и' => 'i',
|
41
|
+
'й' => 'j', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n',
|
42
|
+
'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't',
|
43
|
+
'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch',
|
44
|
+
'ш' => 'sh', 'щ' => 'sh', 'ъ' => 'u', 'ы' => 'y', 'ь' => '',
|
45
|
+
'э' => 'e', 'ю' => 'yu', 'я' => 'ya',
|
46
|
+
'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
|
47
|
+
'Е' => 'E', 'Ё' => 'Yo', 'Ж' => 'Zh', 'З' => 'Z', 'И' => 'I',
|
48
|
+
'Й' => 'J', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N',
|
49
|
+
'О' => 'O', 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T',
|
50
|
+
'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Ch',
|
51
|
+
'Ш' => 'Sh', 'Щ' => 'Sh', 'Ъ' => 'U', 'Ы' => 'Y', 'Ь' => '',
|
52
|
+
'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya',
|
53
|
+
# ukranian
|
54
|
+
'Є' => 'Ye', 'І' => 'I', 'Ї' => 'Yi', 'Ґ' => 'G', 'є' => 'ye',
|
55
|
+
'і' => 'i', 'ї' => 'yi', 'ґ' => 'g',
|
56
|
+
# czech
|
57
|
+
'č' => 'c', 'ď' => 'd', 'ě' => 'e', 'ň' => 'n', 'ř' => 'r',
|
58
|
+
'š' => 's', 'ť' => 't', 'ů' => 'u', 'ž' => 'z', 'Č' => 'C',
|
59
|
+
'Ď' => 'D', 'Ě' => 'E', 'Ň' => 'N', 'Ř' => 'R', 'Š' => 'S',
|
60
|
+
'Ť' => 'T', 'Ů' => 'U', 'Ž' => 'Z',
|
61
|
+
# polish
|
62
|
+
'ą' => 'a', 'ć' => 'c', 'ę' => 'e', 'ł' => 'l', 'ń' => 'n',
|
63
|
+
'ś' => 's', 'ź' => 'z', 'ż' => 'z', 'Ą' => 'A', 'Ć' => 'C',
|
64
|
+
'Ę' => 'e', 'Ł' => 'L', 'Ń' => 'N', 'Ś' => 'S', 'Ź' => 'Z',
|
65
|
+
'Ż' => 'Z',
|
66
|
+
# latvian
|
67
|
+
'ā' => 'a', 'ē' => 'e', 'ģ' => 'g', 'ī' => 'i', 'ķ' => 'k',
|
68
|
+
'ļ' => 'l', 'ņ' => 'n', 'ū' => 'u', 'Ā' => 'A', 'Ē' => 'E',
|
69
|
+
'Ģ' => 'G', 'Ī' => 'i', 'Ķ' => 'k', 'Ļ' => 'L', 'Ņ' => 'N',
|
70
|
+
'Ū' => 'u'
|
71
|
+
}
|
72
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Slugg
|
2
|
+
class Sanitizer
|
3
|
+
attr_accessor :options
|
4
|
+
|
5
|
+
def initialize options = {}
|
6
|
+
@options = {
|
7
|
+
:separator => "-",
|
8
|
+
:stripper => ""
|
9
|
+
}.merge options
|
10
|
+
end
|
11
|
+
|
12
|
+
def safe string
|
13
|
+
string
|
14
|
+
.split('')
|
15
|
+
.map { |c| c = Slugg::CHARS.include?(c) ? Slugg::CHARS[c] : c }
|
16
|
+
.join
|
17
|
+
.downcase
|
18
|
+
.gsub(@options[:stripper], '')
|
19
|
+
.gsub(/[\W|_]+/, @options[:separator])
|
20
|
+
.gsub(%r"^#{@options[:separator]}+", '')
|
21
|
+
.gsub(%r"#{@options[:separator]}+$", '')
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/slugg.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'slugg/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "slugg"
|
8
|
+
spec.version = Slugg::VERSION
|
9
|
+
spec.authors = ["Yuri Artemev"]
|
10
|
+
spec.email = ["i@artemeff.com"]
|
11
|
+
spec.description = %q{Make safety urls from strings}
|
12
|
+
spec.summary = %q{Generates safe urls from strings}
|
13
|
+
spec.homepage = "https://github.com/artemeff/slugg"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Slugg::Sanitizer do
|
5
|
+
describe ".new" do
|
6
|
+
let(:options) do
|
7
|
+
described_class.new(separator: "_", stripper: 'a').options
|
8
|
+
end
|
9
|
+
|
10
|
+
it "set separator" do
|
11
|
+
options[:separator].should eq "_"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "set custom stripper" do
|
15
|
+
options[:stripper].should eq 'a'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#safe" do
|
20
|
+
context "with default options" do
|
21
|
+
let(:safe) do
|
22
|
+
-> (string) { described_class.new.safe string }
|
23
|
+
end
|
24
|
+
|
25
|
+
it "replace with separator" do
|
26
|
+
safe.("string with spaces").should eq "string-with-spaces"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "downcase string" do
|
30
|
+
safe.("STRRRING").should eq "strrring"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "replace non-english chars" do
|
34
|
+
safe.("кто вообще это читает?").should eq "kto-voobshe-eto-chitaet"
|
35
|
+
end
|
36
|
+
|
37
|
+
it "remove separator from left and right" do
|
38
|
+
safe.(" much spaces ! ").should eq "much-spaces"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "with custom separator" do
|
43
|
+
let(:safe) do
|
44
|
+
-> (string, separator) do
|
45
|
+
described_class.new(separator: separator).safe string
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
it "replace with separator '_'" do
|
50
|
+
safe.("string with spaces", "_").should eq "string_with_spaces"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "with custom stripper" do
|
55
|
+
let(:safe) do
|
56
|
+
-> (string, stripper) do
|
57
|
+
described_class.new(stripper: stripper).safe string
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
it "strips all 'the'" do
|
62
|
+
safe.("strip the string", "the").should eq "strip-string"
|
63
|
+
end
|
64
|
+
|
65
|
+
it "strips all /\\d/" do
|
66
|
+
safe.("strip 123 string", /\d/).should eq "strip-string"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/spec/slugg_spec.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Slugg do
|
4
|
+
context "VERSION" do
|
5
|
+
subject { Slugg::VERSION }
|
6
|
+
it { should be_a String }
|
7
|
+
end
|
8
|
+
|
9
|
+
shared_examples "alias" do |method, options|
|
10
|
+
it "is alias to Sanitizer.safe" do
|
11
|
+
expect(Slugg.send(method.to_sym, "str")).to be_a String
|
12
|
+
end
|
13
|
+
|
14
|
+
it "may assign options" do
|
15
|
+
expect(Slugg.send(method.to_sym, "str", options)).to be_a String
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe ".new" do
|
20
|
+
include_examples "alias", "new", { separator: '_' }
|
21
|
+
end
|
22
|
+
|
23
|
+
describe ".make" do
|
24
|
+
include_examples "alias", "make", { separator: '_' }
|
25
|
+
end
|
26
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'slugg'
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: slugg
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuri Artemev
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-06-22 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Make safety urls from strings
|
14
|
+
email:
|
15
|
+
- i@artemeff.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- .gitignore
|
21
|
+
- .rspec
|
22
|
+
- .travis.yml
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/slugg.rb
|
28
|
+
- lib/slugg/chars.rb
|
29
|
+
- lib/slugg/sanitizer.rb
|
30
|
+
- lib/slugg/version.rb
|
31
|
+
- slugg.gemspec
|
32
|
+
- spec/slugg/sanitizer_spec.rb
|
33
|
+
- spec/slugg_spec.rb
|
34
|
+
- spec/spec_helper.rb
|
35
|
+
homepage: https://github.com/artemeff/slugg
|
36
|
+
licenses:
|
37
|
+
- MIT
|
38
|
+
metadata: {}
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 2.0.3
|
56
|
+
signing_key:
|
57
|
+
specification_version: 4
|
58
|
+
summary: Generates safe urls from strings
|
59
|
+
test_files:
|
60
|
+
- spec/slugg/sanitizer_spec.rb
|
61
|
+
- spec/slugg_spec.rb
|
62
|
+
- spec/spec_helper.rb
|
63
|
+
has_rdoc:
|