to_slug 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +24 -0
- data/Rakefile +2 -0
- data/lib/to_slug/version.rb +3 -0
- data/lib/to_slug.rb +52 -0
- data/spec/to_slug_spec.rb +63 -0
- data/to_slug.gemspec +23 -0
- metadata +84 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
to_slug (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.2)
|
10
|
+
rspec (2.4.0)
|
11
|
+
rspec-core (~> 2.4.0)
|
12
|
+
rspec-expectations (~> 2.4.0)
|
13
|
+
rspec-mocks (~> 2.4.0)
|
14
|
+
rspec-core (2.4.0)
|
15
|
+
rspec-expectations (2.4.0)
|
16
|
+
diff-lcs (~> 1.1.2)
|
17
|
+
rspec-mocks (2.4.0)
|
18
|
+
|
19
|
+
PLATFORMS
|
20
|
+
ruby
|
21
|
+
|
22
|
+
DEPENDENCIES
|
23
|
+
rspec
|
24
|
+
to_slug!
|
data/Rakefile
ADDED
data/lib/to_slug.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
class String # This reopns the string class
|
4
|
+
# Define a new method to convert the string to be url friendly
|
5
|
+
def to_slug
|
6
|
+
# This is the input as there are no arguments passed.
|
7
|
+
string = self
|
8
|
+
|
9
|
+
# Define which accents map to which ascii characters
|
10
|
+
accents = {
|
11
|
+
['á','à','â','ä','ã','å'] => 'a',
|
12
|
+
['Ã','Ä','Â','À','Ã'] => 'A',
|
13
|
+
['é','è','ê','ë'] => 'e',
|
14
|
+
['Ë','É','È','Ê'] => 'E',
|
15
|
+
['í','ì','î','ï'] => 'i',
|
16
|
+
['�?','Î','Ì','�?'] => 'I',
|
17
|
+
['ó','ò','ô','ö','õ'] => 'o',
|
18
|
+
['Õ','Ö','Ô','Ò','Ó'] => 'O',
|
19
|
+
['ú','ù','û','ü','ů'] => 'u',
|
20
|
+
['Ú','Û','Ù','Ü'] => 'U',
|
21
|
+
['ç'] => 'c', ['Ç'] => 'C',
|
22
|
+
['ñ'] => 'n', ['Ñ'] => 'N',
|
23
|
+
['ý'] => 'y'
|
24
|
+
}
|
25
|
+
|
26
|
+
# Replace each accent in the string
|
27
|
+
accents.each do |accent, replacement|
|
28
|
+
accent.each do |character|
|
29
|
+
string = string.gsub(character, replacement)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Convert underscores to dashs
|
34
|
+
string = string.gsub(/[_]/,"-")
|
35
|
+
|
36
|
+
# Remove any characters that aren't alphanumeric (or a dash)
|
37
|
+
string = string.gsub(/[^a-zA-Z0-9 \-]/,"")
|
38
|
+
|
39
|
+
# Convert multiple spaces to a single space
|
40
|
+
string = string.gsub(/[ ]+/," ")
|
41
|
+
|
42
|
+
# Give a little trim around the edges (leading/trailing whitespace)
|
43
|
+
string.strip!
|
44
|
+
|
45
|
+
# Replace single spaces with a URL friendly dash (-)
|
46
|
+
string = string.gsub(/ /,"-")
|
47
|
+
|
48
|
+
# CASE. EVERYTHING. DOWN. (and return since it's the last line)
|
49
|
+
string = string.downcase
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'to_slug'
|
4
|
+
|
5
|
+
describe String, "to_slug" do
|
6
|
+
|
7
|
+
it "replaces spaces with dashes" do
|
8
|
+
"This is a string".to_slug == "this-is-a-string"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "converts accented characters to a close ASCII alternative" do
|
12
|
+
"aëiòú".to_slug.should == "aeiou"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "converts lots of accented characters to a close ASCII alternative" do
|
16
|
+
"thîs ís a nåsty strïng éëê. ýůçký".to_slug.should == "this-is-a-nasty-string-eee-yucky"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "converts a single accented character to a close ACII alternative" do
|
20
|
+
"å".to_slug.should == "a"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "converts scores to dashes" do
|
24
|
+
"This is an example to_slug".to_slug.should == "this-is-an-example-to-slug"
|
25
|
+
end
|
26
|
+
|
27
|
+
it "removes leading/trailing whitespace" do
|
28
|
+
" this is a string ".to_slug.should == "this-is-a-string"
|
29
|
+
end
|
30
|
+
|
31
|
+
it "removes leading punctuation" do
|
32
|
+
"!!*% This is leading to somewhere cool".to_slug.should == "this-is-leading-to-somewhere-cool"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "removes single quotes" do
|
36
|
+
"This is a 'quoted' string".to_slug.should == "this-is-a-quoted-string"
|
37
|
+
end
|
38
|
+
|
39
|
+
it "removes double quotes" do
|
40
|
+
'This is a "quoted" string'.to_slug.should == "this-is-a-quoted-string"
|
41
|
+
end
|
42
|
+
|
43
|
+
it "handles multiple spaces" do
|
44
|
+
"I like to put extra spaces. See".to_slug.should == "i-like-to-put-extra-spaces-see"
|
45
|
+
end
|
46
|
+
|
47
|
+
it "preserves dashes" do
|
48
|
+
"I like Mrs - on my salad".to_slug.should == "i-like-mrs---on-my-salad"
|
49
|
+
end
|
50
|
+
|
51
|
+
it "downcases all characters" do
|
52
|
+
"THIS IS SPARTAAAaaa".to_slug.should == "this-is-spartaaaaaa"
|
53
|
+
end
|
54
|
+
|
55
|
+
it "accepts empty strings" do
|
56
|
+
"".to_slug.should == ""
|
57
|
+
end
|
58
|
+
|
59
|
+
it "remains a String class" do
|
60
|
+
"I am a string class yo!".to_slug.should be_kind_of(String)
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
data/to_slug.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "to_slug/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "to_slug"
|
7
|
+
s.version = ToSlug::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Eric Boehs"]
|
10
|
+
s.email = ["ericboehs@gmail.com"]
|
11
|
+
s.homepage = "https://ericboehs.com"
|
12
|
+
s.summary = %q{Adds a to_slug method to ruby's String class}
|
13
|
+
s.description = %q{This gem makes URL friendly strings out of not so pretty strings.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "to_slug"
|
16
|
+
|
17
|
+
s.add_development_dependency "rspec"
|
18
|
+
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
s.require_paths = ["lib"]
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: to_slug
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Eric Boehs
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-02-01 00:00:00 -06:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id001
|
33
|
+
description: This gem makes URL friendly strings out of not so pretty strings.
|
34
|
+
email:
|
35
|
+
- ericboehs@gmail.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files: []
|
41
|
+
|
42
|
+
files:
|
43
|
+
- .gitignore
|
44
|
+
- Gemfile
|
45
|
+
- Gemfile.lock
|
46
|
+
- Rakefile
|
47
|
+
- lib/to_slug.rb
|
48
|
+
- lib/to_slug/version.rb
|
49
|
+
- spec/to_slug_spec.rb
|
50
|
+
- to_slug.gemspec
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: https://ericboehs.com
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
version: "0"
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project: to_slug
|
79
|
+
rubygems_version: 1.3.7
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: Adds a to_slug method to ruby's String class
|
83
|
+
test_files:
|
84
|
+
- spec/to_slug_spec.rb
|