2009-06-05 142 views
2

有谁知道我可以如何将html转换为纯文本。那么我真的需要将RedCloth转换为纯文本,无论哪种方式都可以。Ruby:将HTML/Redcloth转换为纯文本

我不是在谈论剥离标签(这是我迄今为止所做的所有)。例如,我想一个有序列表来保留号码,无序列表使用的子弹等星号

def red_cloth_to_plain_text(s) 
     s = RedCloth.new(s).to_html 
     s = strip_tags(s) 
     s = html_unescape(s) # reverse of html_escape 
     s = undo_red_cloths_html_codes(s) 
     return s 
end 

也许我不得不尝试RedCloth以纯文本格式

回答

2

您需要制作一个新的格式器类。

module RedCloth::Formatters 
    module PlainText 
    include RedCloth::Formatters::Base 
    # ... 
    end 
end 

我今天不会为你写代码,但这很容易做到。如果您怀疑我,请阅读RedCloth源代码:HTML格式化程序只有346行。

所以,一旦你有你的纯文本格式,你修补类,并使用它:

module RedCloth 
    class TextileDoc 
    def to_txt(*rules) 
     apply_rules(rules) 
     to(RedCloth::Formatters::PlainText) 
    end 
    end 
end 

print RedCloth.new(str).to_txt