learn more about riagenic
22 April 2009

ParagraphFormat() ? where art thou..

Interesting night for me, as I’ve spent the last 4 hours fumbling around in the Google dark trying to find a way to take “comments” for the team.silverlight blog, sanitize them but preserve their natural format in which the user typed them.

In that for example, I like to sign my signature in most comments on blogs like this:

-
Scott Barnes
Rich Platforms Product Manager
Microsoft.

3 Lines basically, but to do this in most of the libraries I've seen, they always end up like this:

- Scott Barnes Rich Platforms Product Manager Microsoft.

The only way i can preserve the said format, is to use <pre> tags and via CSS make them look more natural.

The downside, is I then sacrifice basic html which is one feature I want to implement.

Any magical RegEx formulas out there than can handle both shift+linefeed as well as preserve double space line feeds with <p></p> tags I’m all ears.

As the intended format of a comment should end up like this:

start

<p> 
	Your comment goes here 
</p> 
<p> 
	-<br /> 
	Scott Barnes</br> 
	Rich Platforms Product Manager </br> 
	Microsoft</br> 
</p>
Filed under:
 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Damian Edwards said:

Will this do?

public static class StringExtensions

   {

       public static string ParagraphFormat(this string text)

       {

           // Find multiple new-lines 3 or over & change to double new-lines

           var re = new Regex("\n{3,}");

           text = re.Replace(text, "\n\n");

           // Split into paragraphs

           var paragraphs = text.Split(new [] { "\n\n" }, StringSplitOptions.RemoveEmptyEntries);

           var sb = new StringBuilder();

           foreach (var p in paragraphs)

           {

               sb.Append("<p>");

               // Split into lines

               var lines = p.Split('\n');

               bool last = lines.Length == 1;

               var count = 1;

               foreach (var l in lines)

               {

                   sb.Append(l);

                   if (count < lines.Length)

                   {

                       sb.Append("<br />");

                   }

                   count++;

               }

               sb.Append(@"</p>");

           }

           return sb.ToString();

       }

   }

22 April 09 at 3:41 AM
# scbarnes said:

You have much to learn young pup :) hehehehe

Nice code, i added some extras and now i can get the above... untz untz untz..

---

   public static class StringExtensions

   {

       public static string ParagraphFormat(this string text)

       {

           if (String.IsNullOrEmpty(text))

               return "null";

           // Find multiple new-lines 3 or over & change to double new-lines

           var re = new Regex("\n{3,}");

           text = re.Replace(text, "\n\n");

           // Split into paragraphs

           var paragraphs = text.Split(new[] { "\n\n" }, StringSplitOptions.RemoveEmptyEntries);

           var sb = new StringBuilder();

           foreach (var p in paragraphs)

           {

               sb.Append("<p>");

               // Split into lines

               var lines = p.Split('\n');

               bool last = lines.Length == 1;

               var count = 1;

               string prevline = "";

               foreach (var l in lines)

               {

                   sb.Append(l);

                   if (count < lines.Length)

                   {

                       if (l.StartsWith("\r") & prevline.EndsWith("\r"))

                       {

                           sb.Append("</p><p>");

                       }

                       else

                       {

                           sb.Append("<br />");

                       }

                   }

                   count++;

                   prevline = l;

               }

               sb.Append(@"</p>");

           }

           var re2 = new Regex("<(\\w+)>(\\s|&nbsp;)*</\\1>");

           string result = sb.ToString();

           result = re2.Replace(result,"");

           result = result.Replace("\r<br />\r</p>", "</p>");

           return result;

       }

   }

22 April 09 at 4:47 AM
# Joe said:

All these late nights, I wonder what Mr Barnes is working on?

22 April 09 at 9:07 AM

Leave a Comment

Comment Policy: No HTML allowed. URIs and line breaks are converted automatically. Your e–mail address will not show up on any public page.

(required) 
(optional)
(required) 

  
Enter Code Here: Required

About scbarnes

Scott Barnes currently is a Rich Platform Product Manager (WPF & Silverlight). He has been working with Adobe/Macromedia technology for the past 10 years with a main focus specifically on Internet Applications (aka. RIA, Rich Client Technology etc).

Scott first started out as a graphic designer in the late 90’s and over the years developed a passion for programmatic art (Designer + Developer mind). He recently has branched out further into 3D modelling and animation making full use of both his designer + developer mindset.

"..The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man..." - George Bernard Shaw
Page view tracker