slugifier 1.0.0
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/README.md +19 -0
- data/lib/slugifier.rb +38 -0
- data/spec/slugifier_spec.rb +17 -0
- metadata +62 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5ef915c5d0d39c51502460d07f7ce4af34a9eb39
|
4
|
+
data.tar.gz: afd606fd68c9b2d31f02ad42f91b28c2619e6f05
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4029087cc9eea5ca730a0943d3b53b407e3a823f4b95a4f3d2f9ad5739a0efb6170fd3f8cd3ffb1b8031926b112be8715c295d23d1a93ccad12ee245d90765d8
|
7
|
+
data.tar.gz: f79602bc95d724fc7b884ea1f7f0fe7c03b2a44ca025772f64b82ad09b0b4565664836fe23e8bfe82a49db3b618f181047a7aceef05b65114e34bd6d7dfe61d3
|
data/README.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# Slugifier
|
2
|
+
|
3
|
+
Heavily-based on [Keyvan Akbary's PHP slugifier](https://github.com/keyvanakbary/slugifier).
|
4
|
+
|
5
|
+
We use this at [Chicisimo](https://github.com/chicisimo).
|
6
|
+
|
7
|
+
## What's a slug?
|
8
|
+
|
9
|
+
> Some systems define a slug as the part of a URL which identifies a page using human-readable keywords.
|
10
|
+
|
11
|
+
[http://en.wikipedia.org/wiki/Semantic_URL#Slug](http://en.wikipedia.org/wiki/Semantic_URL#Slug)
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
require "slugifier"
|
17
|
+
|
18
|
+
Slugifier.slugify("Here's to the crazy ones") # => here-s-to-the-crazy-ones
|
19
|
+
```
|
data/lib/slugifier.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
class Slugifier
|
2
|
+
UNWANTED_CHARACTERS = /([^a-z0-9])+/;
|
3
|
+
|
4
|
+
def self.slugify(string)
|
5
|
+
string = normalize(string)
|
6
|
+
string.gsub!(UNWANTED_CHARACTERS, "-")
|
7
|
+
string
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.normalize(string)
|
11
|
+
string = transliterate(string)
|
12
|
+
string.downcase!
|
13
|
+
string.strip!
|
14
|
+
string
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.transliterate(string)
|
18
|
+
transliteration = string
|
19
|
+
table.each { |character, translit| transliteration.tr!(character, translit) }
|
20
|
+
transliteration
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.table
|
24
|
+
{
|
25
|
+
"Š"=>"S", "š"=>"s", "Đ"=>"Dj", "đ"=>"dj", "Ž"=>"Z", "ž"=>"z", "Č"=>"C",
|
26
|
+
"č"=>"c", "Ć"=>"C", "ć"=>"c", "À"=>"A", "Á"=>"A", "Â"=>"A", "Ã"=>"A",
|
27
|
+
"Ä"=>"A", "Å"=>"A", "Æ"=>"A", "Ç"=>"C", "È"=>"E", "É"=>"E", "Ê"=>"E",
|
28
|
+
"Ë"=>"E", "Ì"=>"I", "Í"=>"I", "Î"=>"I", "Ï"=>"I", "Ñ"=>"N", "Ò"=>"O",
|
29
|
+
"Ó"=>"O", "Ô"=>"O", "Õ"=>"O", "Ö"=>"O", "Ø"=>"O", "Ù"=>"U", "Ú"=>"U",
|
30
|
+
"Û"=>"U", "Ü"=>"U", "Ý"=>"Y", "Þ"=>"B", "ß"=>"Ss", "à"=>"a", "á"=>"a",
|
31
|
+
"â"=>"a", "ã"=>"a", "ä"=>"a", "å"=>"a", "æ"=>"a", "ç"=>"c", "è"=>"e",
|
32
|
+
"é"=>"e", "ê"=>"e", "ë"=>"e", "ì"=>"i", "í"=>"i", "î"=>"i", "ï"=>"i",
|
33
|
+
"ð"=>"o", "ñ"=>"n", "ò"=>"o", "ó"=>"o", "ô"=>"o", "õ"=>"o", "ö"=>"o",
|
34
|
+
"ø"=>"o", "ù"=>"u", "ú"=>"u", "û"=>"u", "ý"=>"y", "þ"=>"b", "ÿ"=>"y",
|
35
|
+
"Ŕ"=>"R", "ŕ"=>"r", "ü" => "u", "ƒ" => "f"
|
36
|
+
}
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require_relative "../lib/slugifier"
|
2
|
+
|
3
|
+
describe Slugifier do
|
4
|
+
it "returns an slug" do
|
5
|
+
examples.each { |string, slug| expect(Slugifier.slugify(string)).to eq(slug) }
|
6
|
+
end
|
7
|
+
|
8
|
+
def examples
|
9
|
+
[
|
10
|
+
["Word", "word"],
|
11
|
+
["An awesome slug", "an-awesome-slug"],
|
12
|
+
[" should trim this text ", "should-trim-this-text"],
|
13
|
+
["Práctica de acentuación", "practica-de-acentuacion"],
|
14
|
+
["Cumpleaños del muerciélago", "cumpleanos-del-muercielago"]
|
15
|
+
]
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: slugifier
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pedro Gimenez
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
description: Returns a slug given a string.
|
28
|
+
email:
|
29
|
+
- me@pedro.bz
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files:
|
33
|
+
- README.md
|
34
|
+
files:
|
35
|
+
- README.md
|
36
|
+
- lib/slugifier.rb
|
37
|
+
- spec/slugifier_spec.rb
|
38
|
+
homepage: http://github.com/pedrogimenez/slugifier
|
39
|
+
licenses:
|
40
|
+
- MIT
|
41
|
+
metadata: {}
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 2.1.0
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 2.2.0
|
59
|
+
signing_key:
|
60
|
+
specification_version: 4
|
61
|
+
summary: Returns a slug given a string.
|
62
|
+
test_files: []
|