Generalize Utils#slugify for any scripts

It replaces any non alphanumeric glyphs by an hyphen.
This commit is contained in:
Renaud Martinet 2014-10-31 15:56:03 +01:00
parent 81f4abdbcd
commit f9e249ae20
2 changed files with 6 additions and 1 deletions

View File

@ -113,7 +113,7 @@ module Jekyll
unless string.nil?
string \
# Replace each non-alphanumeric character sequence with a hyphen
.gsub(/[^a-z0-9]+/i, '-') \
.gsub(/[^[:alnum:]]+/i, '-') \
# Remove leading/trailing hyphen
.gsub(/^\-|\-$/i, '') \
# Downcase it

View File

@ -120,6 +120,7 @@ class TestUtils < Test::Unit::TestCase
should "drop trailing punctuation" do
assert_equal "so-what-is-jekyll-exactly", Utils.slugify("So what is Jekyll, exactly?")
assert_equal "كيف-حالك", Utils.slugify("كيف حالك؟")
end
should "ignore hyphens" do
@ -134,6 +135,10 @@ class TestUtils < Test::Unit::TestCase
assert_equal "customizing-git-git-hooks", Utils.slugify("Customizing Git - Git Hooks")
end
should "replace punctuation in any scripts by hyphens" do
assert_equal "5時-6時-三-一四", Utils.slugify("5時〜6時 三・一四")
end
should "not modify the original string" do
title = "Quick-start guide"
Utils.slugify(title)