Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. public static class StringMethodExtensions
  2. {
  3. private static string _paraBreak = "rnrn";
  4. private static string _link = "<a href="{0}">{1}</a>";
  5. private static string _linkNoFollow = "<a href="{0}" rel="nofollow">{1}</a>";
  6.  
  7. /// <summary>
  8. /// Returns a copy of this string converted to HTML markup.
  9. /// </summary>
  10. public static string ToHtml(this string s)
  11. {
  12. return ToHtml(s, false);
  13. }
  14.  
  15. /// <summary>
  16. /// Returns a copy of this string converted to HTML markup.
  17. /// </summary>
  18. /// <param name="nofollow">If true, links are given "nofollow"
  19. /// attribute</param>
  20. public static string ToHtml(this string s, bool nofollow)
  21. {
  22. StringBuilder sb = new StringBuilder();
  23.  
  24. int pos = 0;
  25. while (pos < s.Length)
  26. {
  27. // Extract next paragraph
  28. int start = pos;
  29. pos = s.IndexOf(_paraBreak, start);
  30. if (pos < 0)
  31. pos = s.Length;
  32. string para = s.Substring(start, pos - start).Trim();
  33.  
  34. // Encode non-empty paragraph
  35. if (para.Length > 0)
  36. EncodeParagraph(para, sb, nofollow);
  37.  
  38. // Skip over paragraph break
  39. pos += _paraBreak.Length;
  40. }
  41. // Return result
  42. return sb.ToString();
  43. }
  44.  
  45. /// <summary>
  46. /// Encodes a single paragraph to HTML.
  47. /// </summary>
  48. /// <param name="s">Text to encode</param>
  49. /// <param name="sb">StringBuilder to write results</param>
  50. /// <param name="nofollow">If true, links are given "nofollow"
  51. /// attribute</param>
  52. private static void EncodeParagraph(string s, StringBuilder sb, bool nofollow)
  53. {
  54. // Start new paragraph
  55. sb.AppendLine("<p>");
  56.  
  57. // HTML encode text
  58. s = HttpUtility.HtmlEncode(s);
  59.  
  60. // Convert single newlines to <br>
  61. s = s.Replace(Environment.NewLine, "<br />rn");
  62.  
  63. // Encode any hyperlinks
  64. EncodeLinks(s, sb, nofollow);
  65.  
  66. // Close paragraph
  67. sb.AppendLine("rn</p>");
  68. }
  69.  
  70. /// <summary>
  71. /// Encodes [[URL]] and [[Text][URL]] links to HTML.
  72. /// </summary>
  73. /// <param name="text">Text to encode</param>
  74. /// <param name="sb">StringBuilder to write results</param>
  75. /// <param name="nofollow">If true, links are given "nofollow"
  76. /// attribute</param>
  77. private static void EncodeLinks(string s, StringBuilder sb, bool nofollow)
  78. {
  79. // Parse and encode any hyperlinks
  80. int pos = 0;
  81. while (pos < s.Length)
  82. {
  83. // Look for next link
  84. int start = pos;
  85. pos = s.IndexOf("[[", pos);
  86. if (pos < 0)
  87. pos = s.Length;
  88. // Copy text before link
  89. sb.Append(s.Substring(start, pos - start));
  90. if (pos < s.Length)
  91. {
  92. string label, link;
  93.  
  94. start = pos + 2;
  95. pos = s.IndexOf("]]", start);
  96. if (pos < 0)
  97. pos = s.Length;
  98. label = s.Substring(start, pos - start);
  99. int i = label.IndexOf("][");
  100. if (i >= 0)
  101. {
  102. link = label.Substring(i + 2);
  103. label = label.Substring(0, i);
  104. }
  105. else
  106. {
  107. link = label;
  108. }
  109. // Append link
  110. sb.Append(String.Format(nofollow ? _linkNoFollow : _link, link, label));
  111.  
  112. // Skip over closing "]]"
  113. pos += 2;
  114. }
  115. }
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement