Permalink
Browse files

Merge branch 'master' of git://github.com/yizzreel/permalizer into yi…

…zzreel/master
  • Loading branch information...
2 parents c1181d2 + ea42783 commit 6973c31f55d8de082365c945ca33aab0e31a5032 rrevans committed May 10, 2008
Showing with 82 additions and 26 deletions.
  1. +2 −2 lib/permalizer.rb
  2. +65 −24 lib/permalizer/permalink.rb
  3. +15 −0 spec/permalizer_spec.rb
View
@@ -2,9 +2,9 @@
require 'iconv'
# permalizer files
-require File.dirname(__FILE__) + '/permalizer/permalink.rb'
+require File.dirname(__FILE__) + '/permalizer/permalink'
# open the String Class and include our additions
String.class_eval do
- include Permalink
+ include Permalink::InstanceMethods
end
@@ -1,31 +1,72 @@
# Permalizer adds functionality to the String Object so that clean URLs creation is made simple
module Permalink
+ module InstanceMethods
+ # permalize! is a destructive method that will make the given string for use as a clean URL
+ # example:
+ # blog.title = "My Cool Gem!"
+ # blog.title.permalize! # => "my-cool-gem"
+ #
+ def permalize!
+ replace permalize
+ end
- # permalize! is a destructive method that will make the given string for use as a clean URL
- # example:
- # blog.title = "My Cool Gem!"
- # blog.title.permalize! # => "my-cool-gem"
- #
- def permalize!
- permalink!(self)
+ # permalize is the same as permalize! except that it is not a destructive method
+ # it creates a duplicate of the string and returns it as a clean string for URL usage
+ #
+ def permalize
+ Permalink::Permalizer.new(self).to_s
+ end
end
-
- # permalize is the same as permalize! except that it is not a destructive method
- # it creates a duplicate of the string and returns it as a clean string for URL usage
- #
- def permalize
- string = self.dup
- permalink!(string)
+
+ class Permalizer
+
+ # Class variable which determines the Iconv fix method
+ @@fix_method = :us_ascii
+ # Class variable which determines whether the decompose hook is applied
+ @@decompose_string = false
+
+ class << self
+ attr_accessor :decompose_string
+ attr_accessor :fix_method
+ end
+
+ def initialize(word)
+ # only if decompose_string is true and ActiveSupport is loaed in UTF-8 mode (with UTF8Handler as handler class)
+ # This is required for some remote Linux machines, I regulary use it in production environment
+ @word = word
+ decompose! if Permalizer.decompose_string && word.respond_to?(:chars) && word.chars.respond_to?(:decompose)
+ end
+
+ def us_ascii
+ Iconv.new('US-ASCII//TRANSLIT', 'UTF-8').iconv @word
+ end
+
+ def utf_8
+ Iconv.new('UTF-8//TRANSLIT//IGNORE', 'UTF-8').iconv @word
+ end
+
+ def iso_8859_1
+ Iconv.new('ISO-8859-1//TRANSLIT//IGNORE', 'UTF-8').iconv @word
+ end
+
+ def to_s
+ transform send(@@fix_method)
+ rescue
+ transform us_ascii
+ end
+
+ protected
+
+ def transform(word)
+ word.gsub(/[^\w\s\-\—]/,'').gsub(/[^\w]|[\_]/,' ').split.join('-').downcase
+ end
+
+ private
+
+ def decompose!
+ @word = @word.chars.decompose
+ end
+
end
-
- private
-
- # permalink!
- # Fix unicode characters, regex unwanted characters, split string, thus removing all whitespace, join it, and downcase it
- #
- # TODO: Ruby 1.9 string encoding
- def permalink!(word)
- (Iconv.new('US-ASCII//TRANSLIT', 'utf-8').iconv word).gsub(/[^\w\s\-\—]/,'').gsub(/[^\w]|[\_]/,' ').split.join('-').downcase
- end
end
View
@@ -16,6 +16,12 @@
"I've been banged".permalize!.should eql("ive-been-banged")
end
+ it "should ovewrite string original value when permalize! is called" do
+ string = "i'll be overriden soon"
+ string.permalize!
+ string.should eql("ill-be-overriden-soon")
+ end
+
it "should correctly handle single and double quotes" do
"\'here are some single quotes\'".permalize.should eql("here-are-some-single-quotes")
"\'here are some single quotes\'".permalize!.should eql("here-are-some-single-quotes")
@@ -73,4 +79,13 @@
"Should HanDle UPPER case LeTtErS".permalize!.should eql("should-handle-upper-case-letters")
end
+ it "should perform permalization even with a misspelled fix_method" do
+ Permalink::Permalizer.fix_method = :undefined_fix_method
+ "This will be permalized".permalize.should eql("this-will-be-permalized")
+ end
+
+ it "should perform an utf-8 transformation" do
+ Permalink::Permalizer.fix_method = :utf_8
+ "This string contains utf-8 characters: esdrújula. €100".permalize.should eql("this-string-contains-utf-8-characters-esdrujula-eur100")
+ end
end

0 comments on commit 6973c31

Please sign in to comment.