Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Processing just HTML fragment and returning it

Tags:

ruby

nokogiri

When I do the following with Nokogiri:

some_html = '<img src="bleh.jpg"/>test<br/>'
f = Nokogiri::HTML(some_html)
#do some processing
puts f

It will print the whole XHTML doc structure with the upper code in it.

How can I just print/return/get the html part which is in some_html variable?


No.

f will return:

"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www
.w3.org/TR/REC-html40/loose.dtd\">\n<html><body>\n<img src=\"bleh.jpg\">test<br>\n
</body></html>\n"

I only want the inner/fragment part:

<img src=\"bleh.jpg\">test<br>
like image 740
all jazz Avatar asked Nov 04 '09 14:11

all jazz


1 Answers

Instead of parsing using Nokogiri::HTML(...) use Nokogiri::HTML::fragment(...):

asdf = Nokogiri::HTML::fragment('<img src="bleh.jpg">test<br>')
print asdf.to_html
# >> <img src="bleh.jpg">test<br>
like image 85
the Tin Man Avatar answered Oct 14 '22 15:10

the Tin Man