Advertisement
Chevex

Untitled

Oct 27th, 2011
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.31 KB | None | 0 0
  1.         /// <summary>
  2.         /// Match all tags in the text with a regular expression.
  3.         /// </summary>
  4.         /// <param name="input">The text to be matched against.</param>
  5.         /// <param name="handleTags">A function to interpret and transform tags.</param>
  6.         /// <returns>Text with transformed tags.</returns>
  7.         private static string TransformTags(string input, Func<string, string, string, string> handleTags)
  8.         {
  9.             var regexOptions = RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline;
  10.             input = Regex.Replace(input, RegularExpressions.BBCodeTags, new MatchEvaluator(match =>
  11.             {
  12.                 var originalText = match.Groups[0].Value;
  13.                 var tag = match.Groups[1].Value;
  14.                 var optionalValue = match.Groups[2].Value;
  15.                 var content = match.Groups[3].Value;
  16.                 content = TransformTags(content, handleTags);
  17.                 content = handleTags(content, optionalValue, tag) ?? originalText;
  18.                 return content;
  19.             }), regexOptions);
  20.  
  21.             return input;
  22.         }
  23.  
  24. ------------------------------
  25.  
  26.         /// <summary>
  27.         /// Find formatting tags in the text and transform them into the appropriate HTML.
  28.         /// </summary>
  29.         /// <param name="text">The text to be transformed.</param>
  30.         /// <returns>A formatted string.</returns>
  31.         public static string ConvertTagsToHtml(string text)
  32.         {
  33.             text = TransformTags(text, (content, optionalValue, tag) =>
  34.                 {
  35.                     tag = tag.ToLower();
  36.  
  37.                     if (tag.Is("code"))
  38.                         return string.Format("<pre><code>{0}</code></pre>", content);
  39.                     else if (tag.Is("color"))
  40.                         return string.Format("<span style='color: #{0};'>{1}</span>", optionalValue, content);
  41.                     else if (tag.Is("img"))
  42.                     {
  43.                         string imageUrl = ConfirmHttp(content);
  44.                         try
  45.                         {
  46.                             var client = new WebClient();
  47.                             var stream = client.OpenRead(imageUrl);
  48.                             var bitmap = new Bitmap(stream);
  49.                             stream.Flush();
  50.                             stream.Close();
  51.                             var width = Convert.ToDecimal(bitmap.Size.Width);
  52.                             var height = Convert.ToDecimal(bitmap.Size.Height);
  53.                             if (width > 500m)
  54.                             {
  55.                                 var ratio = width / 500m;
  56.                                 height = height / ratio;
  57.                                 width = 500m;
  58.                             }
  59.                             return string.Format("<div style='height: {0}px; width: {1}px;'><a target='_blank' href='{2}'><img style='height: {0}px; width: {1}px;' src='{2}' /></a></div>", height, width, imageUrl);
  60.                         }
  61.                         catch
  62.                         {
  63.                             return string.Format("<div><a target='_blank' href='{0}'><img src='{0}' /></a></div>", imageUrl);
  64.                         }
  65.                     }
  66.                     else if (tag.Is("url"))
  67.                         return string.Format("<a target='_blank' href='{0}'>{1}</a>", string.IsNullOrEmpty(optionalValue) ? ConfirmHttp(content) : ConfirmHttp(optionalValue), content);
  68.                     else if (tag.Is("transmit"))
  69.                         return string.Format("<span class='transmit'>{0}</span>", content);
  70.                     else if (tag.Is("quote"))
  71.                         return string.Format("<div><div class='quote'>{0}</div></div>", content);
  72.                     else if (tag.Is("b"))
  73.                         return string.Format("<b>{0}</b>", content);
  74.                     else if (tag.Is("i"))
  75.                         return string.Format("<i>{0}</i>", content);
  76.                     else if (tag.Is("u"))
  77.                         return string.Format("<u>{0}</u>", content);
  78.                     else if (tag.Is("s"))
  79.                         return string.Format("<strike>{0}</strike>", content);
  80.                     else
  81.                         return null;
  82.                 });
  83.  
  84.             return text;
  85.         }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement