views:

354

answers:

2

Hello all,

So there are a few paragraphs separated by line feeds "\r\n" in a textarea, say

Paragraph1 "\r\n"
Paragraph2 "\r\n"
Paragraph3 "\r\n"

what i want to achieve is to process each paragraph into

tag. How do you do that? (Need a server side solution) so it looks like

<p> Paragraph 1 </p>
<p> Paragraph 2 </p>
<p> Paragraph 3 </p>

On top of the complexity, if you have a blockquote there

Paragraph1 "\r\n"
<blockquote> "\r\n"
test
</blockquote> "\r\n"
Paragraph2 "\r\n"
Paragraph3 "\r\n"

should come out to look like

<p> Paragraph 1 </p>
<blockquote>
<p>test</p>
</blockquote>
<p> Paragraph 2 </p>
<p> Paragraph 3 </p>

Thanks a lot

ps: I see stackoverflow does a good job at that.

A: 

You are not creating paragraphs with "\r\n", you're creating line-breaks. If you want paragraphs, use a WYSIWYG editor.

Diodeus
Plenty of sites, including this one, turn line breaks into paragraphs intelligently. WYSIWYG editors on the web can be more trouble than they're worth, too - the HTML is often horrendous, among other things. This is not a helpful answer.
ceejayoz
Oh, does it do BLOCKQUOTES too?
Diodeus
@Diodeus, sorry, I didn't make it clear. Yes, what I'm trying to do is to read a chunk of text submitted through a text area, look at line breaks and convert the user typed "paragraphs" into html "paragraphs" for display purpose. Like @ceejayoz mentioend, I don't want to get into WYSIWYG editor as I don't want to process extra unnecessary stuff. Just a few simple cases. Thanks
Liming
+2  A: 

Maybe it's a bit overkill for what you want to do, but Stackoverflow uses Markdown. There is at least one ASP.NET converter available: Markdown.NET

Pekka
@Pekka. Thanks. Looks like a lot to read through in MarkDown.NET, but at least it's a start and see what I can scrape out of it. Thought, I was hopping someone have a few simple regular expression for that purpose. Surely, someone must've done it. Does everyone simply replace line breaks with "<br/>" tags and be done with it? Not so valid conversion.
Liming
@Liming I'm sure there are simpler approaches, but I'am not a .NET man so I don't know what solutions are available. It's not entirely trivial, though, mind you. You would have to deal with a lot of edge cases (spaces, indentations.....) to make this work reliably.
Pekka