Really interesting, snb.

Note that your approach doesn't consider the manual linebreaks in the source string.
Perhaps it could be improved (with formatting preserved as well )