fjrodafo-slugify 0.1.1 → 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 +4 -4
- data/README.md +6 -6
- data/bin/fjrodafo-slugify +1 -1
- data/lib/fjrodafo/slugify/transliteration.rb +11 -1
- data/lib/fjrodafo/slugify.rb +17 -9
- data/test/test_fjrodafo-slugify.rb +5 -0
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3763460c961dfb76f6f92b96ed2663f33912bb2076f2ef3befda806db7b100ab
|
|
4
|
+
data.tar.gz: ad3bff584c50e70e5f1e16fa7240eb5f3663d0730e3c92afa2a66e96e831b623
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e5d66d732b9196950964325e634404032c12222773270c5450ef62ffa7a85393b2ba95e3697754d046dd75ed6b0d1020e7cb7cc8cca910bd8901dd7a5d96fcf4
|
|
7
|
+
data.tar.gz: 44a5ce6d92d87921f8031ae8b27a845330d4a4a0f1e94a5a9f94ddd35c65c4152dcba6dfd30417ef23b07940b63f2feb9e75b027a1eb05d73de0bdbe621b8e67
|
data/README.md
CHANGED
|
@@ -6,18 +6,18 @@ gem install fjrodafo-slugify
|
|
|
6
6
|
|
|
7
7
|
## Run it!
|
|
8
8
|
|
|
9
|
-
```
|
|
10
|
-
#
|
|
9
|
+
```ruby
|
|
10
|
+
# Load the gem
|
|
11
11
|
require "fjrodafo/slugify"
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
-
```
|
|
15
|
-
Fjrodafo::Slugify.to_slug("
|
|
16
|
-
# => "
|
|
14
|
+
```ruby
|
|
15
|
+
Fjrodafo::Slugify.to_slug("Hello, World!")
|
|
16
|
+
# => "hello-world"
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
## Links
|
|
20
20
|
|
|
21
|
-
Contribute to the repository on [GitHub](https://github.com/FJrodafo/
|
|
21
|
+
Contribute to the repository on [GitHub](https://github.com/FJrodafo/Slugify).
|
|
22
22
|
|
|
23
23
|
Check out this gem on [GitHub Packages](https://github.com/FJrodafo/Slugify/pkgs/rubygems/fjrodafo-slugify) or [RubyGems](https://rubygems.org/gems/fjrodafo-slugify) website!
|
data/bin/fjrodafo-slugify
CHANGED
|
@@ -1,15 +1,25 @@
|
|
|
1
|
+
# Main module for the gem
|
|
1
2
|
module FJrodafo
|
|
3
|
+
# Submodule for slugify functionality
|
|
2
4
|
module Slugify
|
|
5
|
+
# Module that contains the character transliteration mappings
|
|
3
6
|
module Transliteration
|
|
7
|
+
# A hash mapping special or accented characters to their ASCII equivalents
|
|
8
|
+
# Used to replace characters that are not URL-friendly
|
|
4
9
|
MAP = {
|
|
5
10
|
"ø" => "o",
|
|
6
11
|
"ß" => "ss",
|
|
7
12
|
"æ" => "ae",
|
|
8
13
|
"œ" => "oe",
|
|
9
14
|
"ł" => "l"
|
|
10
|
-
}.freeze
|
|
15
|
+
}.freeze # Freeze the hash to make it immutable
|
|
11
16
|
|
|
17
|
+
# String of all characters to replace, used with `String#tr`
|
|
18
|
+
# Example: "øßæœł"
|
|
12
19
|
FROM = MAP.keys.join.freeze
|
|
20
|
+
|
|
21
|
+
# String of replacement characters, used with `String#tr`
|
|
22
|
+
# Example: "ossaeoel"
|
|
13
23
|
TO = MAP.values.join.freeze
|
|
14
24
|
end
|
|
15
25
|
end
|
data/lib/fjrodafo/slugify.rb
CHANGED
|
@@ -1,23 +1,31 @@
|
|
|
1
|
+
# Load the file that contains the Transliteration module which defines the character replacement map for special characters
|
|
1
2
|
require_relative "slugify/transliteration"
|
|
2
3
|
|
|
4
|
+
# Main module for the gem
|
|
3
5
|
module FJrodafo
|
|
6
|
+
# Submodule that provides the "slugify" functionality
|
|
4
7
|
module Slugify
|
|
8
|
+
# Class method that converts any text into a URL-safe slug
|
|
9
|
+
# @param text [String] The input text to convert
|
|
10
|
+
# @return [String] The resulting slug
|
|
5
11
|
def self.to_slug(text)
|
|
12
|
+
# Return an empty string if the input is nil
|
|
6
13
|
return "" if text.nil?
|
|
7
14
|
|
|
15
|
+
# Convert the input to string (in case it is not a string) and chain transformations to produce a slug
|
|
8
16
|
text
|
|
9
|
-
.to_s
|
|
10
|
-
.downcase
|
|
11
|
-
.strip
|
|
12
|
-
.unicode_normalize(:nfd)
|
|
17
|
+
.to_s # Convert the input to a string, in case it's nil, a number, or another type
|
|
18
|
+
.downcase # Convert the string to lowercase
|
|
19
|
+
.strip # Remove leading and trailing spaces
|
|
20
|
+
.unicode_normalize(:nfd) # Normalize Unicode to separate accents
|
|
13
21
|
.tr(
|
|
14
|
-
Transliteration::FROM,
|
|
22
|
+
Transliteration::FROM, # Replace special characters according to the map
|
|
15
23
|
Transliteration::TO
|
|
16
24
|
)
|
|
17
|
-
.gsub(/\p{Mn}/, "")
|
|
18
|
-
.gsub(/[^a-z0-9\s-]/, "")
|
|
19
|
-
.gsub(/[\s_-]+/, "-")
|
|
20
|
-
.gsub(/^-+|-+$/, "")
|
|
25
|
+
.gsub(/\p{Mn}/, "") # Remove accent marks (diacritics)
|
|
26
|
+
.gsub(/[^a-z0-9\s-]/, "") # Remove everything except a-z, 0-9, space, or hyphen
|
|
27
|
+
.gsub(/[\s_-]+/, "-") # Replace consecutive spaces, underscores, or hyphens with a single hyphen
|
|
28
|
+
.gsub(/^-+|-+$/, "") # Remove hyphens at the start or end
|
|
21
29
|
end
|
|
22
30
|
end
|
|
23
31
|
end
|
|
@@ -2,6 +2,11 @@ require "minitest/autorun"
|
|
|
2
2
|
require "fjrodafo/slugify"
|
|
3
3
|
|
|
4
4
|
class FJrodafoSlugifyTest < Minitest::Test
|
|
5
|
+
def test_empty
|
|
6
|
+
assert_equal "",
|
|
7
|
+
FJrodafo::Slugify.to_slug("")
|
|
8
|
+
end
|
|
9
|
+
|
|
5
10
|
def test_hello_world
|
|
6
11
|
assert_equal "hello-world",
|
|
7
12
|
FJrodafo::Slugify.to_slug("Hello, World!")
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: fjrodafo-slugify
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Francisco José Rodríguez Afonso
|
|
@@ -36,7 +36,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
36
36
|
requirements:
|
|
37
37
|
- - ">="
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
|
-
version: '
|
|
39
|
+
version: '3.1'
|
|
40
40
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
41
41
|
requirements:
|
|
42
42
|
- - ">="
|