Advertisement
Willcode4cash

Parse to HTML

Sep 17th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.42 KB | None | 0 0
  1. namespace MyApp.Extensions
  2. {
  3.     using System.Collections.Generic;
  4.     using System.Linq;
  5.     using System.Text.RegularExpressions;
  6.     using System.Web;
  7.  
  8.     public static class ParseExtensions
  9.     {
  10.         /// <summary>A string extension method that extracts the hash tags contained in value.</summary>
  11.         /// <param name="value">The value to act on.</param>
  12.         /// <returns>The extracted hash tags.</returns>
  13.         public static List<string> ExtractHashTags(this string value)
  14.         {
  15.             return Regex.Matches(value, @"#([A-Za-z])([A-Za-z0-9]*)")
  16.                 .Cast<Match>()
  17.                 .Select(m => m.Groups[0].Value.Replace("#", string.Empty).Trim())
  18.                 .Distinct()
  19.                 .ToList();
  20.         }
  21.  
  22.         /// <summary>A string extension method that parses the bold attributes contained in value </summary>
  23.         /// <param name="value">The value.</param>
  24.         /// <returns>A string.</returns>
  25.         public static string ParseBold(this string value)
  26.         {
  27.             return Regex.Replace(value, @"\*(.*?)\*", AsBoldTag);
  28.         }
  29.  
  30.         /// <summary>A string extension method that parses the hashtag attributes contained in value</summary>
  31.         /// <param name="value">The value.</param>
  32.         /// <returns>A string.</returns>
  33.         public static string ParseHashtag(this string value)
  34.         {
  35.             return Regex.Replace(value, @"#([A-Za-z])([A-Za-z0-9]*)", AsHashtag);
  36.         }
  37.  
  38.         /// <summary>A string extension method that parses the italic attributes contained in value</summary>
  39.         /// <param name="value">The value.</param>
  40.         /// <returns>A string.</returns>
  41.         public static string ParseItalic(this string value)
  42.         {
  43.             return Regex.Replace(value, @"_(.*?)_", AsItalicTag);
  44.         }
  45.  
  46.         /// <summary>A string extension method that parses the link attributes contained in value</summary>
  47.         /// <param name="value">The value.</param>
  48.         /// <returns>A string.</returns>
  49.         public static string ParseLink(this string value)
  50.         {
  51.             return Regex.Replace(value,
  52.                 @"(\[)([A-Za-z0-9 -_]*)(\])(\()((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;amp;:/~\+#]*[\w\-\@?^=%&amp;amp;/~\+#])?)(\))",
  53.                 AsUrlTag);
  54.         }
  55.  
  56.         /// <summary>Parses the post.</summary>
  57.         /// <param name="value">The value.</param>
  58.         /// <returns>A string.</returns>
  59.         public static string ParsePost(this string value)
  60.         {
  61.             return value.ParseItalic().ParseBold().ParseHashtag().ParseUsertag().ParseLink();
  62.         }
  63.  
  64.         /// <summary>Parses the usertag.</summary>
  65.         /// <param name="value">The value.</param>
  66.         /// <returns>A string.</returns>
  67.         public static string ParseUsertag(this string value)
  68.         {
  69.             return Regex.Replace(value, @"@([A-Za-z])([A-Za-z0-9]*)", AsUserTag);
  70.         }
  71.  
  72.         /// <summary>Expresses the match as a bold tag</summary>
  73.         /// <param name="m">The m.</param>
  74.         /// <returns>A string.</returns>
  75.         private static string AsBoldTag(Match m)
  76.         {
  77.             var value = m.Value;
  78.             var tag = value.Replace("*", string.Empty);
  79.             return string.IsNullOrWhiteSpace(tag) ? string.Empty : tag.FormatSpan("bold");
  80.         }
  81.  
  82.         /// <summary>Expresses the match as a hashtag</summary>
  83.         /// <param name="m">The m.</param>
  84.         /// <returns>A string.</returns>
  85.         private static string AsHashtag(Match m)
  86.         {
  87.             var value = m.Value;
  88.             var tag = value.Replace("#", string.Empty);
  89.             var url = VirtualPathUtility.ToAbsolute($"~/Hashtag/{tag}");
  90.             return string.IsNullOrWhiteSpace(tag) ? string.Empty : tag.FormatLink(url, "hashtag-link", "hashtag");
  91.         }
  92.  
  93.         /// <summary>Expresses the match as an italic tag</summary>
  94.         /// <param name="m">The m.</param>
  95.         /// <returns>A string.</returns>
  96.         private static string AsItalicTag(Match m)
  97.         {
  98.             var value = m.Value;
  99.             var tag = value.Replace("_", string.Empty);
  100.             return string.IsNullOrWhiteSpace(tag) ? string.Empty : tag.FormatSpan("italic");
  101.         }
  102.  
  103.         /// <summary>Expresses the match as a URL tag</summary>
  104.         /// <param name="m">The m.</param>
  105.         /// <returns>A string.</returns>
  106.         private static string AsUrlTag(Match m)
  107.         {
  108.             var desc = m.Groups[2].Value;
  109.             var url = m.Groups[5].Value;
  110.             return string.IsNullOrWhiteSpace(url) ? string.Empty : desc.FormatLink(url, string.Empty, "link");
  111.         }
  112.  
  113.         /// <summary>Expresses the match as a user tag</summary>
  114.         /// <param name="m">The m.</param>
  115.         /// <returns>A string.</returns>
  116.         private static string AsUserTag(Match m)
  117.         {
  118.             var value = m.Value;
  119.             var tag = value.Replace("@", string.Empty);
  120.             var url = VirtualPathUtility.ToAbsolute($"~/Profile/{tag}");
  121.             return string.IsNullOrWhiteSpace(tag) ? string.Empty : tag.FormatLink(url, "usertag-link", "at");
  122.         }
  123.  
  124.         /// <summary>A string extension method that formats the link.</summary>
  125.         /// <param name="value">The value.</param>
  126.         /// <param name="url">URL of the document.</param>
  127.         /// <param name="className">Name of the class.</param>
  128.         /// <param name="faIcon"></param>
  129.         /// <returns>The formatted link.</returns>
  130.         private static string FormatLink(this string value, string url, string className, string faIcon)
  131.         {
  132.             var cssClass = string.IsNullOrWhiteSpace(className)
  133.                 ? string.Empty
  134.                 : $" class=\"{className}\"";
  135.             return $"<a rel=\"canonical\" href=\"{url}\"{cssClass}><i class=\"fa fa-{faIcon} fa-1x\"></i><span>{value}</span></a>";
  136.         }
  137.  
  138.         /// <summary>A string extension method that format span.</summary>
  139.         /// <param name="value">The value.</param>
  140.         /// <param name="className">Name of the class.</param>
  141.         /// <returns>The formatted span.</returns>
  142.         private static string FormatSpan(this string value, string className)
  143.         {
  144.             var cssClass = string.IsNullOrWhiteSpace(className)
  145.                 ? string.Empty
  146.                 : $" class=\"{className}\"";
  147.             return $"<span{cssClass}>{value}</span>";
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement