Guest User

Untitled

a guest
Feb 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.21 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.  
  27.  
  28.  
  29.         /// <summary>
  30.         /// Find formatting tags in the text and transform them into the appropriate HTML.
  31.         /// </summary>
  32.         /// <param name="text">The text to be transformed.</param>
  33.         /// <returns>A formatted string.</returns>
  34.         public static string ConvertTagsToHtml(string text)
  35.         {
  36.             text = TransformTags(text, (content, optionalValue, tag) =>
  37.                 {
  38.                     tag = tag.ToLower();
  39.  
  40.                     if (tag.Is("code"))
  41.                         return string.Format("<pre><code>{0}</code></pre>", content);
  42.                     else if (tag.Is("color"))
  43.                         return string.Format("<span style='color: #{0};'>{1}</span>", optionalValue, content);
  44.                     else if (tag.Is("img"))
  45.                     {
  46.                         string imageUrl = ConfirmHttp(content);
  47.                         try
  48.                         {
  49.                             var client = new WebClient();
  50.                             var stream = client.OpenRead(imageUrl);
  51.                             var bitmap = new Bitmap(stream);
  52.                             stream.Flush();
  53.                             stream.Close();
  54.                             var width = Convert.ToDecimal(bitmap.Size.Width);
  55.                             var height = Convert.ToDecimal(bitmap.Size.Height);
  56.                             if (width > 500m)
  57.                             {
  58.                                 var ratio = width / 500m;
  59.                                 height = height / ratio;
  60.                                 width = 500m;
  61.                             }
  62.                             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);
  63.                         }
  64.                         catch
  65.                         {
  66.                             return string.Format("<div><a target='_blank' href='{0}'><img src='{0}' /></a></div>", imageUrl);
  67.                         }
  68.                     }
  69.                     else if (tag.Is("url"))
  70.                         return string.Format("<a target='_blank' href='{0}'>{1}</a>", string.IsNullOrEmpty(optionalValue) ? ConfirmHttp(content) : ConfirmHttp(optionalValue), content);
  71.                     else if (tag.Is("transmit"))
  72.                         return string.Format("<span class='transmit'>{0}</span>", content);
  73.                     else if (tag.Is("quote"))
  74.                         return string.Format("<div><div class='quote'>{0}</div></div>", content);
  75.                     else if (tag.Is("b"))
  76.                         return string.Format("<b>{0}</b>", content);
  77.                     else if (tag.Is("i"))
  78.                         return string.Format("<i>{0}</i>", content);
  79.                     else if (tag.Is("u"))
  80.                         return string.Format("<u>{0}</u>", content);
  81.                     else if (tag.Is("s"))
  82.                         return string.Format("<strike>{0}</strike>", content);
  83.                     else
  84.                         return null;
  85.                 });
  86.  
  87.             return text;
  88.         }
Add Comment
Please, Sign In to add comment