ruby-on-rails – 将HTML转换为正确的纯文本?

有什么办法可以将HTML转换为正确的纯文本吗?我尝试了从raw到sanitize的所有东西,甚至还有使用text_part方法的Mail gem,它本应该做到这一点,但对我来说不起作用.

到目前为止,我最好的镜头是strip_tags(strip_links(resource.body))但是< p>,< ul>等未正确转换.

这或多或少是我在HTML中的含义:

Hello

This is some text. Blah blah blah.

Address:
John Doe
10 ABC Street
Whatever City

New Features
- Feature A
- Feature B
- Feature C
Check this out: http://www.google.com

Best,
Admin

转换成类似的东西

Hello
This is some text. Blah blah blah.
Address: John Doe 10 ABC Street Whatever City

New Features Feature A Feature B Feature C
Check this out: http://www.google.com

Best, Admin

任何的想法?

Rails 4.2.1有#strip_tags,一种内置方法,特别适用于剥离HTML标记.

一些例子:

strip_tags("Strip <i>these</i> tags!")

=>剥去这些标签!

strip_tags("<b>Bold</b> no more!  <a href='more.html'>See more here</a>...")

=>大胆不再!在这里看到更多……

strip_tags("<div id='top-bar'>Welcome to my website!</div>")

=>欢迎来到我的网站!

API docs中查看.

翻译自:https://stackoverflow.com/questions/18866943/convert-html-to-a-proper-plain-text

转载注明原文:ruby-on-rails – 将HTML转换为正确的纯文本?