Rails:智能文本截断
我想知道是否有一个插件可以启用某种智能截断。 我需要以单词或句子的精度截断文本。
例如:
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 ...
I wonder if there's a plugin to enable a sort of smart truncation. I need to truncate my text with a precision of a word or of a sentence.
For example:
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.
or
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 ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我还没有见过这样的插件,但是有一个类似的问题可以作为库或辅助函数的基础。
您显示该函数的方式似乎将其作为 String 的扩展:除非您确实希望能够在视图之外执行此操作,否则我倾向于使用
application_helper.rb. 也许是这样的?
I haven't seen such a plugin, but there was a similar question that could serve as the basis for a lib or helper function.
The way you show the function seems to put it as an extension to String: unless you really want to be able to do this outside of a view, I'd be inclined to go for a function in
application_helper.rb
. Something like this, perhaps?这将根据指定的 char_limit 长度在字边界处截断。 所以它不会在奇怪的地方截断句子
This will truncate at the word boundary based on the char_limit length specified. So it will not truncate the sentence at weird places
gem truncate_html 可以完成这项工作。 它还可以跳过 HTML 片段(这非常有用),并提供自定义单词边界正则表达式的可能性。 此外,所有参数的默认值都可以在
config/environment.rb
中进行配置。示例:
The gem truncate_html does this job. It also can skip over pieces of HTML – which can be quiet useful – and offers the possibility to customize the word boundary regex. Furthermore the defaults for all parameters can be configured eg in your
config/environment.rb
.Example:
好帮手。 由于我有不同的经历,我确实对其进行了更改,以便它在最后一个单词处停止并使用字符限制。 我认为这在大多数应用程序中都是更真实的场景。
更新:采用上面的代码并对其进行了一些更新。 对于旧的 ruby 来说似乎更好,并且可以与 utf8 一起使用。
Nice helper. Since I had a different experience I did change it so that it stops on the last word and work with character limit. I think this is much more real world scenario in most apps.
Update: Took the code above and updated it a bit. Seemed much nicer approach for old ruby and works with utf8.
现在,我在不同项目的一些更新上工作了相当长的一段时间,我对代码进行了这些改进,这些改进在现实生活场景中似乎更有用。
Working for quite a while now on some updates with different projects and I came up with these refinements of the code that seem much more usable in real life scenarios.