使用 Nokogiri,如何将 html 转换为文本块元素(确保它们导致换行)

问题描述 投票:0回答:3

Nokogiri

#content
方法不会将块元素转换为段落;例如:

fragment = 'hell<span>o</span><p>world<p>I am Josh</p></p>'
Nokogiri::HTML(fragment).content
=> "helloworldI am Josh"

我期望输出:

=> "hello\n\nworld\n\nI am Josh"

如何将 html 转换为文本以确保块元素导致换行并且 内联元素 被替换为没有空格?

ruby nokogiri
3个回答
7
投票

您可以使用

#before
#after
来添加换行符:

doc.search('p,div,br').each{ |e| e.after "\n" }

0
投票

这是我的解决方案:

fragment = 'hell<span>o</span><p>world<p>I am Josh</p></p>'
HtmlToText.process(fragment)
=> "hello\n\nworld\n\nI am Josh"

我遍历 nokogiri 树,边走边构建一个文本字符串,将文本包装在

"\n\n"
中用于块元素,而
""
用于内联元素。然后
gsub
在最后清理大量的
\n
字符。这很老套,但很管用。

require 'nokogiri'

class HtmlToText
  class << self
    def process html
      nokogiri = Nokogiri::HTML(html)
      text = ''
      nokogiri.traverse do |el|
        if el.class == Nokogiri::XML::Element
          sep = inline_element?(el) ? "" : "\n"
          if el.children.length <= 0
            text += "#{sep}"
          else 
            text = "#{sep}#{sep}#{text}#{sep}#{sep}"
          end
        elsif el.class == Nokogiri::XML::Text
          text += el.text
        end
      end
      text.gsub(/\n{3,}/, "\n\n").gsub(/(\A\n+)|(\n+\z)/, "")
    end

    private

    def inline_element? el
      el && el.try(:name) && inline_elements.include?(el.name)
    end

    def inline_elements
      %w(
        a abbr acronym b bdo big br button cite code dfn em i img input
        kbd label map object q samp script select small span strong sub
        sup textarea time tt var
      )
    end
  end
end

-2
投票

有一个可靠的宝石:html_to_plain_text

© www.soinside.com 2019 - 2024. All rights reserved.