Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace MyApp.Extensions
- {
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Web;
- public static class ParseExtensions
- {
- /// <summary>A string extension method that extracts the hash tags contained in value.</summary>
- /// <param name="value">The value to act on.</param>
- /// <returns>The extracted hash tags.</returns>
- public static List<string> ExtractHashTags(this string value)
- {
- return Regex.Matches(value, @"#([A-Za-z])([A-Za-z0-9]*)")
- .Cast<Match>()
- .Select(m => m.Groups[0].Value.Replace("#", string.Empty).Trim())
- .Distinct()
- .ToList();
- }
- /// <summary>A string extension method that parses the bold attributes contained in value </summary>
- /// <param name="value">The value.</param>
- /// <returns>A string.</returns>
- public static string ParseBold(this string value)
- {
- return Regex.Replace(value, @"\*(.*?)\*", AsBoldTag);
- }
- /// <summary>A string extension method that parses the hashtag attributes contained in value</summary>
- /// <param name="value">The value.</param>
- /// <returns>A string.</returns>
- public static string ParseHashtag(this string value)
- {
- return Regex.Replace(value, @"#([A-Za-z])([A-Za-z0-9]*)", AsHashtag);
- }
- /// <summary>A string extension method that parses the italic attributes contained in value</summary>
- /// <param name="value">The value.</param>
- /// <returns>A string.</returns>
- public static string ParseItalic(this string value)
- {
- return Regex.Replace(value, @"_(.*?)_", AsItalicTag);
- }
- /// <summary>A string extension method that parses the link attributes contained in value</summary>
- /// <param name="value">The value.</param>
- /// <returns>A string.</returns>
- public static string ParseLink(this string value)
- {
- return Regex.Replace(value,
- @"(\[)([A-Za-z0-9 -_]*)(\])(\()((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?)(\))",
- AsUrlTag);
- }
- /// <summary>Parses the post.</summary>
- /// <param name="value">The value.</param>
- /// <returns>A string.</returns>
- public static string ParsePost(this string value)
- {
- return value.ParseItalic().ParseBold().ParseHashtag().ParseUsertag().ParseLink();
- }
- /// <summary>Parses the usertag.</summary>
- /// <param name="value">The value.</param>
- /// <returns>A string.</returns>
- public static string ParseUsertag(this string value)
- {
- return Regex.Replace(value, @"@([A-Za-z])([A-Za-z0-9]*)", AsUserTag);
- }
- /// <summary>Expresses the match as a bold tag</summary>
- /// <param name="m">The m.</param>
- /// <returns>A string.</returns>
- private static string AsBoldTag(Match m)
- {
- var value = m.Value;
- var tag = value.Replace("*", string.Empty);
- return string.IsNullOrWhiteSpace(tag) ? string.Empty : tag.FormatSpan("bold");
- }
- /// <summary>Expresses the match as a hashtag</summary>
- /// <param name="m">The m.</param>
- /// <returns>A string.</returns>
- private static string AsHashtag(Match m)
- {
- var value = m.Value;
- var tag = value.Replace("#", string.Empty);
- var url = VirtualPathUtility.ToAbsolute($"~/Hashtag/{tag}");
- return string.IsNullOrWhiteSpace(tag) ? string.Empty : tag.FormatLink(url, "hashtag-link", "hashtag");
- }
- /// <summary>Expresses the match as an italic tag</summary>
- /// <param name="m">The m.</param>
- /// <returns>A string.</returns>
- private static string AsItalicTag(Match m)
- {
- var value = m.Value;
- var tag = value.Replace("_", string.Empty);
- return string.IsNullOrWhiteSpace(tag) ? string.Empty : tag.FormatSpan("italic");
- }
- /// <summary>Expresses the match as a URL tag</summary>
- /// <param name="m">The m.</param>
- /// <returns>A string.</returns>
- private static string AsUrlTag(Match m)
- {
- var desc = m.Groups[2].Value;
- var url = m.Groups[5].Value;
- return string.IsNullOrWhiteSpace(url) ? string.Empty : desc.FormatLink(url, string.Empty, "link");
- }
- /// <summary>Expresses the match as a user tag</summary>
- /// <param name="m">The m.</param>
- /// <returns>A string.</returns>
- private static string AsUserTag(Match m)
- {
- var value = m.Value;
- var tag = value.Replace("@", string.Empty);
- var url = VirtualPathUtility.ToAbsolute($"~/Profile/{tag}");
- return string.IsNullOrWhiteSpace(tag) ? string.Empty : tag.FormatLink(url, "usertag-link", "at");
- }
- /// <summary>A string extension method that formats the link.</summary>
- /// <param name="value">The value.</param>
- /// <param name="url">URL of the document.</param>
- /// <param name="className">Name of the class.</param>
- /// <param name="faIcon"></param>
- /// <returns>The formatted link.</returns>
- private static string FormatLink(this string value, string url, string className, string faIcon)
- {
- var cssClass = string.IsNullOrWhiteSpace(className)
- ? string.Empty
- : $" class=\"{className}\"";
- return $"<a rel=\"canonical\" href=\"{url}\"{cssClass}><i class=\"fa fa-{faIcon} fa-1x\"></i><span>{value}</span></a>";
- }
- /// <summary>A string extension method that format span.</summary>
- /// <param name="value">The value.</param>
- /// <param name="className">Name of the class.</param>
- /// <returns>The formatted span.</returns>
- private static string FormatSpan(this string value, string className)
- {
- var cssClass = string.IsNullOrWhiteSpace(className)
- ? string.Empty
- : $" class=\"{className}\"";
- return $"<span{cssClass}>{value}</span>";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement