包含了标题,链接,字体,对齐,图片,引用,列表等方面的功能.
Ubb.ReadMe.htm
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
复制代码 代码如下:
//作者:deerchao
//http://www.unibetter.com/blogs/blogdeerchao/default.aspx
//在不移除以上(及本条)注释的前提下,任何人可以以任何方式使用此代码.
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Web;
usingSystem.Text.RegularExpressions;
namespaceDeerchao.Web
{
publicclassUbbDecoder
{
privatestaticreadonlyRegexOptionsoptions=RegexOptions.Compiled|RegexOptions.Singleline;
///
///解析Ubb代码为Html代码
///
///
///
publicstaticstringDecode(stringubb)
{
if(string.IsNullOrEmpty(ubb))
returnnull;
stringresult=ubb;
result=HttpUtility.HtmlEncode(result);
result=DecodeStyle(result);
result=DecodeFont(result);
result=DecodeColor(result);
result=DecodeImage(result);
result=DecodeLinks(result);
result=DecodeQuote(result);
result=DecodeAlign(result);
result=DecodeList(result);
result=DecodeHeading(result);
result=DecodeBlank(result);
returnresult;
}
///
///解析Ubb代码为Html代码,所有的链接为rel="nofollow"
///
///
///
publicstaticstringDecodeNoFollow(stringubb)
{
if(string.IsNullOrEmpty(ubb))
returnnull;
stringresult=ubb;
result=HttpUtility.HtmlEncode(result);
result=DecodeStyle(result);
result=DecodeFont(result);
result=DecodeColor(result);
result=DecodeImage(result);
result=DecodeLinksNoFollow(result);
result=DecodeQuote(result);
result=DecodeAlign(result);
result=DecodeList(result);
result=DecodeHeading(result);
result=DecodeBlank(result);
returnresult;
}
privatestaticstringDecodeHeading(stringubb)
{
stringresult=ubb;
result=Regex.Replace(result,@"[h(d)](.*?)[/h1]","
returnresult;
}
privatestaticstringDecodeList(stringubb)
{
stringsListFormat="
stringresult=ubb;
//Lists
result=Regex.Replace(result,@"[*]([^[]*)","
result=Regex.Replace(result,@"[list]s*(.*?)[/list]","$1
",options);
result=Regex.Replace(result,@"[list=1]s*(.*?)[/list]",string.Format(sListFormat,"decimal"),options);
result=Regex.Replace(result,@"[list=i]s*(.*?)[/list]",string.Format(sListFormat,"lower-roman"),options);
result=Regex.Replace(result,@"[list=I]s*(.*?)[/list]",string.Format(sListFormat,"upper-roman"),options);
result=Regex.Replace(result,@"[list=a]s*(.*?)[/list]",string.Format(sListFormat,"lower-alpha"),options);
result=Regex.Replace(result,@"[list=A]s*(.*?)[/list]",string.Format(sListFormat,"upper-alpha"),options);
returnresult;
}
privatestaticstringDecodeBlank(stringubb)
{
stringresult=ubb;
result=Regex.Replace(result,@"(?<=)|(?=)","",options);
result=Regex.Replace(result,@"rn","
");
string[]blockTags={"h[1-6]","li","list","div","p","ul"};
//clearbrbeforeblocktags(startorend)
foreach(stringtaginblockTags)
{
Regexr=newRegex("
(<"+tag+")",options);
result=r.Replace(result,"$1");
r=newRegex("
("+tag+")",options);
result=r.Replace(result,"$1");
}
returnresult;
}
privatestaticstringDecodeAlign(stringubb)
{
stringresult=ubb;
result=Regex.Replace(result,@"[left](.*?)[/left]","