10

我想知道是否有一个插件可以启用一种智能截断。我需要以一个单词或一个句子的精度截断我的文本。

例如:

Post.my_message.smart_truncate(
    "Once upon a time in a world far far away. And they found that many people
     were sleeping better.", :sentences => 1)
# => Once upon a time in a world far far away.

或者

Post.my_message.smart_truncate(
    "Once upon a time in a world far far away. And they found that many people
     were sleeping better.", :words => 12)
# => Once upon a time in a world far far away. And they ...
4

5 回答 5

20

我还没有见过这样的插件,但是有一个类似的问题可以作为 lib 或辅助函数的基础。

您显示该函数的方式似乎将其作为 String 的扩展:除非您真的希望能够在视图之外执行此操作,否则我倾向于在application_helper.rb. 大概是这样的吧?

module ApplicationHelper

  def smart_truncate(s, opts = {})
    opts = {:words => 12}.merge(opts)
    if opts[:sentences]
      return s.split(/\.(\s|$)+/)[0, opts[:sentences]].map{|s| s.strip}.join('. ') + '.'
    end
    a = s.split(/\s/) # or /[ ]+/ to only split on spaces
    n = opts[:words]
    a[0...n].join(' ') + (a.size > n ? '...' : '')
  end
end

smart_truncate("a b c. d e f. g h i.", :sentences => 2) #=> "a b c. d e f."
smart_truncate("apple blueberry cherry plum", :words => 3) #=> "apple blueberry cherry..."
于 2009-08-18T13:20:15.533 回答
8

这将根据指定的 char_limit 长度在单词边界处截断。所以它不会在奇怪的地方截断句子

def smart_truncate(text, char_limit)
    size = 0
    text.split().reject do |token|
      size += token.size() + 1
      size > char_limit
    end.join(' ') + ( text.size() > char_limit ? ' ' + '...' : '' )
end
于 2010-10-08T08:51:53.163 回答
1

gem truncate_html完成这项工作。它还可以跳过 HTML 片段——这可能非常有用——并提供自定义单词边界正则表达式的可能性。此外,可以配置所有参数的默认值,例如在您的config/environment.rb.

例子:

some_html = '<ul><li><a href="http://whatever">This is a link</a></li></ul>'

truncate_html(some_html, :length => 15, :omission => '...(continued)')
=> <ul><li><a href="http://whatever">This...(continued)</a></li></ul>
于 2013-08-06T07:56:47.150 回答
0

好帮手。由于我有不同的体验,我确实对其进行了更改,使其停在最后一个单词上并使用字符限制。我认为这是大多数应用程序中更真实的场景。

更新:获取上面的代码并对其进行了一些更新。对于旧 ruby​​ 和 utf8 来说似乎更好的方法。

def smart_truncate(text, char_limit)
  text = text.squish
  size = 0
  text.mb_chars.split().reject do |token|
    size+=token.size()
    size>char_limit
  end.join(" ")
end
于 2012-09-19T09:31:48.193 回答
0

现在已经为不同项目的一些更新工作了很长一段时间,我想出了这些代码的改进,这些改进似乎在现实生活场景中更有用。

def smart_truncate_characters(text, char_limit)
  text = text.to_s
  text = text.squish
  size = 0
  new_text = text.mb_chars.split().reject do |token|
    size+=token.size()
    size>char_limit
  end.join(" ")
  if size > char_limit
    return new_text += '…'
  else
    return new_text
  end
end

def smart_truncate_sentences(text, sentence_limit)
  text = text.to_s
  text = text.squish
  size = 0
  arr = text.mb_chars.split(/(?:\.|\?|\!)(?= [^a-z]|$)/)
  arr = arr[0...sentence_limit]
  new_text = arr.join(".")
  new_text += '.'
end

def smart_truncate(text, sentence_limit, char_limit)
  text =  smart_truncate_sentences(text, sentence_limit)
  text =  smart_truncate_characters(text, char_limit)
end
于 2013-05-22T07:56:35.743 回答