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