Weird but true: If you don't care about it validating, you don't need a closing </p> tag in html4. But if you have multiple <p> statements there will be multiple empty lines.
If you can put <br> in your output then you can put <p> or <p class = "long complicated list of conditions"> or <a href = "worldsbestsite.com"> or anything else you want ;)
Now, as already noted, I don't speak the language, but if you can identify spaces in the text area can't you identify line breaks? Trim away any leading or trailing ones. Then you have <p> at the beginning, </p>\r\n<p> replacing each set of (\r\n)+ along the way, and a final </p> at the end.
Does the replacement just run once, or can you make a "while" type of iteration? You want it structured as "do such-and-such until you run out of things to do it to".