Grafting an HTML string to a node (appending a child tree)

169 views
Skip to first unread message

Mark Reginald James

unread,
Feb 17, 2010, 5:12:52 AM2/17/10
to nokogiri-talk
Hello,

I want to display blog post excerpts, and I'm using Nokogiri to parse
the post body so that I can properly truncate the HTML, and then graft
an arbitrary HTML "read more" string as extra children of the last
node parent (so that the read-more is displayed inline).

But I've been unable to work out how to do this easily. Do I have to
write a recursive node-adding method?

I've searched this list, but haven't been able to find an answer.

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/348298
http://groups.google.com/group/nokogiri-talk/browse_thread/thread/8111ca56fa49545d

I've tried "parent.inner_html += more_html", and the "after" method,
but neither worked.

Thanks.

Mike Dalessio

unread,
Feb 17, 2010, 12:19:02 PM2/17/10
to nokogiri-talk
On Wed, Feb 17, 2010 at 5:12 AM, Mark Reginald James <m...@bigpond.net.au> wrote:
Hello,

I want to display blog post excerpts, and I'm using Nokogiri to parse
the post body so that I can properly truncate the HTML, and then graft
an arbitrary HTML "read more" string as extra children of the last
node parent (so that the read-more is displayed inline).

But I've been unable to work out how to do this easily.

Are you having issues with the truncation, or the adding of a node? What have you tried?

 
Do I have to
write a recursive node-adding method?

You can add HTML to <body> by doing something like this:

doc.at_css("body").children.last.after("<b>hello</b>")

Aaron: what do you think about making #add_child accept a string like #after and #before? Seems like the API's a little messy here, like #add_next_sibling and #after being different implementations. I'd love to clean it up.

 

I've searched this list, but haven't been able to find an answer.

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/348298
http://groups.google.com/group/nokogiri-talk/browse_thread/thread/8111ca56fa49545d

I've tried "parent.inner_html += more_html", and the "after" method,
but neither worked.

Thanks.

--
You received this message because you are subscribed to the Google Groups "nokogiri-talk" group.
To post to this group, send email to nokogi...@googlegroups.com.
To unsubscribe from this group, send email to nokogiri-tal...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nokogiri-talk?hl=en.




--
mike dalessio
mi...@csa.net

Mark Reginald James

unread,
Mar 12, 2010, 12:10:20 AM3/12/10
to nokogiri-talk
On Feb 18, 4:19 am, Mike Dalessio <mike.dales...@gmail.com> wrote:
> On Wed, Feb 17, 2010 at 5:12 AM, Mark Reginald James <m...@bigpond.net.au>wrote:
>
> > Hello,
>
> > I want to display blog post excerpts, and I'm using Nokogiri to parse
> > the post body so that I can properly truncate the HTML, and then graft
> > an arbitrary HTML "read more" string as extra children of the last
> > node parent (so that the read-more is displayed inline).
>
> > But I've been unable to work out how to do this easily.
>
> You can add HTML to <body> by doing something like this:
>
> doc.at_css("body").children.last.after("<b>hello</b>")

Thanks Mike for the pointer to "after". I'd tried it without success,
but your post induced me to persist until I got it working.

The working code for an inline graft of an HTML "Read More" fragment
was

[HTML-aware truncation of the document]
body = doc.root.children.first
last_node = body.children.last
last_node_children = last_node.children
last_node = last_node_children.last unless
last_node_children.empty?
last_node.after(more_html)
body.inner_html


Reply all
Reply to author
Forward
0 new messages