Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Match all tags in the text with a regular expression.
- /// </summary>
- /// <param name="input">The text to be matched against.</param>
- /// <param name="handleTags">A function to interpret and transform tags.</param>
- /// <returns>Text with transformed tags.</returns>
- private static string TransformTags(string input, Func<string, string, string, string> handleTags)
- {
- var regexOptions = RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline;
- input = Regex.Replace(input, RegularExpressions.BBCodeTags, new MatchEvaluator(match =>
- {
- var originalText = match.Groups[0].Value;
- var tag = match.Groups[1].Value;
- var optionalValue = match.Groups[2].Value;
- var content = match.Groups[3].Value;
- content = TransformTags(content, handleTags);
- content = handleTags(content, optionalValue, tag) ?? originalText;
- return content;
- }), regexOptions);
- return input;
- }
- ------------------------------
- /// <summary>
- /// Find formatting tags in the text and transform them into the appropriate HTML.
- /// </summary>
- /// <param name="text">The text to be transformed.</param>
- /// <returns>A formatted string.</returns>
- public static string ConvertTagsToHtml(string text)
- {
- text = TransformTags(text, (content, optionalValue, tag) =>
- {
- tag = tag.ToLower();
- if (tag.Is("code"))
- return string.Format("<pre><code>{0}</code></pre>", content);
- else if (tag.Is("color"))
- return string.Format("<span style='color: #{0};'>{1}</span>", optionalValue, content);
- else if (tag.Is("img"))
- {
- string imageUrl = ConfirmHttp(content);
- try
- {
- var client = new WebClient();
- var stream = client.OpenRead(imageUrl);
- var bitmap = new Bitmap(stream);
- stream.Flush();
- stream.Close();
- var width = Convert.ToDecimal(bitmap.Size.Width);
- var height = Convert.ToDecimal(bitmap.Size.Height);
- if (width > 500m)
- {
- var ratio = width / 500m;
- height = height / ratio;
- width = 500m;
- }
- 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);
- }
- catch
- {
- return string.Format("<div><a target='_blank' href='{0}'><img src='{0}' /></a></div>", imageUrl);
- }
- }
- else if (tag.Is("url"))
- return string.Format("<a target='_blank' href='{0}'>{1}</a>", string.IsNullOrEmpty(optionalValue) ? ConfirmHttp(content) : ConfirmHttp(optionalValue), content);
- else if (tag.Is("transmit"))
- return string.Format("<span class='transmit'>{0}</span>", content);
- else if (tag.Is("quote"))
- return string.Format("<div><div class='quote'>{0}</div></div>", content);
- else if (tag.Is("b"))
- return string.Format("<b>{0}</b>", content);
- else if (tag.Is("i"))
- return string.Format("<i>{0}</i>", content);
- else if (tag.Is("u"))
- return string.Format("<u>{0}</u>", content);
- else if (tag.Is("s"))
- return string.Format("<strike>{0}</strike>", content);
- else
- return null;
- });
- return text;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement