使用Nokogiri在结束标记之前添加文本

时间:2015-05-10 19:19:16

标签: ruby-on-rails ruby nokogiri

我正在使用基于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>

1 个答案:

答案 0 :(得分:0)

由于您没有提供任何输入数据,您可以从中进行插值:

require 'nokogiri'

doc = Nokogiri::HTML(<<EOT)
<html>
<body>
<p>foo bar baz</p>
</body>
</html>
EOT

paragraph = doc.at('p')
text = paragraph.text
text[4..-1] = '...'
paragraph.content = text
puts doc.to_html
# >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# >> <html>
# >> <body>
# >> <p>foo ...</p>
# >> </body>
# >> </html>

你让它变得比实际更难。 Nokogiri足够聪明,可以知道我们是通过标记还是简单地发送文本,而content将创建一个文本节点或元素,具体取决于它是什么。

此代码简单:

  1. 查找p代码。
  2. 从中提取文字。
  3. 使用'...'替换给定点到最后的文本。
  4. 用该文字替换段落的内容。
  5. 如果您只想附加到该文本,那就更容易了:

    paragraph = doc.at('p')
    paragraph.content = paragraph.text + ' ...Read more.'
    puts doc.to_html
    # >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    # >> <html>
    # >> <body>
    # >> <p>foo bar baz ...Read more.</p>
    # >> </body>
    # >> </html>