ASP.NET Forums 2.0 本地化修改(四)
今天主要讲几个小问题,给编辑器增加新的按钮,这里用的比较简单的方法,大家可以参考增加自己需要的工具栏:
增加FTB工具栏按钮主要是继承FreeTextBoxControls.ToolbarButton;
这次要增加5个按钮,分别为增加WMP音频,WMP视频,REALONE音频,REALONE视频以及FLASH按钮;
1、首先增加资源文件,打开languages/zh-CN/Resources.xml,增加如下资源:
<resource name = "CreateEditPost_FTB_RPAButton">插入RealPlayer音频文件</resource>
<resource name = "CreateEditPost_FTB_RPVButton">插入RealPlayer视频文件</resource>
<resource name = "CreateEditPost_FTB_WMAButton">插入Windows Media Player音频文件</resource>
<resource name = "CreateEditPost_FTB_WMVButton">插入Windows Media Player视频文件</resource>
2、准备解析对应内容的正则表达式,看下面代码,直接有注释(将该代码插入到Components/Components/Transforms.cs的BBcodeToHtml方法的最下面)
表达式:[rpa play=true]rpaUrl[/rpa]
说明: 支持realplayer的音频,
参数: play参数为true或false,是否自动开始播放
rpaUrl为音频文件地址
*/
encodedString = Regex.Replace(encodedString,
@"\[rpa[ ]play=((true|false))\](\S*)\[/rpa\]",
"<OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=500>"
+ "<PARAM NAME=SRC VALUE=\"$3\">"
+ "<PARAM NAME=AUTOSTART VALUE=$2>"
+ "<PARAM NAME=CONTROLS VALUE=controlpanel>"
+ "<PARAM NAME=CONSOLE VALUE=Clip1></OBJECT>"
+ "<BR><a href=\"$3\" target=_blank>$3</a>",
RegexOptions.IgnoreCase);
/*
表达式:[rpv play=true,width=100,height=200]rpvUrl[/rpv]
说明: 支持realplayer的视频,
参数: play参数为true或false,是否自动开始播放
width:视频文件宽度
height:食品文件高度
rpvUrl为视频文件地址
*/
encodedString = Regex.Replace(encodedString,
@"\[rpv[ ]play=((true|false)),width=(\d*),height=(\d*)\](\S*)\[/rpv\]",
"<OBJECT classid=clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA class=OBJECT id=RAOCX width=$3 height=$4>"
+ "<PARAM NAME=SRC VALUE=\"$5\">"
+ "<PARAM NAME=AUTOSTART VALUE=$2>"
+ "<PARAM NAME=CONSOLE VALUE=Clip1>"
+ "<PARAM NAME=CONTROLS VALUE=imagewindow>"
+ "</OBJECT><br>"
+ "<OBJECT classid=CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA height=32 id=video2 width=$3>"
+ "<PARAM NAME=SRC VALUE=\"$5\">"
+ "<PARAM NAME=AUTOSTART VALUE=$2>"
+ "<PARAM NAME=CONTROLS VALUE=controlpanel>"
+ "<PARAM NAME=CONSOLE VALUE=Clip1></OBJECT>"
+ "<BR><a href=\"$5\" target=_blank>$5</a>",
RegexOptions.IgnoreCase);
/*
表达式:[wma play=true]wmaUrl[/wma]
说明: 支持windows media player的音频,
参数: play参数为true或false,是否自动开始播放
wmaUrl为音频文件地址
*/
encodedString = Regex.Replace(encodedString,
@"\[wma[ ]play=((true|false))\](\S*)\[/wma\]",
"<object align=middle classid=CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6 class=OBJECT id=MediaPlayer width=500 height=63>"
+ "<param name=ShowStatusBar value=true>"
+ "<param name=autoStart value=$2>"
+ "<param name=url value=\"$3\">"
+ "<embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=\"$3\" width=500 height=50></embed></object>"
+ "<BR><a href=\"$3\" target=_blank>$3</a>",
RegexOptions.IgnoreCase);
/*
表达式:[wmv play=true,width=100,height=200]wmvUrl[/wmv]
说明: 支持windows media player的视频
参数: play参数为true或false,是否自动开始播放
width:视频文件宽度
height:食品文件高度
wmvUrl为视频文件地址
*/
encodedString = Regex.Replace(encodedString,
@"\[wmv[ ]play=((true|false)),width=(\d*),height=(\d*)\](\S*)\[/wmv\]",
"<object align=middle classid=CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6 class=OBJECT id=MediaPlayer width=$3 height=$4>"
+ "<param name=autoStart value=$2>"
+ "<param name=ShowStatusBar value=true>"
+ "<param name=url value=$5>"
+ "<PARAM name=uiMode value=full>"
+ "<PARAM name=stretchToFit value=true>"
+ "<embed type=application/x-oleobject codebase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701 flename=mp src=\"$5\" width=$3 height=$4></embed>"
+ "</object>"
+ "<BR><a href=\"$5\" target=_blank>$5</a>",
RegexOptions.IgnoreCase);
/*
表达式:[flash width=500,height=400]url[/flash]
说明: 支持flash
参数: width为flash宽度
height为flash高度
url为flash的地址
*/
encodedString = Regex.Replace(encodedString,
@"\[flash[ ]width=(\d*),height=(\d*)\](\S*)\[/flash\]",
"<OBJECT codeBase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\" "
+ " classid=\"clsid:d27CDB6E-AE6D-11cf-96B8-444553540000\" "
+ "width=$1 height=$2>"
+ "<PARAM NAME=movie VALUE=\"$3\">"
+ "<PARAM NAME=quality VALUE=high>"
+ "<embed src=\"$3\" width=$1 height=$2 quality=high name=index type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\"/>"
+ "</OBJECT>"
+ "<BR><a href=\"$3\" target=_blank>$3</a>",
RegexOptions.IgnoreCase);
3、为了防止程序将UBB代码作为表情符替换,修改FormatPost方法,具体看代码说明即可,主要是调换一下顺序:
{
string formattedPost = rawPostBody;
// Perform HTML Encoding of any tag elements, if not PostType.HTML
//
if (postType != PostType.HTML)
{
formattedPost = HttpUtility.HtmlEncode(formattedPost);
if (postType != PostType.Poll)
formattedPost = formattedPost.Replace("\n", Globals.HtmlNewLine);
}
// Remove any script code
//
formattedPost = Transforms.StripScriptTags(formattedPost);
// Fix to reverse certain items that were HtmlEncoded above
//
formattedPost = UnHtmlEncode(formattedPost);
// Peform specialized transforms first.
//
if (allowCustomTransforms)
{
formattedPost = EmoticonTransforms(formattedPost);
formattedPost = PerformSpecializedTransforms(formattedPost);
}
//edited by jacky
//2004-10-2
//将其从allowCustomTransforms上移动到下,防止转换ubb生成后的代码
// Do BBCode transform, if any
//
formattedPost = BBcodeToHtml(formattedPost);
if (postType == PostType.Poll)
{
ArrayList voteOptions = BodyToVoteOptions(formattedPost);
formattedPost = CreateVoteXml(voteOptions).OuterXml;
}
return formattedPost;
}
4、基础工作已经做完,下面增加FTB按钮:
1)FlashButton, 在Controls/FreeTextBox下增加一文件FlashButton.cs,代码如下:
using AspNetForums.Components;
using FreeTextBoxControls;
namespace AspNetForums.Controls
{
/// <summary>
/// FlashButton 的摘要说明。
/// </summary>
public class FlashButton : ToolbarButton
{
public FlashButton():base(ResourceManager.GetString("CreateEditPost_FTB_FlashButton"),"FTB_FlashUBB","button_flash")
{
ScriptBlock = @"function FTB_FlashUBB(ftbName) {
FTB_SurroundText(ftbName,'[flash width=500,height=400]Flash文件地址[/flash]','');
}";
}
}
}
2)RPAButton,插入RealPlayer音频文件,在Controls/FreeTextBox下增加一文件RPAButton.cs,代码如下:
using AspNetForums.Components;
using FreeTextBoxControls;
namespace AspNetForums.Controls
{
/// <summary>
/// RPAButton 的摘要说明。
/// </summary>
public class RPAButton : ToolbarButton
{
public RPAButton():base(ResourceManager.GetString("CreateEditPost_FTB_RPAButton"),"FTB_RPA","button_rpa")
{
ScriptBlock = @"function FTB_RPA(ftbName) {
FTB_SurroundText(ftbName,'[rpa play=true]RealPlayerAudio文件地址[/rpa]','');
}";
}
}
}
3)RPVButton,在同上文件夹中,增加RPVButton.cs文件,代码如下:
using FreeTextBoxControls;
namespace AspNetForums.Controls
{
/// <summary>
/// RPVButton 的摘要说明。
/// </summary>
public class RPVButton : ToolbarButton
{
public RPVButton() : base(ResourceManager.GetString("CreateEditPost_FTB_RPVButton"), "FTB_RPV", "button_rpv")
{
ScriptBlock = @"function FTB_RPV(ftbName) {
FTB_SurroundText(ftbName,'[rpv play=true,width=500,height=350]RealPlayerVedio文件地址[/rpv]','');
}";
}
}
}
4)WMAButton,用于插入Windows Media 音频文件,增加WMAButton.cs文件,代码如下:
using FreeTextBoxControls;
namespace AspNetForums.Controls
{
/// <summary>
/// WMAButton 的摘要说明。
/// </summary>
public class WMAButton : ToolbarButton
{
public WMAButton() : base(ResourceManager.GetString("CreateEditPost_FTB_WMAButton"), "FTB_WMA", "button_wma")
{
ScriptBlock = @"function FTB_WMA(ftbName) {
FTB_SurroundText(ftbName,'[wma play=true]WindowsMediaAudio文件地址[/wma]','');
}";
}
}
}
5)WMVButton,在同上目录下增加WMVButton.cs,代码如下:
using AspNetForums.Components;
using FreeTextBoxControls;
namespace AspNetForums.Controls
{
/// <summary>
/// WMVButton 的摘要说明。
/// </summary>
public class WMVButton : ToolbarButton
{
public WMVButton() : base(ResourceManager.GetString("CreateEditPost_FTB_WMVButton"), "FTB_WMV", "button_wmv")
{
ScriptBlock = @"function FTB_WMV(ftbName) {
FTB_SurroundText(ftbName,'[wmv play=true,width=500,height=350]WindowsMediaVideo文件地址[/wmv]','');
}";
}
}
}
5、最后一步,将这些按钮添加到页面上,打开themes/default/skins/View-CreateEditPost.ascx,找到FTB的代码,在<Toolbars>标签中,插入以下代码:
<Forums:FlashButton runat="server"/>
<Forums:RPAButton runat="server"/>
<Forums:RPVButton runat="server"/>
<Forums:WMAButton runat="server"/>
<Forums:WMVButton runat="server"/>
</FTB:Toolbar>
OK,一切都已经完成,当然这里采用的是插入的比较简单的方法,也可以修改为采用向导方式由用户输入文件地址、视频文件大小、是否自动播放等。