Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 25th, 2012  |  syntax: Ruby  |  size: 1.07 KB  |  hits: 61  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. module Extentions
  2.   module Cleanup
  3.     class << self
  4.       def cleanup(text)
  5.         lines = []
  6.         text.lines do |line|
  7.           line.gsub! /(style=".*?")/, ''
  8.           line.gsub! /<br.?\/>/, "</p>\n<p>"
  9.           line.gsub! /<\/?span.?>/, ''
  10.           line.gsub! '</div>', '</p>'
  11.           line.gsub! '<div', '<p'
  12.  
  13.           lines << line
  14.         end
  15.  
  16.         lines.join
  17.       end
  18.     end
  19.  
  20.     extend ActiveSupport::Concern
  21.  
  22.     self.included do
  23.       attr_accessor :enable_cleanup
  24.  
  25.       class << self
  26.         class_attribute :symbols
  27.  
  28.         def cleanup_markup_for(symbols)
  29.           symbols = [symbols] if symbols.kind_of? Symbol
  30.           raise ArgumentError unless symbols.kind_of? Array
  31.  
  32.           puts self.inspect
  33.  
  34.           @symbols = symbols
  35.         end
  36.       end
  37.  
  38.       before_validation :cleanup_callback
  39.     end
  40.  
  41.     def cleanup_callback
  42.       if self.enable_cleanup
  43.         (self.class.symbols || []).each do |sym|
  44.           self.send "#{sym}=".to_sym, Cleanup.cleanup(self.send sym)
  45.         end
  46.       end
  47.     end
  48.   end
  49. end