开发者

Ruby, Textile: how to get plain text without tags and textile format

I have articles with bodies that are textile formatted text. I make full text search with sphinx and I want to highlight query in results. Sphinx give me something like this

A *simple* _<span class="highlight">example&l开发者_如何学Got;/span>_.

I want to remove all textile format but save highlight tags, like this

A simple <span class="highlight">example</span>.

Is it possible with Redcloth gem?


You could process the content with RedCloth and then strip the result of all tags. So something like:

strip_tags (RedCloth.new(simple_input).to_html)

strip_tags is available in the ActionView::Helpers::TextHelper module. http://apidock.com/rails/ActionView/Helpers/TextHelper/strip_tags for more info.


Have yet to work with Sphinx, but judging from your question, it appears that <span class="highlight">..</span> is added by Sphinx upon outputting the text as a search result.

I am currently unaware of any method in RedCloth, but the Vidibus-Textile Gem (based on RedCloth) adds a to_text method, which could be useful for your case.

Ofc you'd need to make sure it's invoked before Sphinx adds the span tags.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜