Search code examples
ruby-on-railsrubygemsredcloth

Rails - Redcloth, how do I convert exsiting wysiwyg crap html?


I've seen the light, I'm converting my site over to RedCloth to use the wonderful textile markup.

Problem is, I have a couple years worth of html content in my database that is not textile markup. How do I convert that into textile? Is there a non-manual way?

If no, when outputting content, should I check for html, if present then don't use RedCloth?

example:

// not sure of best way to check if a string has html
if (@content.description has html)
    <%= @content.description.html_safe %>
else
    <%= RedCloth.new(@content.description).to_html %>
end

What is the ruby way on this? Help a newb! :-)


Solution

  • Found this: http://clothred.rubyforge.org/

    And this: https://github.com/jystewart/html2textile

    And this: https://github.com/mattt/pricetag

    edit

    I went with html2textile. I installed it using the instruction from this SO question I asked.

    I then created a task in /lib/tasks/app.rake called textile_ize. It looks like so:

    namespace :db do
      desc "Convert html to textile on desc columns"
      task :textile_ize => :environment do
        puts "Starting Desc"
    
        contents = Content.all
        contents.each do |c|
                        #desc
            parser = HTMLToTextileParser.new
            parser.feed(c.desc)
            c.desc_textile = parser.to_textile
            c.save
        end
    
        puts "Finished with Desc"
    
    end
    

    I could then run rake db:textile_ize and poof, done. I actually added an additional column to store the textile and create html from the textile with :before_save. That looked like this (in my model):

    before_save :textile_ize
    
    # convert textile to html
    def textile_ize
        self.desc = RedCloth.new(self.desc_textile).to_html     
    end
    

    Hope this helps someone out there!