上篇文章介绍了这个程序的一些功能,经过测试,修改部分BUG,现贴出来供大家参考.
//程序名称: XYCODE解释程序 //程序运行: IE5.0或SERVER //程序设计: 小雨 //作者信箱: [email protected] //完成时间: 2005年1月 //调用方式: <script src="xycode.js" language=javascript></script> // 或: <script src="xycode.js" language=javascript runat=server></script> //执行方式: XYCODE(XYCODE文本)
//声明:本程序仅提供网页爱好者参考,如果用于商业运作,请你来信联系。
function XYCODE(context){ context=context.replace(/&/g,"&"); context=context.replace(/</g,"<"); context=context.replace(/>/g,">"); context=XYconv(context,"img",0); context=XYconv(context,"hr",0); context=XYconv(context,"flash",0); context=XYconv(context,"vod",0); context=XYconv(context,"music",0); context=XYconv(context,"code",1); context=XYconv(context,"i",1); context=XYconv(context,"b",1); context=XYconv(context,"u",1); context=XYconv(context,"left",1); context=XYconv(context,"center",1); context=XYconv(context,"right",1); context=XYconv(context,"list",1); context=XYconv(context,"color",1); context=XYconv(context,"bg",1); context=XYconv(context,"size",1); context=XYconv(context,"face",1); context=XYconv(context,"font",1); context=XYconv(context,"fly",1); context=XYconv(context,"mail",1); context=XYconv(context,"url",1); context=XYconv(context,"box",1); context=XYconv(context,"tab",1); context=context.replace(/\xbd\xbd/g,""); context=context.replace(/ /g," "); var tempstr; while(true){ if (/(<[a-z]+ [^>]*>)/i.test(context)){ tempstr=RegExp.$1.replace(/ /g," "); context=context.replace(/(<[a-z]+ [^>]*>)/i,tempstr); } else break; } context=context.replace(/\n/g,"<br>"); //context=context.replace(/\"/,"""); return context; }
function XYconv(context,cmd,mode){ var len=cmd.length; var convtxt=context; var lt="["+String.fromCharCode(0XBD,0XBD); var lt="["+String.fromCharCode(0XBD,0XBD); while(true){ var txt=convtxt.toLowerCase(); var p1=txt.indexOf("["+cmd); if (p1==-1) break; var p2=txt.indexOf("]",p1); if (p2==-1) break; var ltext=convtxt.substring(0,p1); var rtext=convtxt.substr(p2+1); if (mode==0){ //单目代码解释开始 var cmdtxt=txt.substring(p1+len+1,p2).replace(/(\s+)$/g,"").replace(/^(\s+)/g,""); switch(cmd){ case "img": //显示图片 //[IMG="图片路径" WIDTH=宽 HEIGHT=高 BORDER=边框宽度 COLOR=边框颜色 ALIGN=对齐方式] if (getImage(cmdtxt,"=")) { convtxt=ltext+"<IMG SRC="+RegExp.$1; if (getColor(cmdtxt,"color=")) clr=RegExp.$1; else clr=""; if (getNumber(cmdtxt,"border=")) if (clr=="") convtxt=convtxt+"BORDER="+RegExp.$1; else convtxt=convtxt+" STYLE=\"border:"+RegExp.$1+" solid "+clr+"\""; if (getNumberp(cmdtxt,"width=")) convtxt=convtxt+ " WIDTH=" +RegExp.$1; if (getNumberp(cmdtxt,"height=")) convtxt=convtxt+" HEIGHT="+RegExp.$1; if (getAlign(cmdtxt)) convtxt=convtxt+" ALIGN="+RegExp.$1; else if(getValign(cmdtxt)) convtxt=convtxt+" ALIGN="+RegExp.$1; convtxt=convtxt+">"+rtext; break; } convtxt=ltext+lt+convtxt.substr(p1+1); break; case "hr": //画横线 //[HR=横线式样 SIZE=横线粗细 WIDTH=宽 COLOR=横线颜色 ALIGN=对齐方式] //说明:所有参数均要省略 // 横线式样=1时表示没有阴影 // ALIGN=LEFT|CENTER|RIGHT convtxt=""; if (getStr(cmdtxt,"=1")) convtxt=" NOSHADE"; if (getNumber(cmdtxt,"size=")) convtxt=convtxt+" SIZE=" +RegExp.$1; if (getColor(cmdtxt,"color=")) convtxt=convtxt+" COLOR="+RegExp.$1; if (getNumberp(cmdtxt,"width=")) convtxt=convtxt+" WIDTH="+RegExp.$1; if (getAlign(cmdtxt)) convtxt=convtxt+" ALIGN="+RegExp.$1; convtxt=ltext.replace(/\r\n$/,"")+"<HR"+convtxt+">"+rtext.replace(/^\r\n/,""); break; case "flash": //显示FLASH //[FLASH="FLASH文件路径" WIDTH=宽 HEIGHT=高 MODE=显示模式 ALIGN=对齐方式] //说明:FLASH文件路径必须填写 // 显示模式=1时表示透明 // ALIGN=LEFT|CENTER|RIGHT|ABSBOTTOM|ABSMIDDLE|BASELINE|BOTTOM|MIDDLE|TEXTTOP|TOP
if (getPath(cmdtxt,"=")) { url=RegExp.$1; if (/\.swf\"$/.test(url)){ convtxt=ltext+"<EMBED SRC="+url; if (getStr(cmdtxt,"mode=1")) convtxt=convtxt+" WMODE=Transparent"; if (getNumberp(cmdtxt,"width=")) convtxt=convtxt+ " WIDTH=" +RegExp.$1; else convtxt=convtxt+" WIDTH=600"; if (getNumberp(cmdtxt,"height=")) convtxt=convtxt+" HEIGHT="+RegExp.$1; else convtxt=convtxt+" HEIGHT=400"; if (getAlign(cmdtxt)) convtxt=convtxt+" ALIGN="+RegExp.$1; else if(getValign(cmdtxt)) convtxt=convtxt+" ALIGN="+RegExp.$1; convtxt=convtxt+" TYPE=application/x-shockwave-flash QUALITY=high></EMBED>"+rtext; break; } } convtxt=ltext+lt+convtxt.substr(p1+1); break; case "vod": //播放视频 //[VOD="VOD文件路径" WIDTH=宽 HEIGHT=高 ALIGN=对齐方式] //说明:VOD文件路径必须填写 // ALIGN=LEFT|CENTER|RIGHT|ABSBOTTOM|ABSMIDDLE|BASELINE|BOTTOM|MIDDLE|TEXTTOP|TOP
if (getPath(cmdtxt,"=")) { url=RegExp.$1; if (getNumberp(cmdtxt,"width=")) w=RegExp.$1; else w=380; if (getNumberp(cmdtxt,"height=")) h=RegExp.$1; else h=350; agn=""; if (getAlign(cmdtxt)) agn=" ALIGN="+RegExp.$1; else if(getValign(cmdtxt)) agn=" ALIGN="+RegExp.$1; if (/\.(m3u|cda|mpg|mpeg|avi|rm|rmvb|mov)\"$/.test(url)){ convtxt=ltext+"<EMBED SRC="+url+" WIDTH="+w+" HEIGHT="+h+agn+" CONTROLS=all CONTROLS=imagewindow AUTOSTART=-1>"+rtext; break; } else if(/\.(wmv|asx|asf|wm)\"$/.test(url)){ convtxt=ltext+"<OBJECT CLASSID=clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6 WIDTH="+w+" HEIGHT="+h+agn+" CODEBASE=\"\"><PARAM NAME=URL VALUE="+url+"></OBJECT>"+rtext; break; } } convtxt=ltext+lt+convtxt.substr(p1+1); break; case "music": //播放音乐 //[MUSIC="音乐文件路径" CTRL=是否显示控制板 LOOP=循环次数 AUTO=是否自动播放] //说明:音乐文件路径必须填写
if (getPath(cmdtxt,"=")) { url=RegExp.$1; if (/\.(mp3|m3u|cda|wav|ra|rm|mid|midi|rmi|wma|mpg)\"$/.test(url)){ if (getNumber(cmdtxt,"loop=")) loop=RegExp.$1; else loop=1; convtxt=ltext+"<EMBED SRC="+url+" LOOP="+loop; if (getStr(cmdtxt,"ctrl=(-1|1|yes|true)")) { convtxt=convtxt+" HEIGHT=26 WIDTH=350 CONTROLS=ControlPanel"; if (getStr(cmdtxt,"auto=(0|no|false)")) convtxt=convtxt+" AUTOSTART=0"; else convtxt=convtxt+" AUTOSTART=-1"; } convtxt=convtxt+">"+rtext; break; } } convtxt=ltext+lt+convtxt.substr(p1+1); break; } //单目代码解释结束 } else if(mode==1){ //双目代码解释开始 p3=txt.lastIndexOf("[/"+cmd+"]"); if (p3<=p2) break; while(true){ p4=txt.indexOf("[/"+cmd+"]",p2+1); if (p4==-1 || p4>=p3) break; p5=txt.indexOf("["+cmd,p2+1); if (p5>p3 || p5<p4) break; p6=txt.indexOf("]"); if (p6==-1 || p6>p3) break; p3=p4; } //ltext=convtxt.substring(0,p1); rtext=convtxt.substr(p3+len+3); str1=convtxt.substring(p1+len+1,p2).replace(/(\s+) $/g,"").replace(/^(\s+)/g,""); str2=convtxt.substring(p2+1,p3); switch(cmd){ case "code": convtxt=ltext+"[box=1 pad=5 style=4 bgcolor=#eeeeee][size=10pt]"+str2.replace(/\[/g,lt).replace(/^[\x0a\x0d]+/,"")+"[/size][/box]"+rtext; break; case "i": case "b": case "u": //设置文字斜体、加粗、加下划线效果 //[I|B|U]显示内容[/I|B|U] convtxt=ltext+"<"+cmd.toUpperCase()+">"+str2+"</"+cmd.toUpperCase()+">"+rtext; break; case "left": case "center": case "right": //设置对齐方式 //[LEFT|CENTER|RIGHT]显示内容[/LEFT|CENTER|RIGHT] //说明:靠左|居中|靠右 convtxt=ltext+"<DIV ALIGN="+cmd+">"+str2+"</DIV>"+rtext; break; case "list": //设置项目列表 //[LIST=项目列表式样]显示内容[/LIST] //说明:项目列表式样=1|A|a|I|i // 每行自动项目编号,无编号式样时是园点式样 if (getStr(str1,"=([1|a|A|i|I])")) convtxt="<UL TYPE="+RegExp.$1+"><LI>"; else convtxt="<UL><LI>"; str2=str2.replace(/^([\n\r]+)/g,"").replace(/([\n\r]+)$/g,"").replace(/\n/g,"<LI>"); ltext=ltext.replace(/([\n\r]+)$/,""); rtext=rtext.replace(/^([\n\r]+)/,""); convtxt=ltext+convtxt+str2+"</UL>"+rtext; break; case "color": //设置文字颜色 //[COLOR=文字颜色]显示内容[/COLOR] if (getColor(str1,"=")) convtxt=ltext+"<FONT COLOR="+RegExp.$1+">"+str2+"</FONT>"+rtext; else convtxt=ltext+lt+convtxt.substr(p1+1); break; case "bg": //设置背景颜色和背景图片 //[BG=背景颜色 IMG="背景图片"]显示内容[/BG] bg=""; if (getColor(str1,"=")) bg="background-color:"+RegExp.$1+";"; if (getImage(str1,"img=")) bg=bg+"background-image:url("+url+");"; if (bg!="") convtext=ltext+"<FONT STYLE="+bg+">"+str2+"</FONT>"+rtext; else convtxt=ltext+lt+convtxt.substr(p1+1); break; case "size": //设置文字大小 //[SIZE=文字大小]显示内容[/SIZE]
if (getStr(str1,"=(\\d+)(pt|px|mm|em)?")) convtxt=ltext+"<FONT STYLE=font-size:"+RegExp.$1+RegExp.$2+">"+str2+"</FONT>"+rtext; else convtxt=ltext+lt+convtxt.substr(p1+1);
break; case "face": //设置文字字体 //[FACE="字体名称"]显示内容[/FACE] if(getPath(str1,"=")) convtxt=ltext+"<FONT FACE="+RegExp.$1+">"+str2+"</FONT>"+rtext; else convtxt=ltext+lt+convtxt.substr(p1+1); break; case "font": //设置文字属性 //[FONT COLOR=文字颜色 BGCOLOR=背景颜色 BG="背景图片" SIZE=文字大小 FACE="字体"]文字内容[/FONT] tmp=""; if (getColor(str1,"bgcolor=")) tmp=";background-color:"+RegExp.$1; if (getImage(str1,"bg=")) tmp=tmp+";background-image:url("+url+")"; if (getColor(str1,"color=")) tmp=tmp+";color:"+RegExp.$1; if (getStr(str1,"size=(\\d+)(pt|px|mm|em)?")) tmp=tmp+";font-size:"+RegExp.$1+RegExp.$2; if (getPath(str1,"face=")) tmp=tmp+";font-family:"+RegExp.$1; if (tmp!="") convtxt=ltext+"<FONT STYLE="+tmp.substr(1)+">"+str2+"</FONT>"+rtext; else convtxt=ltext+"<font>"+str2+"</font>"+rtext;
break; case "fly": //滚动字幕 //[FLY=滚动方向 STOP=鼠标移到字幕上时是否停止滚动 STEP=移动步长 WAIT=时间间隔 LOOP=滚动次数 WIDTH=字幕宽度 HEIGHT=字幕高度 COLOR=字幕颜色 BGCOLOR=背景颜色 BG="背景图片"]滚动内容[/FLY] //默认值:向左 步长4 间隔90 不限次数 convtxt=""; if (getStr(str1,"=(left|right|up|down|l|r|u|d|0|1|2|3)")) { switch(RegExp.$1){ case "1": case "right": case "r": m="right"; break; case "2": case "up": case "u": m="up"; break; case "3": case "down": case "d": m="down"; break; } convtxt=" DIRECTION="+m; }
if (getNumber(str1,"step=")) convtxt=convtxt+" SCROLLAMOUNT="+RegExp.$1; if (getNumber(str1,"wait=")) convtxt=convtxt+" SCROLLDELAY=" +RegExp.$1; if (getNumber(str1,"mode=")) { if (RegExp.$1=="3") convtxt=convtxt+" BEHAVIOR=silde"; else if(RegExp.$1=="2") convtxt=convtxt+" BEHAVIOR=alternate"; } if (getNumber(str1,"loop=")) convtxt=convtxt+" LOOP="+RegExp.$1; if (getNumberp(str1,"width=")) convtxt=convtxt+" WIDTH="+RegExp.$1; if (getNumberp(str1,"height=")) convtxt=convtxt+" HEIGHT="+RegExp.$1; if (getColor(str1,"color=")) convtxt=convtxt+" COLOR="+RegExp.$1; if (getColor(str1,"bgcolor=")) convtxt=convtxt+" BGCOLOR="+RegExp.$1; if (getImage(str1,"bg=")) convtxt=convtxt+" STYLE=background-image:url("+url+")"; if (getStr(str1,"stop")) convtxt=convtxt+" onmouseover=this.stop() onmouseout=this.start()" convtxt=ltext+"<MARQUEE"+convtxt+">"+str2+"</MARQUEE>"+rtext; break; case "url": case "mail": //加链接地址或加邮箱地址 //[URL|MAIL="地址" COLOR=链接颜色 NOLINE]链接显示内容[/URL|MAIL]
if (getStr(str1,"noline")) clr=" STYLE=text-decoration:none" else clr=" STYLE=text-decoration:underline" if (getColor(str1,"color=")) clr=clr+";color:"+RegExp.$1; if (str2!="" && getPath(str1,"=")){ if (cmd=="mail") convtxt=ltext+"<A HREF=\"mailto:"+(RegExp.$1.substr(1))+clr+">"+str2+"</A>"+rtext; else convtxt=ltext+"<A HREF="+RegExp.$1+clr+">"+str2+"</A>"+rtext; } else convtxt=ltext+lt+convtxt.substr(p1+1);
break; case "box": //为显示内容加边框 //[BOX=边框粗细 STYLE=边框样式 PADD=边框空白宽度 WIDTH=边框宽度 HEIGHT=边框高度 COLOR=边框颜色 BG=背景颜色或"图片" ALIGN=水平对齐方式 VALIGN=竖直对齐方式]显示内容[/BOX] if (getNumber(str1,"=")) box=";border-width:"+RegExp.$1; else box=";border-width:1"; if (getColor(str1,"color=")) box=box+";border-color:"+RegExp.$1; sty="solid"; if (getNumber(str1,"style=")){ switch(RegExp.$1){ case "0": sty="none"; break; case "2": sty="dotted"; break; case "3": sty="dashed"; break; case "4": sty="double"; break; case "5": sty="groove"; break; case "6": sty="ridge"; break; case "7": sty="inset"; break; case "8": sty="outset"; break; } } box=box+";border-style:"+sty; if (getNumberp(str1,"width=")) box=box+";width:"+RegExp.$1; if (getNumberp(str1,"height=")) box=box+";height:"+RegExp.$1; box=box.substr(1); if (box!="") box=" STYLE="+box; if (getNumber(str1,"padd=")) bg=";padding:"+RegExp.$1; else bg=";padding:5"; if (getColor(str1,"bgcolor=")) bg=bg+";background-color:"+RegExp.$1; if (getImage(str1,"bg=")) bg=bg+";background-image:url("+url+")"; if (getAlign(str1)) bg=bg+";text-align:"+RegExp.$1; bg=bg.substr(1); if (bg!="") bg=" STYLE="+bg; if (getValign(str1)) bg=bg+" VALIGN="+RegExp.$1; convtxt=ltext+"<DIV"+box+"><DIV"+bg+">"+str2.replace(/^\r\n/,"")+"</DIV></DIV>"+rtext.replace(/^\r\n/,""); break; case "tab": //显示表格 //[TAB][TD]内容1[TD]内容2....换行符[TD]内容1[TD]内容2....[/TAB] //说明: //[TAB]内参数: // LINESIZE =表格线宽度 // TABWIDTH =表格宽度 // TABHEIGHT=表格高度 // SPAC =单元格间距 // PADD =单元格边距 // HIGHCOLOR=亮线颜色 // DRAKCOLOR=暗线颜色 // TABBGCLR =表格背景颜色 // BG =表格背景图片 // LINECOLOR=表格线颜色 // 以下内容对所有单元格 // COLOR =文字颜色 // SIZE =文字大小 // FACE =文字字体 // // ALIGN =单元格内容水平对齐方式 // VALIGN =单元格内容竖直对齐方式 // HEIGHT =单元格高度 // BGCOLOR =单元格背景颜色 //[TD]内容参数: // 以下内容仅限指定单元格 // COLOR =文字颜色 // SIZE =文字大小 // FACE =文字字体 // // ALIGN =单元格内容水平对齐方式 // VALIGN =单元格内容竖直对齐方式 // HEIGHT =单元格高度 // BGCOLOR =单元格背景颜色 // BG =单元格背景图片 tmp=str2.replace(/^([\x0d\x0a]+)/,"").replace(/([\x0d\x0a]+)$/,""); if (tmp!=""){ tab="" if (getNumber(str1,"linesize=")) tab="<TABLE BORDER="+RegExp.$1; else tab="<TABLE BORDER=1"; if (getNumberp(str1,"tabwidth=")) tab=tab+" WIDTH="+RegExp.$1; else tab=tab+" WIDTH=100%"; if (getNumberp(str1,"tabheight=")) tab=tab+" HEIGHT="+RegExp.$1; if (getNumber(str1,"spac=")) tab=tab+" CELLSPACING="+RegExp.$1; if (getNumber(str1,"padd=")) tab=tab+" CELLPADDING="+RegExp.$1; if (getColor(str1,"linecolor=")) tab=tab+" BORDERCOLOR="+RegExp.$1; if (getColor(str1,"highcolor=")) tab=tab+" BORDERCOLORLIGH="+RegExp.$1; if (getColor(str1,"drakcolor=")) tab=tab+" BORDERCOLORDRAK="+RegExp.$1; if (getColor(str1,"tabbgclr=")) tab=tab+" BGCOLOR="+RegExp.$1; if (getImage(str1,"bg=")) tab=tab+" BACKGROUND="+RegExp.$1; ft=""; if (getColor(str1,"color=")) ft="color:"+RegExp.$1; if (getStr(str1,"size=(\\d+)(pt|px|mm|em)?")) ft=ft+";font-size:"+RegExp.$1+RegExp.$2; if (getPath(str1,"face=")) ft=ft+";font-family:"+RegExp.$1.replace(/\"/g,""); if (ft!="") ft=" STYLE=\""+ft+"\""; tab=tab+ft+">"; tdh=""; tdbg=""; agn=""; vgn=""; if (getAlign(str1,"align=")) agn=" ALIGN="+RegExp.$1; if (getValign(str1,"valign=")) vgn=" VALIGN="+RegExp.$1; if (getNumberp(str1,"height=")) tdh=" HEIGHT="+RegExp.$1; if (getColor(str1,"bgcolor=")) tdbg=" BGCOLOR="+RegExp.$1; tr=tmp.split("\r\n"); h=tr.length; w=0; for (i=0;i<h;i++){ td=tr[i].split(/\[td/i); if(w<td.length) w=td.length; } s=tab; for(i=0;i<h;i++){ td=tr[i].split(/\[td/i); ww=td.length; s=s+"<TR>"; for (j=0;j<w;j++){ if (j<ww){ //alert("“"+td[j]+"”\nlength="+td[j].length); k=td[j].indexOf("]"); if (k==-1){ x=td[j]; if (x=="") x=" " s=s+"<TD"+agn+vgn+tdbg+tdh+">"+x+"</TD>"; } else { x=td[j].substr(k+1); xx=td[j].substring(0,k); sty=agn; if (getAlign(xx)) sty=" ALIGN="+RegExp.$1; if (getValign(xx)) sty=sty+" VALIGN="+RegExp.$1; else sty=sty+vgn; if (getColor(xx,"bgcolor=")) sty=sty+" BGCOLOR="+RegExp.$1; else sty=sty+tdbg; if (getNumberp(xx,"height=")) sty=sty+" HEIGHT="+RegExp.$1; else sty=sty+tdh; if (getImage(xx,"bg=")) sty=sty+" BACKGROUND="+RegExp.$1; ft=""; if (getColor(xx,"color=")) ft="color:"+RegExp.$1; if (getStr(xx,"size=(\\d+)(pt|px|mm|em)?")) ft=ft+";font-size:"+RegExp.$1+RegExp.$2; if (getPath(xx,"face=")) ft=ft+";font-family:"+RegExp.$1.replace(/\"/g,""); if (ft!="") ft=" STYLE=\""+ft+"\""; if (x=="") x=" " s=s+"<TD"+sty+ft+">"+x+"</TD>"; } } else s=s+"<TD"+agn+vgn+tdbg+tdh+"> </TD>"; } s=s+"</tr>"; } convtxt=ltext+s+"</TABLE>"+rtext; break; } convtxt=ltext+lt+convtxt.substr(p1+1); break; } //双目代码解释结束 } else { //多目代码解释开始 //多目代码解释结束 } } return convtxt; }
function getAlign(str){ str=" "+str+" "; return / align=(LEFT|CENTER|RIGHT) /i.test(str); }
function getValign(str){ str=" "+str+" "; return / valign=(ABSBOTTOM|ABSMIDDLE|BASELINE|BOTTOM|MIDDLE|TEXTTOP|TOP) /i.test(str); }
function getNumberp(str,cmd){ str=" "+str+" "; eval("re=/ "+cmd+"(\\d+%?) /i"); return re.test(str); }
function getNumber(str,cmd){ str=" "+str+" "; eval("re=/ "+cmd+"(\\-?\\d+) /i"); return re.test(str); }
function getColor(str,cmd){ str=" "+str+" "; eval("re=/ "+cmd+"(\\d+) /i"); if (re.test(str)) return true; eval("re=/ "+cmd+"(#[0-9a-z]{6}) /i"); if (re.test(str)) return true; eval("re=/ "+cmd+"(red|blue|write|yellow|teal|tan|aqua|black|brown|cyan|gray|green) /i"); return re.test(str); }
function getImage(str,cmd){ str=" "+str+" "; if (/\.(gif|jpeg|jpg|png)\" $/i.test(str)){ eval("re=/ "+cmd+"(\"[^\\f\\n\\r\\t\\v]+\") /i"); return re.test(str); } return false; }
function getStr(str,cmd){ str=" "+str+" "; eval("re=/ "+cmd+" /i"); return re.test(str); }
function getPath(str,cmd){ str=" "+str+" "; eval("re=/ "+cmd+"(\"[^\\f\\n\\r\\t\\v]+\") /i"); return re.test(str); } 
|