This seems like such a common situation that I can't believe I haven't found an easy recipe. What is the best way to close an unclosed tag in user-submitted input? It doesn't appear that HPricot has a built-in method for this, though HPricot looks like the most likely tool to use. White_list and RedCloth also don't appear to solve this. What else should I investigate? TIA!
You can run the html through Tidy. Using Mac ports, it'd be `port install tidy`.
Tidy also fits nicely into functional tests for your own html. I've used it in continuous builder setups where we needed the whole team, some of them not very html savvy but still good programmers otherwise, to contribute valid html.
One problem I encountered however was how to validate partials. To solve that I would sample the first tag from a partial and then wrap it with whatever was required to become (minimally) valid HTML. With a little recursion you can go upward from a <td> partial to a valid? html document, then run that against tidy.
It doesn't appear that HPricot has a built-in method for this
I should have looked a bit longer. HPricot makes this trivial:
Hpricot("A <b>closed</b> tag and an <u>unclosed tag").to_html
=> "A <b>closed</b> tag and an <u>unclosed tag</u>"