无为清净楼资源网 Design By www.qnjia.com
解析得到的代码能通过XHTML 1.0 STRICT验证;
包含了标题,链接,字体,对齐,图片,引用,列表等方面的功能.
Ubb.ReadMe.htm
[Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]
复制代码 代码如下:
//作者:deerchao
// http://www.unibetter.com/blogs/blogdeerchao/default.aspx
//在不移除以上(及本条)注释的前提下,任何人可以以任何方式使用此代码.
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
namespace Deerchao.Web
{
public class UbbDecoder
{
private static readonly RegexOptions options = RegexOptions.Compiled | RegexOptions.Singleline;
/// <summary>
/// 解析Ubb代码为Html代码
/// </summary>
/// <param name="ubb">Ubb代码</param>
/// <returns>解析得到的Html代码</returns>
public static string Decode(string ubb)
{
if (string.IsNullOrEmpty(ubb))
return null;
string result = 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);
return result;
}
/// <summary>
/// 解析Ubb代码为Html代码,所有的链接为rel="nofollow"
/// </summary>
/// <param name="ubb">Ubb代码</param>
/// <returns>解析得到的Html代码</returns>
public static string DecodeNoFollow(string ubb)
{
if (string.IsNullOrEmpty(ubb))
return null;
string result = 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);
return result;
}
private static string DecodeHeading(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[h(\d)\](.*?)\[/h\1\]", "<h$1>$2</h$1>", options);
return result;
}
private static string DecodeList(string ubb)
{
string sListFormat = "<ol style=\"list-style:{0};\">$1</ol>";
string result = ubb;
// Lists
result = Regex.Replace(result, @"\[\*\]([^\[]*)", "<li>$1</li>", options);
result = Regex.Replace(result, @"\[list\]\s*(.*?)\[/list\]", "<ul>$1</ul>", 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);
return result;
}
private static string DecodeBlank(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"(?<= ) | (?= )", " ", options);
result = Regex.Replace(result, @"\r\n", "<br />");
string[] blockTags = {"h[1-6]", "li", "list", "div", "p", "ul"};
//clear br before block tags(start or end)
foreach (string tag in blockTags)
{
Regex r = new Regex("<br />(<" + tag + ")",options);
result = r.Replace(result, "$1");
r = new Regex("<br />(</" + tag + ")",options);
result = r.Replace(result, "$1");
}
return result;
}
private static string DecodeAlign(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[left\](.*?)\[/left\]", "<div style=\"text-align:left\">$1</div>", options);
result = Regex.Replace(result, @"\[right\](.*?)\[/right\]", "<div style=\"text-align:right\">$1</div>", options);
result = Regex.Replace(result, @"\[center\](.*?)\[/center\]", "<div style=\"text-align:center\">$1</div>", options);
return result;
}
private static string DecodeQuote(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[quote\]", "<blockquote><div>", options);
result = Regex.Replace(result, @"\[/quote\]", "</div></blockquote>", options);
return result;
}
private static string DecodeFont(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[size=([-\w]+)\](.*?)\[/size\]", "<span style=\"font-size:$1\">$2</span>", options);
result = Regex.Replace(result, @"\[font=(.*?)\](.*?)\[/font\]", "<span style=\"font-family:$1\">$2</span>", options);
return result;
}
private static string DecodeLinks(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[url\]www\.(.*?)\[/url\]", "<a href=\"http://www.$1\">$1</a>", options);
result = Regex.Replace(result, @"\[url\](.*?)\[/url\]", "<a href=\"$1\">$1</a>", options);
result = Regex.Replace(result, @"\[url=(.*?)\](.*?)\[/url\]", "<a href=\"$1\" title=\"$2\">$2</a>", options);
result = Regex.Replace(result, @"\[email\](.*?)\[/email\]", "<a href=\"mailto:$1\">$1</a>", options);
return result;
}
private static string DecodeLinksNoFollow(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[url\]www\.(.*?)\[/url\]", "<a rel=\"nofollow\" href=\"http://www.$1\">$1</a>", options);
result = Regex.Replace(result, @"\[url\](.*?)\[/url\]", "<a rel=\"nofollow\" href=\"$1\">$1</a>", options);
result = Regex.Replace(result, @"\[url=(.*?)\](.*?)\[/url\]", "<a rel=\"nofollow\" href=\"$1\" title=\"$2\">$2</a>", options);
result = Regex.Replace(result, @"\[email\](.*?)\[/email\]", "<a href=\"mailto:$1\">$1</a>", options);
return result;
}
private static string DecodeImage(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[hr\]", "<hr />", options);
result = Regex.Replace(result, @"\[img\](.+?)\[/img\]", "<img src=\"$1\" alt=\"\" />", options);
result = Regex.Replace(result, @"\[img=(\d+)x(\d+)\](.+?)\[/img\]", "<img src=\"$3\" style=\"width:$1px;height:$2px\" alt=\"\" />", options);
return result;
}
private static string DecodeColor(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[color=(#?\w+?)\](.+?)\[/color\]", "<span style=\"color:$1\">$2</span>",options);
return result;
}
private static string DecodeStyle(string ubb)
{
string result=ubb;
//we don't need this for perfomance and other consideration:
//(<table[^>]*>(?><table[^>]*>(?<Depth>)|</table>(?<-Depth>)|.)+(?(Depth)(?!))</table>)
result = Regex.Replace(result, @"\[[b]\](.*?)\[/[b]\]", "<strong>$1</strong>", options);
result = Regex.Replace(result, @"\[[u]\](.*?)\[/[u]\]", "<span style=\"text-decoration:underline\">$1</span>", options);
result = Regex.Replace(result, @"\[[i]\](.*?)\[/[i]\]", "<i>$1</i>", options);
return result;
}
}
}
包含了标题,链接,字体,对齐,图片,引用,列表等方面的功能.
Ubb.ReadMe.htm
[Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]
复制代码 代码如下:
//作者:deerchao
// http://www.unibetter.com/blogs/blogdeerchao/default.aspx
//在不移除以上(及本条)注释的前提下,任何人可以以任何方式使用此代码.
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
namespace Deerchao.Web
{
public class UbbDecoder
{
private static readonly RegexOptions options = RegexOptions.Compiled | RegexOptions.Singleline;
/// <summary>
/// 解析Ubb代码为Html代码
/// </summary>
/// <param name="ubb">Ubb代码</param>
/// <returns>解析得到的Html代码</returns>
public static string Decode(string ubb)
{
if (string.IsNullOrEmpty(ubb))
return null;
string result = 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);
return result;
}
/// <summary>
/// 解析Ubb代码为Html代码,所有的链接为rel="nofollow"
/// </summary>
/// <param name="ubb">Ubb代码</param>
/// <returns>解析得到的Html代码</returns>
public static string DecodeNoFollow(string ubb)
{
if (string.IsNullOrEmpty(ubb))
return null;
string result = 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);
return result;
}
private static string DecodeHeading(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[h(\d)\](.*?)\[/h\1\]", "<h$1>$2</h$1>", options);
return result;
}
private static string DecodeList(string ubb)
{
string sListFormat = "<ol style=\"list-style:{0};\">$1</ol>";
string result = ubb;
// Lists
result = Regex.Replace(result, @"\[\*\]([^\[]*)", "<li>$1</li>", options);
result = Regex.Replace(result, @"\[list\]\s*(.*?)\[/list\]", "<ul>$1</ul>", 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);
return result;
}
private static string DecodeBlank(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"(?<= ) | (?= )", " ", options);
result = Regex.Replace(result, @"\r\n", "<br />");
string[] blockTags = {"h[1-6]", "li", "list", "div", "p", "ul"};
//clear br before block tags(start or end)
foreach (string tag in blockTags)
{
Regex r = new Regex("<br />(<" + tag + ")",options);
result = r.Replace(result, "$1");
r = new Regex("<br />(</" + tag + ")",options);
result = r.Replace(result, "$1");
}
return result;
}
private static string DecodeAlign(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[left\](.*?)\[/left\]", "<div style=\"text-align:left\">$1</div>", options);
result = Regex.Replace(result, @"\[right\](.*?)\[/right\]", "<div style=\"text-align:right\">$1</div>", options);
result = Regex.Replace(result, @"\[center\](.*?)\[/center\]", "<div style=\"text-align:center\">$1</div>", options);
return result;
}
private static string DecodeQuote(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[quote\]", "<blockquote><div>", options);
result = Regex.Replace(result, @"\[/quote\]", "</div></blockquote>", options);
return result;
}
private static string DecodeFont(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[size=([-\w]+)\](.*?)\[/size\]", "<span style=\"font-size:$1\">$2</span>", options);
result = Regex.Replace(result, @"\[font=(.*?)\](.*?)\[/font\]", "<span style=\"font-family:$1\">$2</span>", options);
return result;
}
private static string DecodeLinks(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[url\]www\.(.*?)\[/url\]", "<a href=\"http://www.$1\">$1</a>", options);
result = Regex.Replace(result, @"\[url\](.*?)\[/url\]", "<a href=\"$1\">$1</a>", options);
result = Regex.Replace(result, @"\[url=(.*?)\](.*?)\[/url\]", "<a href=\"$1\" title=\"$2\">$2</a>", options);
result = Regex.Replace(result, @"\[email\](.*?)\[/email\]", "<a href=\"mailto:$1\">$1</a>", options);
return result;
}
private static string DecodeLinksNoFollow(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[url\]www\.(.*?)\[/url\]", "<a rel=\"nofollow\" href=\"http://www.$1\">$1</a>", options);
result = Regex.Replace(result, @"\[url\](.*?)\[/url\]", "<a rel=\"nofollow\" href=\"$1\">$1</a>", options);
result = Regex.Replace(result, @"\[url=(.*?)\](.*?)\[/url\]", "<a rel=\"nofollow\" href=\"$1\" title=\"$2\">$2</a>", options);
result = Regex.Replace(result, @"\[email\](.*?)\[/email\]", "<a href=\"mailto:$1\">$1</a>", options);
return result;
}
private static string DecodeImage(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[hr\]", "<hr />", options);
result = Regex.Replace(result, @"\[img\](.+?)\[/img\]", "<img src=\"$1\" alt=\"\" />", options);
result = Regex.Replace(result, @"\[img=(\d+)x(\d+)\](.+?)\[/img\]", "<img src=\"$3\" style=\"width:$1px;height:$2px\" alt=\"\" />", options);
return result;
}
private static string DecodeColor(string ubb)
{
string result = ubb;
result = Regex.Replace(result, @"\[color=(#?\w+?)\](.+?)\[/color\]", "<span style=\"color:$1\">$2</span>",options);
return result;
}
private static string DecodeStyle(string ubb)
{
string result=ubb;
//we don't need this for perfomance and other consideration:
//(<table[^>]*>(?><table[^>]*>(?<Depth>)|</table>(?<-Depth>)|.)+(?(Depth)(?!))</table>)
result = Regex.Replace(result, @"\[[b]\](.*?)\[/[b]\]", "<strong>$1</strong>", options);
result = Regex.Replace(result, @"\[[u]\](.*?)\[/[u]\]", "<span style=\"text-decoration:underline\">$1</span>", options);
result = Regex.Replace(result, @"\[[i]\](.*?)\[/[i]\]", "<i>$1</i>", options);
return result;
}
}
}
标签:
C#正则实现Ubb解析类的代码
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年11月11日
2024年11月11日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]