C#正则实现Ubb解析类的代码
C#正则实现Ubb解析类的代码
作者: 字体:[增加 减小] 类型:转载 时间:2007-03-30
解析得到的代码能通过XHTML 1.0 STRICT验证;
包含了标题,链接,字体,对齐,图片,引用,列表等方面的功能.
包含了标题,链接,字体,对齐,图片,引用,列表等方面的功能.
代码如下:
1 //作者:deerchao 2 // http://www.unibetter.com/blogs/blogdeerchao/default.aspx 3 //在不移除以上(及本条)注释的前提下,任何人可以以任何方式使用此代码. 4 5 using System; 6 using System.Collections.Generic; 7 using System.Text; 8 using System.Web; 9 using System.Text.RegularExpressions; 10 11 namespace Deerchao.Web 12 { 13 public class UbbDecoder 14 { 15 private static readonly RegexOptions options = RegexOptions.Compiled | RegexOptions.Singleline; 16 17 /// <summary> 18 /// 解析Ubb代码为Html代码 19 /// </summary> 20 /// <param name="ubb">Ubb代码</param> 21 /// <returns>解析得到的Html代码</returns> 22 public static string Decode(string ubb) 23 { 24 if (string.IsNullOrEmpty(ubb)) 25 return null; 26 string result = ubb; 27 result = HttpUtility.HtmlEncode(result); 28 29 result = DecodeStyle(result); 30 result = DecodeFont(result); 31 result = DecodeColor(result); 32 result = DecodeImage(result); 33 result = DecodeLinks(result); 34 result = DecodeQuote(result); 35 result = DecodeAlign(result); 36 result = DecodeList(result); 37 result = DecodeHeading(result); 38 result = DecodeBlank(result); 39 40 return result; 41 } 42 43 /// <summary> 44 /// 解析Ubb代码为Html代码,所有的链接为rel="nofollow" 45 /// </summary> 46 /// <param name="ubb">Ubb代码</param> 47 /// <returns>解析得到的Html代码</returns> 48 public static string DecodeNoFollow(string ubb) 49 { 50 if (string.IsNullOrEmpty(ubb)) 51 return null; 52 string result = ubb; 53 result = HttpUtility.HtmlEncode(result); 54 55 result = DecodeStyle(result); 56 result = DecodeFont(result); 57 result = DecodeColor(result); 58 result = DecodeImage(result); 59 result = DecodeLinksNoFollow(result); 60 result = DecodeQuote(result); 61 result = DecodeAlign(result); 62 result = DecodeList(result); 63 result = DecodeHeading(result); 64 result = DecodeBlank(result); 65 66 return result; 67 } 68 69 private static string DecodeHeading(string ubb) 70 { 71 string result = ubb; 72 result = Regex.Replace(result, @"\[h(\d)\](.*?)\[/h\1\]", "<h$1>$2</h$1>", options); 73 return result; 74 } 75 76 private static string DecodeList(string ubb) 77 { 78 string sListFormat = "<ol style=\"list-style:{0};\">$1</ol>"; 79 string result = ubb; 80 // Lists 81 result = Regex.Replace(result, @"\[\*\]([^\[]*)", "<li>$1</li>", options); 82 result = Regex.Replace(result, @"\[list\]\s*(.*?)\[/list\]", "<ul>$1</ul>", options); 83 result = Regex.Replace(result, @"\[list=1\]\s*(.*?)\[/list\]", string.Format(sListFormat, "decimal"), options); 84 result = Regex.Replace(result, @"\[list=i\]\s*(.*?)\[/list\]", string.Format(sListFormat, "lower-roman"), options); 85 result = Regex.Replace(result, @"\[list=I\]\s*(.*?)\[/list\]", string.Format(sListFormat, "upper-roman"), options); 86 result = Regex.Replace(result, @"\[list=a\]\s*(.*?)\[/list\]", string.Format(sListFormat, "lower-alpha"), options); 87 result = Regex.Replace(result, @"\[list=A\]\s*(.*?)\[/list\]", string.Format(sListFormat, "upper-alpha"), options); 88 89 return result; 90 } 91 92 private static string DecodeBlank(string ubb) 93 { 94 string result = ubb; 95 96 result = Regex.Replace(result, @"(?<= ) | (?= )", " ", options); 97 result = Regex.Replace(result, @"\r\n", "<br />"); 98 string[] blockTags = {"h[1-6]", "li", "list", "div", "p", "ul"}; 99 //clear br before block tags(start or end) 100 foreach (string tag in blockTags) 101 { 102 Regex r = new Regex("<br />(<" + tag + ")",options); 103 result = r.Replace(result, "$1"); 104 r = new Regex("<br />(</" + tag + ")",options); 105 result = r.Replace(result, "$1"); 106 } 107 return result; 108 } 109 110 private static string DecodeAlign(string ubb) 111 { 112 string result = ubb; 113 114 result = Regex.Replace(result, @"\[left\](.*?)\[/left\]", "<div style=\"text-align:left\">$1</div>", options); 115 result = Regex.Replace(result, @"\[right\](.*?)\[/right\]", "<div style=\"text-align:right\">$1</div>", options); 116 result = Regex.Replace(result, @"\[center\](.*?)\[/center\]", "<div style=\"text-align:center\">$1</div>", options); 117 118 return result; 119 } 120 121 private static string DecodeQuote(string ubb) 122 { 123 string result = ubb; 124 125 result = Regex.Replace(result, @"\[quote\]", "<blockquote><div>", options); 126 result = Regex.Replace(result, @"\[/quote\]", "</div></blockquote>", options); 127 return result; 128 } 129 130 private static string DecodeFont(string ubb) 131 { 132 string result = ubb; 133 134 result = Regex.Replace(result, @"\[size=([-\w]+)\](.*?)\[/size\]", "<span style=\"font-size:$1\">$2</span>", options); 135 result = Regex.Replace(result, @"\[font=(.*?)\](.*?)\[/font\]", "<span style=\"font-family:$1\">$2</span>", options); 136 return result; 137 } 138 139 private static string DecodeLinks(string ubb) 140 { 141 string result = ubb; 142 143 result = Regex.Replace(result, @"\[url\]www\.(.*?)\[/url\]", "<a href=\"http://www.$1\">$1</a>", options); 144 result = Regex.Replace(result, @"\[url\](.*?)\[/url\]", "<a href=\"$1\">$1</a>", options); 145 result = Regex.Replace(result, @"\[url=(.*?)\](.*?)\[/url\]", "<a href=\"$1\" title=\"$2\">$2</a>", options); 146 result = Regex.Replace(result, @"\[email\](.*?)\[/email\]", "<a href=\"mailto:$1\">$1</a>", options); 147 return result; 148 } 149 150 private static string DecodeLinksNoFollow(string ubb) 151 { 152 string result = ubb; 153 154 result = Regex.Replace(result, @"\[url\]www\.(.*?)\[/url\]", "<a rel=\"nofollow\" href=\"http://www.$1\">$1</a>", options); 155 result = Regex.Replace(result, @"\[url\](.*?)\[/url\]", "<a rel=\"nofollow\" href=\"$1\">$1</a>", options); 156 result = Regex.Replace(result, @"\[url=(.*?)\](.*?)\[/url\]", "<a rel=\"nofollow\" href=\"$1\" title=\"$2\">$2</a>", options); 157 result = Regex.Replace(result, @"\[email\](.*?)\[/email\]", "<a href=\"mailto:$1\">$1</a>", options); 158 return result; 159 } 160 161 private static string DecodeImage(string ubb) 162 { 163 string result = ubb; 164 165 result = Regex.Replace(result, @"\[hr\]", "<hr />", options); 166 result = Regex.Replace(result, @"\[img\](.+?)\[/img\]", "<img src=\"$1\" alt=\"\" />", options); 167 result = Regex.Replace(result, @"\[img=(\d+)x(\d+)\](.+?)\[/img\]", "<img src=\"$3\" style=\"width:$1px;height:$2px\" alt=\"\" />", options); 168 169 return result; 170 } 171 172 private static string DecodeColor(string ubb) 173 { 174 string result = ubb; 175 result = Regex.Replace(result, @"\[color=(#?\w+?)\](.+?)\[/color\]", "<span style=\"color:$1\">$2</span>",options); 176 177 return result; 178 } 179 180 private static string DecodeStyle(string ubb) 181 { 182 string result=ubb; 183 //we don't need this for perfomance and other consideration: 184 //(<table[^>]*>(?><table[^>]*>(?<Depth>)|</table>(?<-Depth>)|.)+(?(Depth)(?!))</table>) 185 result = Regex.Replace(result, @"\[[b]\](.*?)\[/[b]\]", "<strong>$1</strong>", options); 186 result = Regex.Replace(result, @"\[[u]\](.*?)\[/[u]\]", "<span style=\"text-decoration:underline\">$1</span>", options); 187 result = Regex.Replace(result, @"\[[i]\](.*?)\[/[i]\]", "<i>$1</i>", options); 188 189 return result; 190 } 191 } 192 }
浙公网安备 33010602011771号