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("­")
    end
  end
  doc.at("div").inner_html
end
wrap("aaaaaaaaaa")
gives me
"aaaaa&shy;aaaaa"
instead of
"aaaaa­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)
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论