mirror of
https://github.com/discourse/discourse.git
synced 2025-03-23 10:45:41 +08:00
Merge branch 'master' of github.com:discourse/discourse
This commit is contained in:
commit
c1f093d48b
27
lib/slug.rb
27
lib/slug.rb
@ -6,23 +6,24 @@
|
|||||||
module Slug
|
module Slug
|
||||||
|
|
||||||
def self.for(string)
|
def self.for(string)
|
||||||
|
str = string.dup.strip.downcase
|
||||||
|
|
||||||
|
# The characters we want to replace with a hyphen
|
||||||
|
str.tr!("·/_,:;.", "\-")
|
||||||
|
|
||||||
str = string.dup
|
# Convert to ASCII or remove if transliteration is unknown.
|
||||||
str.gsub!(/^\s+|\s+$/, '')
|
str = ActiveSupport::Inflector.transliterate(str, '')
|
||||||
str.downcase!
|
|
||||||
|
# Remove everything except alphanumberic, space, and hyphen characters.
|
||||||
from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;."
|
|
||||||
to = "aaaaeeeeiiiioooouuuunc-------"
|
|
||||||
|
|
||||||
idx = 0
|
|
||||||
from.each_char do |c|
|
|
||||||
str.gsub!(c, to[idx])
|
|
||||||
idx += 1
|
|
||||||
end
|
|
||||||
|
|
||||||
str.gsub!(/[^a-z0-9 -]/, '')
|
str.gsub!(/[^a-z0-9 -]/, '')
|
||||||
|
|
||||||
|
# Replace multiple spaces with one hyphen.
|
||||||
str.gsub!(/\s+/, '-')
|
str.gsub!(/\s+/, '-')
|
||||||
|
|
||||||
|
# Replace multiple hyphens with one hyphen.
|
||||||
str.gsub!(/\-+/, '-')
|
str.gsub!(/\-+/, '-')
|
||||||
|
|
||||||
|
# Remove leading and trailing hyphens
|
||||||
str.gsub!(/^-|-$/, '')
|
str.gsub!(/^-|-$/, '')
|
||||||
|
|
||||||
str
|
str
|
||||||
|
@ -1,12 +1,10 @@
|
|||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
|
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
require 'slug'
|
require 'slug'
|
||||||
|
|
||||||
describe Slug do
|
describe Slug do
|
||||||
|
|
||||||
|
|
||||||
it 'replaces spaces with hyphens' do
|
it 'replaces spaces with hyphens' do
|
||||||
Slug.for("hello world").should == 'hello-world'
|
Slug.for("hello world").should == 'hello-world'
|
||||||
end
|
end
|
||||||
@ -35,5 +33,11 @@ describe Slug do
|
|||||||
Slug.for("...hello").should == "hello"
|
Slug.for("...hello").should == "hello"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'handles our initial transliteration' do
|
||||||
|
from = "àáäâčďèéëěêìíïîľĺňòóöôŕřšťůùúüûýžñç"
|
||||||
|
to = "aaaacdeeeeeiiiillnoooorrstuuuuuyznc"
|
||||||
|
Slug.for(from).should == to
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user