Ruby:将HTML/Redcloth转换为纯文本
有人知道我如何用Ruby将html转换成纯文本吗。嗯,我真的需要将红布转换成纯文本,无论哪种方式都可以 我并不是说仅仅去掉标签(这是我到目前为止所做的一切)。例如,我想要一个有序的列表来保留数字,无序的列表用星号表示项目符号等等Ruby:将HTML/Redcloth转换为纯文本,ruby,formatting,Ruby,Formatting,有人知道我如何用Ruby将html转换成纯文本吗。嗯,我真的需要将红布转换成纯文本,无论哪种方式都可以 我并不是说仅仅去掉标签(这是我到目前为止所做的一切)。例如,我想要一个有序的列表来保留数字,无序的列表用星号表示项目符号等等 def red_cloth_to_plain_text(s) s = RedCloth.new(s).to_html s = strip_tags(s) s = html_unescape(s) # reverse of ht
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
也许我必须尝试一个红布到纯文本格式化程序,这可能就是你必须要做的,但我猜它还不是库的一部分,因为每个人都希望自己的明文有点不同。您需要创建一个新的格式化程序类
module RedCloth::Formatters
module PlainText
include RedCloth::Formatters::Base
# ...
end
end
今天我不会为您编写代码,但这很容易做到。如果你怀疑我,请阅读红布来源: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
Joseph Halter编写了一个红布普通格式化程序: 用法示例:
RedCloth.new("p. this is *simple* _test_").to_plain
将返回:
"this is simple test"