Skip to content

Commit

Permalink
issue where iconv translation of chinese characters to '?????', which…
Browse files Browse the repository at this point in the history
… comes out blank after subsitutions. Added failsafe to generate random permalink if the result is ever blank.
  • Loading branch information
cyu committed Oct 19, 2008
1 parent 775ae1d commit dec57c9
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/permalink_fu.rb
Expand Up @@ -14,15 +14,20 @@ class << self
def escape(string)
begin
result = ((translation_to && translation_from) ? Iconv.iconv(translation_to, translation_from, string) : string).to_s
result.gsub!(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics).
result.gsub!(/[^\w_ \-]+/i, '') # Remove unwanted chars.
result.gsub!(/[ \-]+/i, '-') # No more than one of the separator in a row.
result.gsub!(/^\-|\-$/i, '') # Remove leading/trailing separator.
result.downcase!

result.blank? ? random_permalink : result
rescue
result = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
random_permalink
end
result.gsub!(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics).
result.gsub!(/[^\w_ \-]+/i, '') # Remove unwanted chars.
result.gsub!(/[ \-]+/i, '-') # No more than one of the separator in a row.
result.gsub!(/^\-|\-$/i, '') # Remove leading/trailing separator.
result.downcase!
result
end

def random_permalink
Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
end
end

Expand Down

0 comments on commit dec57c9

Please sign in to comment.