【发布时间】:2015-05-10 19:19:16
【问题描述】:
我正在使用基于 Nokogiri 的助手来截断文本而不破坏 HTML 标签:
require "rubygems"
require "nokogiri"
module TextHelper
def truncate_html(text, max_length, ellipsis = "...")
ellipsis_length = ellipsis.length
doc = Nokogiri::HTML::DocumentFragment.parse text
content_length = doc.inner_text.length
actual_length = max_length - ellipsis_length
content_length > actual_length ? doc.truncate(actual_length).inner_html + ellipsis : text.to_s
end
end
module NokogiriTruncator
module NodeWithChildren
def truncate(max_length)
return self if inner_text.length <= max_length
truncated_node = self.dup
truncated_node.children.remove
self.children.each do |node|
remaining_length = max_length - truncated_node.inner_text.length
break if remaining_length <= 0
truncated_node.add_child node.truncate(remaining_length)
end
truncated_node
end
end
module TextNode
def truncate(max_length)
Nokogiri::XML::Text.new(content[0..(max_length - 1)], parent)
end
end
end
Nokogiri::HTML::DocumentFragment.send(:include, NokogiriTruncator::NodeWithChildren)
Nokogiri::XML::Element.send(:include, NokogiriTruncator::NodeWithChildren)
Nokogiri::XML::Text.send(:include, NokogiriTruncator::TextNode)
开启
content_length > actual_length ? doc.truncate(actual_length).inner_html + ellipsis : text.to_s
它在最后一个标签之后附加一个椭圆。
在我看来,我打电话给
<%= truncate_html(news.parsed_body, 700, "... Read more.").html_safe %>
问题是正在解析的文本被包裹在<p></p>标签中,导致视图中断:
"Lorem Ipsum</p>
... Read More"
是否可以使用Nokogiri将椭圆附加到最后一个节点的最后部分,所以最终输出变为:
"Loren Ipsum... Read More</p>
【问题讨论】:
-
不要将
truncate_html放在您的视图中。预处理控制器中的数据,将其分配给变量并在视图中仅访问该变量。
标签: ruby-on-rails ruby nokogiri