开发者

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, i开发者_如何学运维f 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! :-)


I'm using Rdiscount for markup parsing , it work both with html and textile input . I suppose textile allow html , infact if i put strong tag in this editor it's work!

what happens if you simple parse both html and textile with redcloth?

<%= RedCloth.new(@content.description).to_html %>


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜