欢迎来到福编程网,本站提供各种互联网专业知识!

C#正则实现Ubb解析类的代码

发布时间:2007-03-30 作者: 来源:转载
解析得到的代码能通过XHTML1.0STRICT验证;包含了标题,链接,字体,对齐,图片,引用,列表等方面的功能.Ubb.ReadMe.htmUBB代码说明标题[h1]标题一[/h1]标题一[h2]标题二[/h2]标题二[h1]标题三[/h1]标题三[h4]标题四[/h4]标题四[h5]标题五[/h5]标题五[h6]标题六[/h6
解析得到的代码能通过XHTML1.0STRICT验证;
包含了标题,链接,字体,对齐,图片,引用,列表等方面的功能.
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代码
///

///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"
///

///Ubb代码
///解析得到的Html代码
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]","$2",options);
returnresult;
}

privatestaticstringDecodeList(stringubb)
{
stringsListFormat="$1";
stringresult=ubb;
//Lists
result=Regex.Replace(result,@"[*]([^[]*)","

  • $1
  • ",options);
    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("

    (result=r.Replace(result,"$1");
    }
    returnresult;
    }

    privatestaticstringDecodeAlign(stringubb)
    {
    stringresult=ubb;

    result=Regex.Replace(result,@"[left](.*?)[/left]","$1

    ",options);
    result=Regex.Replace(result,@"[right](.*?)[/right]","$1
    ",options);
    result=Regex.Replace(result,@"[center](.*?)[/center]","$1
    ",options);

    returnresult;
    }

    privatestaticstringDecodeQuote(stringubb)
    {
    stringresult=ubb;

    result=Regex.Replace(result,@"[quote]","

    ",options);
    result=Regex.Replace(result,@"[/quote]","
    ",options);
    returnresult;
    }

    privatestaticstringDecodeFont(stringubb)
    {
    stringresult=ubb;

    result=Regex.Replace(result,@"[size=([-w]+)](.*?)[/size]","$2",options);
    result=Regex.Replace(result,@"[font=(.*?)](.*?)[/font]","$2",options);
    returnresult;
    }

    privatestaticstringDecodeLinks(stringubb)
    {
    stringresult=ubb;

    result=Regex.Replace(result,@"[url]www.(.*?)[/url]","$1",options);
    result=Regex.Replace(result,@"[url](.*?)[/url]","$1",options);
    result=Regex.Replace(result,@"[url=(.*?)](.*?)[/url]","$2",options);
    result=Regex.Replace(result,@"[email](.*?)[/email]","$1",options);
    returnresult;
    }

    privatestaticstringDecodeLinksNoFollow(stringubb)
    {
    stringresult=ubb;

    result=Regex.Replace(result,@"[url]www.(.*?)[/url]","$1",options);
    result=Regex.Replace(result,@"[url](.*?)[/url]","$1",options);
    result=Regex.Replace(result,@"[url=(.*?)](.*?)[/url]","$2",options);
    result=Regex.Replace(result,@"[email](.*?)[/email]","$1",options);
    returnresult;
    }

    privatestaticstringDecodeImage(stringubb)
    {
    stringresult=ubb;

    result=Regex.Replace(result,@"[hr]","


    ",options);
    result=Regex.Replace(result,@"[img](.+?)[/img]","",options);
    result=Regex.Replace(result,@"[img=(d+)x(d+)](.+?)[/img]","",options);

    returnresult;
    }

    privatestaticstringDecodeColor(stringubb)
    {
    stringresult=ubb;
    result=Regex.Replace(result,@"[color=(#?w+?)](.+?)[/color]","$2",options);

    returnresult;
    }

    privatestaticstringDecodeStyle(stringubb)
    {
    stringresult=ubb;
    //wedon'tneedthisforperfomanceandotherconsideration:
    //(]*>(?>]*>(?)|(?<-Depth>)|.)+(?(Depth)(?!)))
    result=Regex.Replace(result,@"[[b]](.*?)[/[b]]","$1",options);
    result=Regex.Replace(result,@"[[u]](.*?)[/[u]]","$1",options);
    result=Regex.Replace(result,@"[[i]](.*?)[/[i]]","$1",options);

    returnresult;
    }
    }
    }

    相关推荐