iconv conversion


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. require 'iconv'
  2. module PermalinkFu
  3. class << self
  4. attr_accessor :translation_to
  5. attr_accessor :translation_from
  6.  
  7. def escape(str)
  8. s = Iconv.iconv(translation_to, translation_from, str).to_s
  9. s.gsub!(/\W+/, ' ') # all non-word chars to spaces
  10. s.strip! # ohh la la
  11. s.downcase! #
  12. s.gsub!(/\ +/, '-') # spaces to dashes, preferred separator char everywhere
  13. s
  14. end
  15. end
  16.  
  17. def has_permalink(attr_name, permalink_field = nil)
  18. permalink_field ||= 'permalink'
  19. after_validation { |record| record.send("#{permalink_field}=", PermalinkFu.escape(record.send(attr_name).to_s)) if record.send(permalink_field).to_s.empty? }
  20. end
  21. end
  22.  
  23. PermalinkFu.translation_to = 'ascii//ignore//translit'
  24. PermalinkFu.translation_from = 'utf-8'

URL: http://svn.techno-weenie.net/projects/plugins/permalink_fu/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.