科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道代码高亮类(VBS),支持多种程序设计语言

代码高亮类(VBS),支持多种程序设计语言

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

本文是关于如何实现支持多种程序设计语言的代码高亮类(VBS)

作者:月临河 来源:51CTO.com整理 2007年9月10日

关键字: 代码高亮类 程序设计

  • 评论
  • 分享微博
  • 分享邮件

'使用方法:

'SetHL=NewHighlight'定义类

'HL.Language="vb"'指定程序语言,支持VBS,JS,XML,HTML,SQL,C#,Java...等

'还可通过直接设置下列属性还设置相关关键字等

'PublicKeywords'关键字

'PublicObjects'对象

'PublicSplitWords'分隔符

'PublicLineComment'行注释

'PublicCommentOn'多行注释

'PublicCommentOff'多行注释结束

'PublicIgnore'是否区分大小写

'PublicCodeContent'代码内容

'PublicTags'标记

'PublicStrOn'字符串标记

'PublicEscape'字符串界定符转义

'PublicIsMultiple'允许多行引用

'HL.CodeContent="要高亮的代码内容"

'Response.Write(Hl.Execute)'该方法返回高亮后的代码

'=====================================

ClassHighlight

PublicKeywords'关键字

PublicObjects'对象

PublicSplitWords'分隔符

PublicLineComment'行注释

PublicCommentOn'多行注释

PublicCommentOff'多行注释结束

PublicIgnore'是否区分大小写

PublicCodeContent'代码内容

PublicTags'标记

PublicStrOn'字符串标记

PublicEscape'字符串界定符转义

PublicIsMultiple'允许多行引用

PrivateContent

PrivateSubClass_Initialize

Keywords="function,void,this,boolean,while,if,return,new,true,false,try,catch,throw,null,else,int,long,do,var"'关键字

Objects="src,width,border,cellspacing,cellpadding,align,bgcolor,class,style,href,type,name,String,Number,Boolean,RegExp,Error,Math,Date"'对象

SplitWords=",.?!;:\/<>(){}[]""'=+-|*%@#$^&"&VBCRLF&CHR(9)'分隔符

LineComment="//"'行注释

CommentOn="/*"'多行注释

CommentOff="*/"'多行注释结束

Ignore=0'是否区分大小写

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"'标记

StrOn="""'"'字符串标记

Escape="\"'字符串界定符转义

CodeContent=""

EndSub

PublicFunctionExecute

DimS

DimT,Key,X,Str

DimFlag

Flag=1:S=1

Fori=1toLen(CodeContent)

IfInstr(1,SplitWords,Mid(CodeContent,i,1),0)>0Then

IfFlag=1Then

Key=Mid(Codecontent,S,i-S)

IfKeywords<>""AndInstr(1,","&Keywords&",",","&Key&",",Ignore)>0Then

Content=Content&"<fontcolor=""blue"">"&Key&"</font>"

ElseIfObjects<>""AndInstr(1,","&Objects&",",","&Key&",",Ignore)>0Then

Content=Content&"<fontcolor=""red"">"&Key&"</font>"

ElseIfTags<>""AndInstr(1,","&Tags&",",","&Key&",",Ignore)>0Then

Content=Content&"<fontcolor=""#996600"">"&Key&"</font>"

Else

Content=Content&Key

EndIf

Endif

Flag=0

X=Mid(CodeContent,i,1)

IfLineComment<>""AndMid(CodeContent,i,Len(LineComment))=LineCommentThen

S=Instr(i,CodeContent,VBCRLF)

ifS=0Then

S=Len(CodeContent)

Endif

Content=Content&"<fontcolor=""Green"">"&HtmlEnCode(Mid(CodeContent,i,S-i))&"</font>"

i=S

ElseIfStrOn<>""AndInstr(StrOn,Mid(CodeContent,i,1))>0Then

Str=Mid(CodeContent,i,1)

S=i

Do

S=Instr(S+1,CodeContent,Str,1)

ifS<>0Then

T=S-1

DoWhileMid(CodeContent,T,1)=Escape

T=T-1

Loop

If(S-T)Mod2=1Then

ExitDo

EndIf

Else

S=Len(CodeContent)

ExitDo

EndIf

LoopWhile1

Content=Content&"<fontcolor=""#FF00FF"">"&HtmlEnCode(Mid(CodeContent,i,S-i+1))&"</font>"

i=S

ElseIfCommentOn<>""AndMid(CodeContent,i,Len(CommentOn))=CommentOnThen

S=Instr(i,CodeContent,CommentOff,1)

ifS=0Then

S=Len(CodeContent)

Endif

Content=Content&"<fontcolor=""Green"">"&HtmlEnCode(Mid(CodeContent,i,S-i+Len(CommentOff)))&"</font>"

i=S+Len(CommentOff)

ElseIfX=""Then

Content=Content&"&nbsp;"

ElseIfX=""""Then

Content=Content&"&quot;"

ElseIfX="&"Then

Content=Content&"&amp;"

ElseIfX="<"Then

Content=Content&"&lt;"

ElseIfX=">"Then

Content=Content&"&gt;"

ElseIfX=Chr(9)Then

Content=Content&"&nbsp;&nbsp;&nbsp;"

ElseIfX=VBLFThen

Content=Content&"<br/>"

Else

Content=Content&X

EndIf

Else

IfFlag=0Then

S=i

Flag=1

Endif

EndIf

Next

ifFlag=1Then

Execute=Content&Mid(CodeContent,S)

Else

Execute=content

EndIf

EndFunction

PrivateFunctionHtmlEnCode(Str)

IfIsNull(Str)Then

HtmlEnCode="":ExitFunction

Endif

Str=Replace(Str,"&","&amp;")

Str=Replace(Str,"<","&lt;")

Str=Replace(Str,">","&gt;")

Str=Replace(Str,"""","&quot;")

Str=Replace(Str,Chr(9),"&nbsp;&nbsp;&nbsp;")

Str=Replace(Str,"","&nbsp;")

Str=Replace(Str,VBLF,"<br/>")

HtmlEnCode=Str

EndFunction

PublicPropertyLetLanguage(Str)

DimS

S=UCase(Str)

SelectCasetrue

CaseS="VB"OrS="VBS"ORS="VBSCRIPT":

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"

Objects="String,Number,Boolean,Date,Integert,Long,Double,Single"

SplitWords=",.?!;:\/<>(){}[]""'=+-|*%@#$^&"&VBCRLF&Chr(9)

LineComment="'"

CommentOn=""

CommentOff=""

StrOn=""""

Escape=""

Ignore=1

CodeContent=""

Tags=""

Cases="C#":

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"'关键字

Objects="String,Boolean,DateTime,Int32,Int64,Exception,DataTable,DataReader"'对象

SplitWords=",.?!;:\/<>(){}[]""'=+-|*%@#$^&"&VBCRLF&CHR(9)'分隔符

LineComment="//"'行注释

CommentOn="/*"'多行注释

CommentOff="*/"'多行注释结束

Ignore=0'是否区分大小写

Tags=""'标记

StrOn=""""'字符串标记

Escape="\"'字符串界定符转义

CaseS="JAVA":

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"'关键字

Objects="String,Boolean,DateTime,Int32,Int64,Exception,DataTable,DataReader"'对象

SplitWords=",.?!;:\/<>(){}[]""'=+-|*%@#$^&"&VBCRLF&CHR(9)'分隔符

LineComment="//"'行注释

CommentOn="/*"'多行注释

CommentOff="*/"'多行注释结束

Ignore=0'是否区分大小写

Tags=""'标记

StrOn=""""'字符串标记

Escape="\"'字符串界定符转义

CaseS="JS"ORS="JSCRIPT"ORS="JAVASCRIPT":

Keywords="function,void,this,boolean,while,if,return,new,true,false,try,catch,throw,null,else,int,long,do,var"'关键字

Objects="String,Number,Boolean,RegExp,Error,Math,Date"'对象

SplitWords=",.?!;:\/<>(){}[]""'=+-|*%@#$^&"&VBCRLF&CHR(9)'分隔符

LineComment="//"'行注释

CommentOn="/*"'多行注释

CommentOff="*/"'多行注释结束

Ignore=0'是否区分大小写

Tags=""'标记

StrOn=""""'字符串标记

Escape="\"'字符串界定符转义

CaseS="XML":

Keywords="!DOCTYPE,?xml,script,version,encoding"'关键字

Objects="String,Number,Boolean,RegExp,Error,Math,Date"'对象

SplitWords=",.?!;:\/<>(){}[]""'=+-|*%@#$^&"&VBCRLF&CHR(9)'分隔符

LineComment="//"'行注释

CommentOn="<!--"'多行注释

CommentOff="-->"'多行注释结束

Ignore=0'是否区分大小写

Tags=""'标记

StrOn=""""'字符串标记

Escape="\"'字符串界定符转义

CaseS="HTML":

CaseS="SQL":

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,LONGRAW,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"'关键字

Objects=""'对象

SplitWords=",.?!;:\\/<>(){}[]""'=+-|*%@#$^&"&VBCRLF&CHR(9)'分隔符

LineComment="--"'行注释

CommentOn="/*"'多行注释

CommentOff="*/"'多行注释结束

Ignore=1'是否区分大小写

Tags=""'标记

StrOn="'"'字符串标记

Escape=""'字符串界定符转义

EndSelect

EndProperty

EndClass

调用方法:

SetHL=NewHighlight'创建类的副本

HL.Language="vb"'指定程序语言,支持VBS,JS,XML,HTML,SQL,C#,Java...等

'还可通过直接设置下列属性还设置相关关键字等

'PublicKeywords'关键字

'PublicObjects'对象

'PublicSplitWords'分隔符

'PublicLineComment'行注释

'PublicCommentOn'多行注释

'PublicCommentOff'多行注释结束

'PublicIgnore'是否区分大小写

'PublicCodeContent'代码内容

'PublicTags'标记

'PublicStrOn'字符串标记

'PublicEscape'字符串界定符转义

'PublicIsMultiple'允许多行引用

HL.CodeContent="要高亮的代码内容"

Response.Write(Hl.Execute)'该方法返回高亮后的代码

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章