Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserve newlines with Nokogiri

I am using Nokogiri in a Rails 3 app. It ignores

<br/> 

tags. I'd like to replace such tags with ", " because they represent line breaks in addresses. How do I do this? I've tried the following, which doesn't seem to help:

  doc.inner_html.gsub!("<br/>", ", ")
like image 371
alpheus Avatar asked Oct 24 '25 18:10

alpheus


1 Answers

Simply:

doc.css('br').each{ |br| br.replace ", " }

Seen in action:

require 'nokogiri'
doc = Nokogiri.HTML('<address>900 Magnolia Road<br/>Nederland, CO<br/>80466</address>')
puts doc.root
#=> <html><body><address>900 Magnolia Road<br>Nederland, CO<br>80466</address></body></html>

doc.css('br').each{ |br| br.replace ", " }
puts doc.root
#=> <html><body><address>900 Magnolia Road, Nederland, CO, 80466</address></body></html>

If you want to be more careful and only replace the <br> inside an <address> tag (for example), then:

doc.css('address > br').each{ |br| br.replace ", " }
like image 52
Phrogz Avatar answered Oct 27 '25 08:10

Phrogz