有人知道如何用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到纯文本格式化程序
发布于 2009-06-05 11:24:42
您需要创建一个新的格式化器类。
module RedCloth::Formatters
module PlainText
include RedCloth::Formatters::Base
# ...
end
end我今天不会为你写你的代码,但这很容易做到。如果您对我有疑问,请阅读RedCloth源代码:它只有346行。
因此,一旦有了PlainText格式化程序,就可以修补类并使用它:
module RedCloth
class TextileDoc
def to_txt( *rules )
apply_rules(rules)
to(RedCloth::Formatters::PlainText)
end
end
end
print RedCloth.new(str).to_txt发布于 2010-05-13 21:33:20
Joseph Halter写了一个RedCloth的普通格式化程序:
http://github.com/JosephHalter/redcloth-formatters-plain
示例用法:
RedCloth.new("p. this is *simple* _test_").to_plain将返回:
"this is simple test"发布于 2009-06-05 11:11:31
这可能就是你必须要做的。You're not the first to want this,但我猜它还不是这个库的一部分,因为每个人都希望他们的纯文本稍有不同。
https://stackoverflow.com/questions/955027
复制相似问题