andrew4582

Word Wrap String Extensions

Sep 1st, 2012
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.94 KB | None | 0 0
  1. namespace Core
  2. {
  3.     using System;
  4.     using System.Text;
  5.     using System.Text.RegularExpressions;
  6.  
  7.     public static class WordWrapStringExtensions
  8.     {
  9.         private static string TrimHtmlWhiteSpace(this string html)
  10.         {
  11.             return new Regex("(\\s|\t|\n|\r)+").Replace(html, " ");
  12.         }
  13.  
  14.  
  15.         private static int indentSize = 4, indentLevel = 0;
  16.  
  17.         private static string newLine = Environment.NewLine;
  18.         private static int newLineLength = newLine.Length;
  19.  
  20.         private static string blockElements = "div|p|img|ul|ol|li|h[1-6]|blockquote";
  21.         private static Regex openingBlockRegex = new Regex("<(" + blockElements + ")[^>]*>", RegexOptions.IgnoreCase | RegexOptions.Compiled),
  22.               closingBlockRegex = new Regex("</(" + blockElements + ")>", RegexOptions.IgnoreCase | RegexOptions.Compiled),
  23.               emptyTagRegex = new Regex("^<.*/>$", RegexOptions.Compiled),
  24.               brTagRegex = new Regex("^<br\\s*/>$", RegexOptions.Compiled);
  25.  
  26.  
  27.         public static string IndentTags(this string html, int wrapThreshold = 120)
  28.         {
  29.             bool indenting = true;
  30.  
  31.             html = new Regex("(<[\\w][\\w\\d]*[^>]*>)|(</[\\w][\\w\\d]*>)", RegexOptions.IgnoreCase).Replace(html, new MatchEvaluator(delegate(Match m)
  32.             {
  33.                 var token = m.Groups[0].Value;
  34.                 var result = new StringBuilder();
  35.                 var currentIndent = newLine.PadRight(newLineLength + indentSize * indentLevel);
  36.  
  37.                 if (openingBlockRegex.IsMatch(token))
  38.                 {
  39.                     if (!indenting)
  40.                         result.Append(currentIndent);
  41.  
  42.                     if (!emptyTagRegex.IsMatch(token))
  43.                         ++indentLevel;
  44.  
  45.                     while (token.Length > wrapThreshold)
  46.                     {
  47.                         int splitPoint = token.Substring(0, wrapThreshold).LastIndexOf(' ');
  48.  
  49.                         result.Append(token.Substring(0, splitPoint))
  50.                               .Append(newLine.PadRight(newLineLength + indentSize * (indentLevel + 1)));
  51.  
  52.                         token = token.Substring(splitPoint);
  53.                     }
  54.  
  55.                     result.Append(token)
  56.                           .Append(newLine.PadRight(newLineLength + indentSize * indentLevel));
  57.  
  58.                     indenting = true;
  59.                 }
  60.                 else if (closingBlockRegex.IsMatch(token))
  61.                 {
  62.                     result.Append(newLine.PadRight(newLineLength + indentSize * (--indentLevel)))
  63.                           .Append(token);
  64.                     indenting = false;
  65.                 }
  66.                 else if (brTagRegex.IsMatch(token))
  67.                 {
  68.                     result.Append(token)
  69.                           .Append(newLine.PadRight(newLineLength + indentSize * indentLevel));
  70.                     indenting = false;
  71.                 }
  72.                 else
  73.                 {
  74.                     result.Append(token);
  75.                     indenting = false;
  76.                 }
  77.  
  78.                 return result.ToString();
  79.             }));
  80.  
  81.             return html;
  82.         }
  83.  
  84.         public static string WordWrap(this string html, int wrapThreshold = 120)
  85.         {
  86.             // wrap lines
  87.             var lines = html.Split(new string[] { newLine }, StringSplitOptions.RemoveEmptyEntries);
  88.  
  89.             for (var i = 0; i < lines.Length; i++)
  90.             {
  91.                 if (lines[i].Length <= wrapThreshold)
  92.                     continue;
  93.  
  94.                 var result = new StringBuilder();
  95.  
  96.                 var currentLine = lines[i];
  97.  
  98.                 var currentLineIndentSize = new Regex("^\\s+").Match(currentLine).Length;
  99.  
  100.                 while (currentLine.Length > wrapThreshold)
  101.                 {
  102.                     int splitPoint = currentLine.Substring(0, wrapThreshold - indentSize).LastIndexOf(' ');
  103.  
  104.                     if (splitPoint < 0)
  105.                         splitPoint = wrapThreshold; // cuts though code, though
  106.  
  107.                     result.Append(currentLine.Substring(0, splitPoint))
  108.                           .Append(newLine.PadRight(newLineLength + currentLineIndentSize + indentSize));
  109.  
  110.                     currentLine = currentLine.Substring(splitPoint + 1);
  111.                 }
  112.  
  113.                 result.Append(currentLine);
  114.  
  115.                 lines[i] = result.ToString();
  116.             }
  117.  
  118.             return String.Join(newLine, lines);
  119.         }
  120.  
  121.         public static string IndentHtml(this string html)
  122.         {
  123.             return html.TrimHtmlWhiteSpace()
  124.                        .IndentTags()
  125.                        .WordWrap();
  126.         }
  127.  
  128. #if MVC1 || MVC2
  129.         public static string Raw(this string value)
  130.         {
  131.             return value;
  132.         }
  133.  
  134.         public static System.Web.IHtmlString Raw(this string value)
  135.         {
  136.             return new System.Web.HtmlString(value);
  137.         }
  138. #endif
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment