Skip to content
This repository has been archived by the owner on Dec 29, 2021. It is now read-only.

Commit

Permalink
Replace Slugalizer/iconv with ActiveSupport::Multibyte. Gives us cros…
Browse files Browse the repository at this point in the history
…s-platform-reliable slugalizing without external dependencies (beyond ActiveSupport).
  • Loading branch information
henrik committed Jun 3, 2008
1 parent b500524 commit fcbf49e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 132 deletions.
6 changes: 2 additions & 4 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

A simple plugin for creating URL-friendly permalinks (slugs) from attributes.

Uses the the [`unicode` library](http://www.yoshidam.net/Ruby.html) (`gem install unicode`) if available.

Falls back to `iconv` from the Ruby standard library if `unicode` can't be loaded, but note that this library is [inconsistent between platforms](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/243426).
Uses [`ActiveSupport::Multibyte::Handlers::UTF8Handler`](http://api.rubyonrails.org/classes/ActiveSupport/Multibyte/Handlers/UTF8Handler.html) (part of Rails since 1.2) rather than `iconv` (which is [inconsistent between platforms](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/243426)) for normalization/decomposition.


## Usage
Expand Down Expand Up @@ -63,4 +61,4 @@ Originally extracted from [Mephisto](http://mephistoblog.com) by [technoweenie](

Conditions added by [Pat Nakajima](http://github.com/nakajima/permalink_fu/).

[Henrik Nyh](http://github.com/technoweenie/permalink_fu/) made various fixes, including the addition of the [slugalizer](http://github.com/henrik/slugalizer) library (originally by [Christoffer Sawicki](http://termos.vemod.net)).
[Henrik Nyh](http://github.com/technoweenie/permalink_fu/) replaced `iconv` with `ActiveSupport::Multibyte`.
31 changes: 10 additions & 21 deletions lib/permalink_fu.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
$LOAD_PATH << "#{File.dirname(__FILE__)}/../vendor"

begin
require 'slugalizer'
require "active_support"
rescue LoadError

message = "Couldn't load 'unicode' library! Falling back to iconv, which has issues: see README!"
defined?(RAILS_DEFAULT_LOGGER) ? RAILS_DEFAULT_LOGGER.warn(message) : STDERR.puts(message)

require 'iconv'
class Iconv
def self.slugalize(string)
s = Iconv.iconv('ascii//translit//IGNORE', 'utf-8', string).to_s
s.gsub!(/[^\w -]+/, '') # strip unwanted characters
s.strip! # ohh la la
s.downcase!
s.gsub!(/[ -]+/, '-') # separate by single dashes
s
end
end

require "rubygems"
require "active_support"
end

module PermalinkFu

def self.escape(str)
object = Object.const_defined?(:Slugalizer) ? Slugalizer : Iconv
object.slugalize(str)
s = ActiveSupport::Multibyte::Handlers::UTF8Handler.normalize(str.to_s, :kd)
s.gsub!(/[^\w -]+/, '') # strip unwanted characters
s.strip! # ohh la la
s.downcase!
s.gsub!(/[ -]+/, '-') # separate by single dashes
s
end

def self.included(base)
Expand Down
107 changes: 0 additions & 107 deletions vendor/slugalizer.rb

This file was deleted.

0 comments on commit fcbf49e

Please sign in to comment.