Make sure html elements are closed when showing a substring on a webpage (ruby on rails)
Let's say I am doing this:
entries.each do |entry|
entry[0,1000] + "..."
end
Let's say that entry has a <ul> and a <li> that were not closed, because it chopped the entry in the middle of a list. How can I make sure to close those tags out, so rendering i开发者_如何学Gosn't messed up?
I was considering creating a method that found the last index of <ul> and made sure it was less than the last index of </ul>, etc. This seems cumbersome though. Any ideas on how to solve this with ruby on rails?
Thanks!
Check out the hpricot gem:
Hpricot("<ul><li>foo<ul><li>bar").to_html
=> "<ul><li>foo<ul><li>bar</li></ul></li></ul>"
精彩评论