开发者

Nokogiri prevent converting entities

def wrap(content)
  require "Nokogiri"
  doc = Nokogiri::HTML.fragment("<div>"+content+"</div>")
  chunks = doc.at("div").traverse do |p|
    if p.is_a?(Nokogiri::XML::Text)
      input = p.content
      p.content = input.scan(/.{1,5}/).join("&shy;")
    end
  end
  doc.at("div").inner_html
end

wrap("aaaaaaaaaa")

gives me

"aaaaa&amp;shy;aaaaa"

instead of

"aaaaa&shy;aaaaa"

H开发者_如何学Cow get the second result ?


Return

doc.at("div").text

instead of

doc.at("div").inner_html

This, however, strips all HTML from the result. If you need to retain other markup, you can probably get away with using CGI.unescapeHTML:

CGI.unescapeHTML(doc.at("div").inner_html)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜