Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fixed UTF-8 slugs, closes #29. #113

Open
wants to merge 1 commit into from

1 participant

@sorin-ionescu

Ruby 1.9 users will get a warning about iconv being deprecated, but it's still better than String#encode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 15, 2012
  1. @sorin-ionescu
This page is out of date. Refresh to see the latest.
Showing with 15 additions and 3 deletions.
  1. +11 −2 lib/ext/ext.rb
  2. +4 −1 lib/toto.rb
View
13 lib/ext/ext.rb
@@ -1,3 +1,5 @@
+require 'iconv'
+
class Object
def meta_def name, &blk
(class << self; self; end).instance_eval do
@@ -7,8 +9,15 @@ def meta_def name, &blk
end
class String
- def slugize
- self.downcase.gsub(/&/, 'and').gsub(/\s+/, '-').gsub(/[^a-z0-9-]/, '')
+ # http://websideattractions.com/2008/11/06/squish-the-slug-the-rails-way
+ def slugize(slug='-')
+ slugged = Iconv.iconv('ascii//TRANSLIT//IGNORE', 'utf-8', self).to_s
+ slugged.gsub!(/&/, 'and')
+ slugged.gsub!(/[^\w_\-#{Regexp.escape(slug)}]+/i, slug)
+ slugged.gsub!(/#{slug}{2,}/i, slug)
+ slugged.gsub!(/^#{slug}|#{slug}$/i, '')
+ slugged.downcase!
+ URI.escape(slugged, /[^\w_+-]/i)
end
def humanize
View
5 lib/toto.rb
@@ -90,7 +90,10 @@ def archives filter = ""
end
def article route
- Article.new("#{Paths[:articles]}/#{route.join('-')}.#{self[:ext]}", @config).load
+ path = self.articles.select do |article|
+ File.basename(article, ".#{self[:ext]}").slugize.eql? route.join('-')
+ end.last || File.join(Paths[:articles], "#{route.join('-')}.#{self[:ext]}")
+ Article.new(path, @config).load
end
def /
Something went wrong with that request. Please try again.