1

我已经看到了曙光,我正在将我的网站转换为RedCloth以使用美妙的纺织品标记。

问题是,我的数据库中有几年价值的 html 内容不是纺织品标记。我怎么把它变成纺织品?有非手动的方法吗?

如果不是,在输出内容时,我是否应该检查 html,如果存在则不要使用 RedCloth?

例子:

// 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

这是什么红宝石方式?帮助新手!:-)

4

2 回答 2

1

找到这个:http ://clothred.rubyforge.org/

这是:https ://github.com/jystewart/html2textile

这是:https ://github.com/mattt/pricetag

编辑

我选择了 html2textile。我使用我问的这个 SO question中的说明安装了它。

然后我在 /lib/tasks/app.rake 中创建了一个名为 Textile_ize 的任务。它看起来像这样:

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

然后我可以跑步rake db:textile_ize和噗,完成。我实际上添加了一个额外的列来存储纺织品并使用:before_save. 看起来像这样(在我的模型中):

before_save :textile_ize

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

希望这可以帮助那里的人!

于 2011-01-28T19:55:05.957 回答
1

我使用 Rdiscount 进行标记解析,它适用于 html 和 Textile 输入。我想 Textile 允许 html ,事实上,如果我在这个编辑器中放置强标签,它就可以工作!

如果你用 redcloth 简单地解析 html 和 Textile 会发生什么?

<%= RedCloth.new(@content.description).to_html %>
于 2011-01-28T16:55:08.173 回答