【问题标题】:Ruby, Textile: how to get plain text without tags and textile formatRuby, Textile:如何获取没有标签和纺织格式的纯文本
【发布时间】:2011-10-13 16:52:34
【问题描述】:

我的文章正文是纺织格式的文本。我用 sphinx 进行全文搜索,我想在结果中突出显示查询。狮身人面像给我这样的东西

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

我想删除所有纺织格式但保存突出显示标签,像这样

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

Redcloth gem 可以吗?

【问题讨论】:

    标签: ruby-on-rails ruby highlighting textile redcloth


    【解决方案1】:

    尚未使用 Sphinx,但从您的问题来看,&lt;span class="highlight"&gt;..&lt;/span&gt; 似乎是 Sphinx 在将文本输出为搜索结果时添加的。

    我目前不知道 RedCloth 中有任何方法,但 Vidibus-Textile Gem(基于 RedCloth)添加了一个 to_text 方法,这可能对您的情况有用。

    您需要确保在Sphinx 添加span 标签之前调用它。

    【讨论】:

    • 另外,定义自定义输入模式(参见redcloth.org/faq)可能是一种选择。尽管将 &lt;span class="highlight"&gt;..&lt;/span&gt; 视为正在处理的纺织图案恢复到自身似乎有点不干净:)
    【解决方案2】:

    您可以使用 RedCloth 处理内容,然后剥离所有标签的结果。所以像:

    strip_tags (RedCloth.new(simple_input).to_html)
    

    strip_tags 在 ActionView::Helpers::TextHelper 模块中可用。 http://apidock.com/rails/ActionView/Helpers/TextHelper/strip_tags 了解更多信息。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多