5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/htmlize.rb', line 5
def self.htmlize(content)
return nil if content.nil?
return '' if content.empty?
document = Nokogiri::HTML(content)
body = document.xpath('html/body').first
contents = tree_as_set(body){ |node| node.text? && !node.content.match(/^\s+$/) }
contents.each do |node|
texts = node.content.split("\n\n")
tag = node.parent.name
node.parent.replace(texts.map{ |text| "<#{tag}>" + text.gsub("\n", "<br>\n") + "</#{tag}>" }.join("\n\n"))
end
body.children.to_html
end
|