diff --git a/lib/slug.rb b/lib/slug.rb index 13dc53033..86025079c 100644 --- a/lib/slug.rb +++ b/lib/slug.rb @@ -6,20 +6,24 @@ module Slug def self.for(string) - - str = string.dup - str.gsub!(/^\s+|\s+$/, '') - str.downcase! - + str = string.dup.strip.downcase + # The characters we want to replace with a hyphen str.tr!("ยท/_,:;.", "\-") # 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 diff --git a/spec/components/slug_spec.rb b/spec/components/slug_spec.rb index 1390103cd..44bd1177e 100644 --- a/spec/components/slug_spec.rb +++ b/spec/components/slug_spec.rb @@ -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