slug_less 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +9 -0
- data/lib/slug_less.rb +3 -0
- data/lib/slug_less/encoding_approximations/latin_diacritics1.rb +29 -0
- data/lib/slug_less/to_slug.rb +46 -0
- data/lib/slug_less/version.rb +3 -0
- data/slug_less.gemspec +24 -0
- data/test/test_helper.rb +4 -0
- data/test/to_slug_test.rb +101 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b9ff9c3f4e32a8804da8bd90c6a76ab53535d132
|
4
|
+
data.tar.gz: 48a1ddeb9251d925be70cdfb1bf56bb8e336e0af
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ec33510c4bbe90615e94f45086217ca88a3a594a1b9d95985e03b3c0c96d779969cc22872a27ea10889f11d83390a538738975520ed6a6b35e353d62e3647b00
|
7
|
+
data.tar.gz: 2a9543cdd7f506cbb82e76cdc957f0f670f18795681e618c776289975639aa18e989c20bcdaa823c658bacbaabccac13240f0a87918863bfd532520984e7930c
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 René Maya
|
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,31 @@
|
|
1
|
+
# SlugLess
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'slug_less'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install slug_less
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it ( https://github.com/renemaya/slug_less/fork )
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/slug_less.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module SlugLess
|
2
|
+
|
3
|
+
LATIN_DIACRITICS1 = {"a" => ["\u{00C0}", "\u{00C1}", "\u{00C2}", "\u{00C3}",
|
4
|
+
"\u{00C4}", "\u{00C5}", "\u{00E0}",
|
5
|
+
"\u{00E1}", "\u{00E2}", "\u{00E3}", "\u{00E4}",
|
6
|
+
"\u{00E5}"
|
7
|
+
],
|
8
|
+
"e" => ["\u{00C8}", "\u{00C9}", "\u{00CA}", "\u{00CB}",
|
9
|
+
"\u{00E8}", "\u{00E9}", "\u{00EA}", "\u{00EB}"
|
10
|
+
],
|
11
|
+
"i" => ["\u{00CC}", "\u{00CD}", "\u{00CE}", "\u{00CF}",
|
12
|
+
"\u{00EC}", "\u{00ED}", "\u{00EE}", "\u{00EF}"
|
13
|
+
],
|
14
|
+
"o" => ["\u{00D2}", "\u{00D3}", "\u{00D4}", "\u{00D5}",
|
15
|
+
"\u{00D6}", "\u{00D8}", "\u{00F2}", "\u{00F3}",
|
16
|
+
"\u{00F4}", "\u{00F5}", "\u{00F6}", "\u{00F8}"
|
17
|
+
],
|
18
|
+
"u" => ["\u{00D9}", "\u{00DA}", "\u{00DB}", "\u{00DC}",
|
19
|
+
"\u{00F9}", "\u{00FA}", "\u{00FB}", "\u{00FC}"
|
20
|
+
],
|
21
|
+
"ae" => ["\u{00C6}", "\u{00E6}"],
|
22
|
+
"c" => ["\u{00C7}", "\u{00E7}"],
|
23
|
+
"d" => ["\u{00D0}", "\u{00F0}"],
|
24
|
+
"n" => ["\u{00D1}", "\u{00F1}"],
|
25
|
+
"y" => ["\u{00DD}", "\u{00FD}"],
|
26
|
+
"th" => ["\u{00DE}"],
|
27
|
+
"ss" => ["\u{00DF}"]
|
28
|
+
}.freeze
|
29
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module SlugLess
|
2
|
+
|
3
|
+
String.class_eval do
|
4
|
+
def to_slug
|
5
|
+
text = self.dup
|
6
|
+
transliterate(text)
|
7
|
+
sanitize(text)
|
8
|
+
remove_special_characters(text)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def transliterate(text)
|
14
|
+
# TODO: figure out how to make this so others can choose which
|
15
|
+
# dictionaries to load or even load custom ones.
|
16
|
+
substitute_characters(text)
|
17
|
+
end
|
18
|
+
|
19
|
+
def substitute_characters(text, char_table = SlugLess::LATIN_DIACRITICS1)
|
20
|
+
characters_to_change = char_table.values
|
21
|
+
slug_friendly_characters = char_table.keys
|
22
|
+
|
23
|
+
characters_to_change.each_with_index do |set, idx|
|
24
|
+
text.chars.each do |char|
|
25
|
+
text.gsub!(char, slug_friendly_characters[idx]) if set.include?(char)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
text
|
30
|
+
end
|
31
|
+
|
32
|
+
def sanitize(text)
|
33
|
+
text.downcase!
|
34
|
+
text.gsub! ' ','-'
|
35
|
+
text.gsub!(/-+/, '-')
|
36
|
+
text.gsub!(/\A-|-\z/, '')
|
37
|
+
text
|
38
|
+
end
|
39
|
+
|
40
|
+
def remove_special_characters(text)
|
41
|
+
text.gsub!(/[^a-z0-9-]/, '')
|
42
|
+
text
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
data/slug_less.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'slug_less/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "slug_less"
|
8
|
+
spec.version = SlugLess::VERSION
|
9
|
+
spec.authors = ["René Maya"]
|
10
|
+
spec.email = ["rene.maya1@gmail.com"]
|
11
|
+
spec.summary = %q{Generate slugs using less code}
|
12
|
+
spec.description = %q{Simple gem that aims at doing one thing: generate slugs with as few lines of clean code as possible.}
|
13
|
+
spec.homepage = "https://github.com/renemaya/slug_less"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "minitest", "~> 5.8"
|
24
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ToSlugTest < Minitest::Test
|
4
|
+
def test_lowercase_string
|
5
|
+
str = 'HeLLo'
|
6
|
+
assert_equal "hello", str.to_slug
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_strip_trailing_space
|
10
|
+
str = 'trailing space '
|
11
|
+
assert_equal 'trailing-space', str.to_slug
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_strip_leading_space
|
15
|
+
str = ' leading space'
|
16
|
+
assert_equal 'leading-space', str.to_slug
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_replace_whitespace_with_dashes
|
20
|
+
str = 'Hello World'
|
21
|
+
assert_equal 'hello-world', str.to_slug
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_keep_numbers_in_slug
|
25
|
+
str = "2015 Learning Ruby"
|
26
|
+
assert_equal '2015-learning-ruby', str.to_slug
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_strip_trailing_dashes
|
30
|
+
str = 'trailing dash-'
|
31
|
+
assert_equal 'trailing-dash', str.to_slug
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_strip_leading_dashes
|
35
|
+
str = '-leading dash'
|
36
|
+
assert_equal 'leading-dash', str.to_slug
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_replace_nspaces_with_one_dash
|
40
|
+
str = 'hola muNdo'
|
41
|
+
assert_equal 'hola-mundo', str.to_slug
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_no_modification_to_valid_slug_strings
|
45
|
+
str = 'a-b-c-d'
|
46
|
+
assert_equal 'a-b-c-d', str.to_slug
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_remove_double_dashes
|
50
|
+
str = 'two--dashes--in--between'
|
51
|
+
assert_equal 'two-dashes-in-between', str.to_slug
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_consider_acronyms_single_words
|
55
|
+
str = "N.Y.P.D. vs. N.S.A. vs. F.B.I."
|
56
|
+
assert_equal 'nypd-vs-nsa-vs-fbi', str.to_slug
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_no_dashes_for_apostrophes
|
60
|
+
str = "Thomas Jefferson's Papers"
|
61
|
+
assert_equal 'thomas-jeffersons-papers', str.to_slug
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_remove_special_characters
|
65
|
+
str = 'xyz!@#$%^&*•¶§∞¢89££¡¿()><?""\':;][]\.,/'
|
66
|
+
assert_equal 'xyz89', str.to_slug
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_remove_diacritics_as_prescribe_in_latin_diacritics1
|
70
|
+
diacritics = "À Á Â Ã Ä Å Æ " +
|
71
|
+
"à á â ã ä å æ " +
|
72
|
+
"Ç È É Ê Ë Ð Ñ " +
|
73
|
+
"ç è é ê ë ð ñ " +
|
74
|
+
"Ì Í Î Ï Ý " +
|
75
|
+
"ì í î ï ý " +
|
76
|
+
"Ò Ó Ô Õ Ö Ø " +
|
77
|
+
"ò ó ô õ ö ø " +
|
78
|
+
"Ù Ú Û Ü " +
|
79
|
+
"ù ú û ü " +
|
80
|
+
"Þ ß"
|
81
|
+
|
82
|
+
expected = %w{a a a a a a ae
|
83
|
+
a a a a a a ae
|
84
|
+
c e e e e d n
|
85
|
+
c e e e e d n
|
86
|
+
i i i i y
|
87
|
+
i i i i y
|
88
|
+
o o o o o o
|
89
|
+
o o o o o o
|
90
|
+
u u u u
|
91
|
+
u u u u
|
92
|
+
th ss
|
93
|
+
}
|
94
|
+
|
95
|
+
output = diacritics.to_slug.split('-')
|
96
|
+
|
97
|
+
output.each_index do |idx|
|
98
|
+
assert_equal expected[idx], output[idx]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: slug_less
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- René Maya
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.8'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.8'
|
55
|
+
description: 'Simple gem that aims at doing one thing: generate slugs with as few
|
56
|
+
lines of clean code as possible.'
|
57
|
+
email:
|
58
|
+
- rene.maya1@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/slug_less.rb
|
69
|
+
- lib/slug_less/encoding_approximations/latin_diacritics1.rb
|
70
|
+
- lib/slug_less/to_slug.rb
|
71
|
+
- lib/slug_less/version.rb
|
72
|
+
- slug_less.gemspec
|
73
|
+
- test/test_helper.rb
|
74
|
+
- test/to_slug_test.rb
|
75
|
+
homepage: https://github.com/renemaya/slug_less
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.4.8
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Generate slugs using less code
|
99
|
+
test_files:
|
100
|
+
- test/test_helper.rb
|
101
|
+
- test/to_slug_test.rb
|
102
|
+
has_rdoc:
|