VBScript版代码高亮 VBScript版代码高亮 <BR>'======================================<BR>'代码高亮类<BR>'使用方法:<BR>'Set HL = New Highlight '定义类<BR>'HL.Language = "vb" '指定程序语言,支持 VBS ,JS ,XML, HTML, SQL, C#, Java...等<BR>'还可通过直接设置下列属性还设置相关关键字等<BR>' Public Keywords '关键字<BR>' Public Objects '对象<BR>' Public SplitWords '分隔符<BR>' Public LineComment '行注释<BR>' Public CommentOn '多行注释<BR>' Public CommentOff '多行注释结束<BR>' Public Ignore '是否区分大小写<BR>' Public CodeContent '代码内容<BR>' Public Tags '标记<BR>' Public StrOn '字符串标记<BR>' Public Escape '字符串界定符转义<BR>' Public IsMultiple '允许多行引用<BR>'HL.CodeContent = "要高亮的代码内容"<BR>'Response.Write(Hl.Execute) '该方法返回高亮后的代码<BR>'===================================== <P><FONT face=Verdana>Class Highlight<BR> Public Keywords '关键字<BR> Public Objects '对象<BR> Public SplitWords '分隔符<BR> Public LineComment '行注释<BR> Public CommentOn '多行注释<BR> Public CommentOff '多行注释结束<BR> Public Ignore '是否区分大小写<BR> Public CodeContent '代码内容<BR> Public Tags '标记<BR> Public StrOn '字符串标记<BR> Public Escape '字符串界定符转义<BR> Public IsMultiple '允许多行引用<BR> Private Content <P><FONT face=Verdana> Private Sub Class_Initialize<BR> Keywords = "function,void,this,boolean,while,if,return,new,true,false,try,catch,throw,null,else,int,long,do,var" '关键字<BR> Objects = "src,width,border,cellspacing,cellpadding,align,bgcolor,class,style,href,type,name,String,Number,Boolean,RegExp,Error,Math,Date" '对象<BR> SplitWords = " ,.?!;:\/<>(){}[]""'=+-|*%@#$^&"&VBCRLF&CHR(9) '分隔符<BR> LineComment = "//" '行注释<BR> CommentOn = "/*" '多行注释<BR> CommentOff = "*/" '多行注释结束<BR> Ignore = 0 '是否区分大小写<BR> Tags = "a,img,html,head,body,title,style,script,language,input,select,div,span,button,img,iframe,frame,frameset,table,tr,td,caption,form,font,meta,textarea" '标记<BR> StrOn = """'" '字符串标记<BR> Escape = "\" '字符串界定符转义<BR> CodeContent = ""<BR> End Sub <P><FONT face=Verdana> Public Function Execute<BR> Dim S<BR> Dim T, Key, X, Str<BR> Dim Flag<BR> Flag = 1: S = 1<BR> For i = 1 to Len(CodeContent)<BR> If Instr(1, SplitWords, Mid(CodeContent, i, 1) , 0)>0 Then<BR> If Flag = 1 Then<BR> Key = Mid(Codecontent, S, i - S)<BR> If Keywords<>"" And Instr(1, ","& Keywords &"," , ","&Key&"," , Ignore)>0 Then<BR> Content = Content& "<font color=""blue"">"&Key&""<BR> ElseIf Objects<>"" And Instr(1,","& Objects &",", ","&Key&"," , Ignore)>0 Then<BR> Content = Content & "<font color=""red"">"&Key&""<BR> ElseIf Tags <>"" And Instr(1, ","& Tags &",", ","&Key&"," , Ignore)>0 Then<BR> Content = Content & "<font color=""#996600"">"&Key&""<BR> Else<BR> Content = Content & Key<BR> End If<BR> End if<BR> Flag = 0<BR> X = Mid(CodeContent, i, 1)<BR> If LineComment<>"" And Mid(CodeContent, i, Len(LineComment)) = LineComment Then<BR> S = Instr(i ,CodeContent, VBCRLF)<BR> if S = 0 Then<BR> S = Len(CodeContent)<BR> End if<BR> Content = Content & "<font color=""Green"">"& HtmlEnCode(Mid(CodeContent,i ,S - i ))&""<BR> i = S<BR> ElseIf StrOn<>"" And Instr(StrOn,Mid(CodeContent, i, 1))>0 Then<BR> Str = Mid(CodeContent, i, 1)<BR> S = i<BR> Do<BR> S = Instr(S + 1 ,CodeContent, Str, 1)<BR> if S <> 0 Then<BR> T = S - 1<BR> Do While Mid(CodeContent, T, 1) = Escape<BR> T = T-1<BR> Loop<BR> If (S -T) Mod 2 = 1 Then<BR> Exit Do<BR> End If<BR> Else<BR> S = Len(CodeContent)<BR> Exit Do<BR> End If<BR> Loop While 1<BR> Content = Content & "<font color=""#FF00FF"">"& HtmlEnCode(Mid(CodeContent,i, S - i + 1))&""<BR> i = S<BR> ElseIf CommentOn<>"" And Mid(CodeContent, i, Len(CommentOn)) = CommentOn Then<BR> S = Instr(i ,CodeContent, CommentOff, 1)<BR> if S = 0 Then<BR> S = Len(CodeContent)<BR> End if<BR> Content = Content & "<font color=""Green"">"& HtmlEnCode(Mid(CodeContent,i, S - i + Len(CommentOff) ))&""<BR> i = S + Len(CommentOff)<BR> ElseIf X = "" Then<BR> Content = Content & " "<BR> ElseIf X = """" Then<BR> Content = Content & """<BR> ElseIf X = "&" Then<BR> Content = Content & "&"<BR> ElseIf X = "<" Then<BR> Content = Content & "<"<BR> ElseIf X = ">" Then<BR> Content = Content & ">"<BR> ElseIf X = Chr(9) Then<BR> Content = Content & " "<BR> ElseIf X = VBLF Then<BR> Content = Content & "<br />"<BR> Else<BR> Content = Content & X<BR> End If<BR> Else<BR> If Flag = 0 Then<BR> S = i<BR> Flag = 1<BR> End if<BR> End If<BR> Next<BR> if Flag = 1 Then<BR> Execute = Content & Mid(CodeContent, S)<BR> Else<BR> Execute = content<BR> End If<BR> End Function <P><FONT face=Verdana> Private Function HtmlEnCode(Str)<BR> If IsNull(Str) Then<BR> HtmlEnCode = "": Exit Function<BR> End if<BR> Str = Replace(Str ,"&","&")<BR> Str = Replace(Str ,"<","<")<BR> Str = Replace(Str ,">",">")<BR> Str = Replace(Str ,"""",""")<BR> Str = Replace(Str ,Chr(9)," ")<BR> Str = Replace(Str ," "," ")<BR> Str = Replace(Str ,VBLF,"<br />")<BR> HtmlEnCode = Str<BR> End Function <P><FONT face=Verdana> Public Property Let Language(Str)<BR> Dim S<BR> S = UCase(Str)<BR> Select Case true<BR> Case S = "VB" Or S = "VBS" OR S = "VBSCRIPT":<BR> Keywords = "And,ByRef,ByVal,Call,Case,Class,Const,Dim,Do,Each,Else,ElseIf,Empty,End,Eqv,Erase,Error,Exit,Explicit,False,For,Function,Get,If,Imp,In,Is,Let,Loop,Mod,Next,Not,Nothing,Null,On,Option,Or,Private,Property,Public,Randomize,ReDim,Resume,Select,Set,Step,Sub,Then,To,True,Until,Wend,While,Xor,Anchor,Array,Asc,Atn,CBool,CByte,CCur,CDate,CDbl,Chr,CInt,CLng,Cos,CreateObject,CSng,CStr,Date,DateAdd,DateDiff,DatePart,DateSerial,DateValue,Day,Dictionary,Document,Element,Err,Exp,FileSystemObject,Filter,Fix,Int,Form,FormatCurrency,FormatDateTime,FormatNumber,FormatPercent,GetObject,Hex,Hour,InputBox,InStr,InstrRev,IsArray,IsDate,IsEmpty,IsNull,IsNumeric,IsObject,Join,LBound,LCase,Left,Len,Link,LoadPicture,Location,Log,LTrim,RTrim,Trim,Mid,Minute,Month,MonthName,MsgBox,Navigator,Now,Oct,Replace,Right,Rnd,Round,ScriptEngine,ScriptEngineBuildVersion,ScriptEngineMajorVersion,ScriptEngineMinorVersion,Second,Sgn,Sin,Space,Split,Sqr,StrComp,String,StrReverse,Tan,Time,TextStream,TimeSerial,TimeValue,TypeName,UBound,UCase,VarType,Weekday,WeekDayName,Year,Function"<BR> Objects ="String,Number,Boolean,Date,Integert,Long,Double,Single"<BR> SplitWords = ",.?!;:\/<>(){}[]""'=+-|*%@#$^& "&VBCRLF&Chr(9)<BR> LineComment = "'"<BR> CommentOn = ""<BR> CommentOff = ""<BR> StrOn = """"<BR> Escape = ""<BR> Ignore = 1<BR> CodeContent = ""<BR> Tags = "" <P><FONT face=Verdana> Case s = "C#":<BR> Keywords = "abstract,as,base,bool,break,byte,case,catch,char,checked,class,const,continue,decimal,default,delegate,do,double,else,enum,event,explicit,extern,false,finally,fixed,float,for,foreach,get,goto,if,implicit,in,int,interface,internal,is,lock,long,namespace,new,null,object,operator,out,override,params,private,protected,public,readonly,ref,return,sbyte,sealed,short,sizeof,stackalloc,static,set,string,struct,switch,this,throw,true,try,typeof,uint,ulong,unchecked,unsafe,ushort,using,value,virtual,void,volatile,while" '关键字<BR> Objects = "String,Boolean,DateTime,Int32,Int64,Exception,DataTable,DataReader" '对象<BR> SplitWords = " ,.?!;:\/<>(){}[]""'=+-|*%@#$^&"&VBCRLF&CHR(9) '分隔符<BR> LineComment = "//" '行注释<BR> CommentOn = "/*" '多行注释<BR> CommentOff = "*/" '多行注释结束<BR> Ignore = 0 '是否区分大小写<BR> Tags = "" '标记<BR> StrOn = """" '字符串标记<BR> Escape = "\" '字符串界定符转义 <P><FONT face=Verdana> Case S = "JAVA" :<BR> Keywords = "abstract,boolean,break,byte,case,catch,char,class,const,continue,default,do,double,else,extends,final,finally,float,for,goto,if,implements,import,instanceof,int,interface,long,native,new,package,private,protected,public,return,short,static,strictfp,super,switch,synchronized,this,throw,throws,transient,try,void,volatile,while" '关键字<BR> Objects = "String,Boolean,DateTime,Int32,Int64,Exception,DataTable,DataReader" '对象<BR> SplitWords = " ,.?!;:\/<>(){}[]""'=+-|*%@#$^&"&VBCRLF&CHR(9) '分隔符<BR> LineComment = "//" '行注释<BR> CommentOn = "/*" '多行注释<BR> CommentOff = "*/" '多行注释结束<BR> Ignore = 0 '是否区分大小写<BR> Tags = "" '标记<BR> StrOn = """" '字符串标记<BR> Escape = "\" '字符串界定符转义 <P><FONT face=Verdana> Case S = "JS" OR S = "JSCRIPT" OR S = "JAVASCRIPT":<BR> Keywords = "function,void,this,boolean,while,if,return,new,true,false,try,catch,throw,null,else,int,long,do,var" '关键字<BR> Objects = "String,Number,Boolean,RegExp,Error,Math,Date" '对象<BR> SplitWords = " ,.?!;:\/<>(){}[]""'=+-|*%@#$^&"&VBCRLF&CHR(9) '分隔符<BR> LineComment = "//" '行注释<BR> CommentOn = "/*" '多行注释<BR> CommentOff = "*/" '多行注释结束<BR> Ignore = 0 '是否区分大小写<BR> Tags = "" '标记<BR> StrOn = """" '字符串标记<BR> Escape = "\" '字符串界定符转义 <P><FONT face=Verdana> Case S = "XML":<BR> Keywords = "!DOCTYPE,?xml,script,version,encoding" '关键字<BR> Objects = "String,Number,Boolean,RegExp,Error,Math,Date" '对象<BR> SplitWords = " ,.?!;:\/<>(){}[]""'=+-|*%@#$^&"&VBCRLF&CHR(9) '分隔符<BR> LineComment = "//" '行注释<BR> CommentOn = "<!--" '多行注释<BR> CommentOff = "-->" '多行注释结束<BR> Ignore = 0 '是否区分大小写<BR> Tags = "" '标记<BR> StrOn = """" '字符串标记<BR> Escape = "\" '字符串界定符转义 <P><FONT face=Verdana> Case S = "HTML":<BR> Case S = "SQL":<BR> Keywords = "COMMIT,DELETE,INSERT,LOCK,ROLLBACK,SELECT,TRANSACTION,READ,ONLY,WRITE,USE,ROLLBACK,SEGMENT,ROLE,EXCEPT,NONE,UPDATE,DUAL,WORK,COMMENT,FORCE,FROM,WHERE,INTO,VALUES,ROW,SHARE,MODE,EXCLUSIVE,UPDATE,ROW,NOWAIT,TO,SAVEPOINT,UNION,UNION,ALL,INTERSECT,MINUS,START,WITH,CONNECT,BY,GROUP,HAVING,ORDER,UPDATE,NOWAIT,IDENTIFIED,SET,DROP,PACKAGE,CREATE,REPLACE,PROCEDURE,FUNCTION,TABLE,RETURN,AS,BEGIN,DECLARE,END,IF,THEN,ELSIF,ELSE,WHILE,CURSOR,EXCEPTION,WHEN,OTHERS,NO_DATA_FOUND,TOO_MANY_ROWS,CURSOR_ALREADY_OPENED,FOR,LOOP,IN,OUT,TYPE,OF,INDEX,BINARY_INTEGER,RAISE,ROWTYPE,VARCHAR2,NUMBER,LONG,DATE,RAW,LONG RAW,CHAR,INTEGER,MLSLABEL,CURRENT,OF,DEFAULT,CURRVAL,NEXTVAL,LEVEL,ROWID,ROWNUM,DISTINCT,ALL,LIKE,IS,NOT,NULL,BETWEEN,ANY,AND,OR,EXISTS,ASC,DESC,ABS,CEIL,COS,COSH,EXP,FLOOR,LN,LOG,MOD,POWER,ROUND,SIGN,SIN,SINH,SQRT,TAN,TANH,TRUNC,CHR,CONCAT,INITCAP,LOWER,LPAD,LTRIM,NLS_INITCAP,NLS_LOWER,NLS_UPPER,REPLACE,RPAD,RTRIM,SOUNDEX,SUBSTR,SUBSTRB,TRANSLATE,UPPER,ASCII,INSTR,INSTRB,LENGTH,LENGTHB,NLSSORT,ADD_MONTHS,LAST_DAY,MONTHS_BETWEEN,NEW_TIME,NEXT_DAY,ROUND,SYSDATE,TRUNC,CHARTOROWID,CONVERT,HEXTORAW,RAWTOHEX,ROWIDTOCHAR,TO_CHAR,TO_DATE,TO_LABEL,TO_MULTI_BYTE,TO_NUMBER,TO_SINGLE_BYTE,DUMP,GREATEST,GREATEST_LB,LEAST,LEAST_UB,NVL,UID,USER,USERENV,VSIZE,AVG,COUNT,GLB,LUB,MAX,MIN,STDDEV,SUM,VARIANCE" '关键字<BR> Objects = "" '对象<BR> SplitWords = " ,.?!;:\\/<>(){}[]""'=+-|*%@#$^&"&VBCRLF&CHR(9) '分隔符<BR> LineComment = "--" '行注释<BR> CommentOn = "/*" '多行注释<BR> CommentOff = "*/" '多行注释结束<BR> Ignore = 1 '是否区分大小写<BR> Tags = "" '标记<BR> StrOn = "'" '字符串标记<BR> Escape = "" '字符串界定符转义<BR> End Select<BR> End Property<BR>End Class<BR><BR>Function plaster()<BR> document.form1.code.focus()<BR> document.execCommand("Paste")<BR>End Function <P><FONT face=Verdana>Function goit(stx)<BR> Dim code,HL<BR> code = Document.all.code.value<BR> Set HL = New Highlight<BR> HL.Language = stx<BR> HL.CodeContent = code<BR> document.getElementById("highlight").innerHTML = Hl.Execute<BR>End Function<BR>