【发布时间】:2011-08-24 07:41:20
【问题描述】:
我使用截断方法截断站点中的字符串。我用代码截断:
truncate(auto_link(textilize(post.content)), :length => 140)
此截断会截断帖子内容,但内容包含链接存在问题。 EG帖子内容是:
<p>Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo
<a href="http://www.yahoo.com">Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo
Yahoo </a> Google Google Google Google Google Google Google Google Google Google <a
href="http://www.google.com"> Google Google Google Google Google Google Google Google
</a><br></p>
在这种情况下,所有 html 标签和链接都通过 truncate 方法计入字符数,而不是 truncate 方法计算查看者实际看到的字符数:
Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo
Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo Yahoo (etc..)
截断方法在这里有什么解决方法吗?
编辑:消除歧义的新示例:
用户以纯文本形式发布他们的帖子,并允许使用纺织品进行某些格式设置。在发布链接方面,我允许用户 (a) 粘贴纯 URL(http://www.site.com、www.site.com 等),(b) 使用可转换“点击我”的纺织语法text":http://www.example.com 到 the click me text 或花时间以 html 形式嵌入链接。
我希望一个包含某人帖子的 sn-ps 的页面仅显示他们帖子的前 140 个字符。但我希望这是读者会看到的文本的 140 个字符。换句话说,假设用户的帖子开头为:
Today I went to the market to pick up some fruit. "At the market there was this awesome
fruit display!":http://www.externalsite.com/picture.jpg Definitely want to go back
tomorrow!
这会被textilize转换成193个字符的字符串:
Today I went to the market to pick up some fruit. <a href="http://www.externalsite.com/picture.jpg">
At the market there was this awesome fruit display!</a> Definitely want to go back
tomorrow!
用户将在页面上看到的文本只有 138 个字符:
Today I went to the market to pick up some fruit. At the market there was this awesome
fruit display! Definitely want to go back tomorrow!
193 个字符的长文本将被 truncate 截断,但如果我对整个文本进行处理,将显示整个文本,最终显示为只有 138 个字符并嵌入了链接。
【问题讨论】:
-
好吧,我想我可以删除 sn-p 的链接——从长远来看,这可能是明智的。但我现在也有兴趣学习如何解决这个问题。我正在添加 auto_link 因为我允许用户粘贴链接(例如,以纯文本形式输入“www.google.com”)并希望它暂时成为可点击的链接。我正在添加一些编辑在这个问题上也更具体。
-
嗯好的 - 谢谢你的想法
标签: html ruby-on-rails ruby-on-rails-3 tags truncate