namespace MyApp.Extensions { using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Web; public static class ParseExtensions { /// A string extension method that extracts the hash tags contained in value. /// The value to act on. /// The extracted hash tags. public static List ExtractHashTags(this string value) { return Regex.Matches(value, @"#([A-Za-z])([A-Za-z0-9]*)") .Cast() .Select(m => m.Groups[0].Value.Replace("#", string.Empty).Trim()) .Distinct() .ToList(); } /// A string extension method that parses the bold attributes contained in value /// The value. /// A string. public static string ParseBold(this string value) { return Regex.Replace(value, @"\*(.*?)\*", AsBoldTag); } /// A string extension method that parses the hashtag attributes contained in value /// The value. /// A string. public static string ParseHashtag(this string value) { return Regex.Replace(value, @"#([A-Za-z])([A-Za-z0-9]*)", AsHashtag); } /// A string extension method that parses the italic attributes contained in value /// The value. /// A string. public static string ParseItalic(this string value) { return Regex.Replace(value, @"_(.*?)_", AsItalicTag); } /// A string extension method that parses the link attributes contained in value /// The value. /// A string. public static string ParseLink(this string value) { return Regex.Replace(value, @"(\[)([A-Za-z0-9 -_]*)(\])(\()((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)(\))", AsUrlTag); } /// Parses the post. /// The value. /// A string. public static string ParsePost(this string value) { return value.ParseItalic().ParseBold().ParseHashtag().ParseUsertag().ParseLink(); } /// Parses the usertag. /// The value. /// A string. public static string ParseUsertag(this string value) { return Regex.Replace(value, @"@([A-Za-z])([A-Za-z0-9]*)", AsUserTag); } /// Expresses the match as a bold tag /// The m. /// A string. 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"); } /// Expresses the match as a hashtag /// The m. /// A string. 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"); } /// Expresses the match as an italic tag /// The m. /// A string. 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"); } /// Expresses the match as a URL tag /// The m. /// A string. 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"); } /// Expresses the match as a user tag /// The m. /// A string. 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"); } /// A string extension method that formats the link. /// The value. /// URL of the document. /// Name of the class. /// /// The formatted link. private static string FormatLink(this string value, string url, string className, string faIcon) { var cssClass = string.IsNullOrWhiteSpace(className) ? string.Empty : $" class=\"{className}\""; return $"{value}"; } /// A string extension method that format span. /// The value. /// Name of the class. /// The formatted span. private static string FormatSpan(this string value, string className) { var cssClass = string.IsNullOrWhiteSpace(className) ? string.Empty : $" class=\"{className}\""; return $"{value}"; } } }