[英]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"
How get the second result?如何获得第二个结果?
Return返回
doc.at("div").text
instead of代替
doc.at("div").inner_html
This, however, strips all HTML from the result.但是,这会从结果中删除所有 HTML。 If you need to retain other markup, you can probably get away with using CGI.unescapeHTML:如果您需要保留其他标记,您可以使用 CGI.unescapeHTML:
CGI.unescapeHTML(doc.at("div").inner_html)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.