Partager via


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>

Comments

  • Anonymous
    April 21, 2009
    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, "nn");            // Split into paragraphs            var paragraphs = text.Split(new [] { "nn" }, 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();        }    }

  • Anonymous
    April 21, 2009
    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, "nn");            // Split into paragraphs            var paragraphs = text.Split(new[] { "nn" }, 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;        }    }

  • Anonymous
    April 22, 2009
    All these late nights, I wonder what Mr Barnes is working on?