09-12
07

ASP 代码高亮类

'======================================
'代码高亮类
'使用方法:
'Set HL = New Highlight '定义类
'HL.Language = "vb" '指定程序语言,支持 VBS ,JS ,XML, HTML, SQL, C#, Java...等
'还可通过直接设置下列属性还设置相关关键字等
' Public Keywords  '关键字
' Public Objects  '对象
' Public SplitWords '分隔符
' Public LineComment '行注释
' Public CommentOn '多行注释
' Public CommentOff '多行注释结束
' Public Ignore  '是否区分大小写
' Public CodeContent '代码内容
' Public Tags   '标记
' Public StrOn  '字符串标记
' Public Escape  '字符串界定符转义
' Public IsMultiple '允许多行引用
'HL.CodeContent = "要高亮的代码内容"
'Response.Write(Hl.Execute) '该方法返回高亮后的代码
'=====================================

折叠展开vb 代码复制内容到剪贴板
  1. Class Highlight      
  2. Public Keywords '关键字      
  3. Public Objects '对象      
  4. Public SplitWords '分隔符      
  5. Public LineComment '行注释      
  6. Public CommentOn '多行注释      
  7. Public CommentOff '多行注释结束      
  8. Public Ignore '是否区分大小写      
  9. Public CodeContent '代码内容      
  10. Public Tags '标记      
  11. Public StrOn '字符串标记      
  12. Public Escape '字符串界定符转义      
  13. Public IsMultiple '允许多行引用      
  14. Private Content      
  15.      
  16. Private Sub Class_Initialize      
  17. Keywords = "function,void,this,boolean,while,if,return,new,true,false,try,catch,throw,null,else,int,long,do,var" '关键字      
  18. Objects = "src,width,border,cellspacing,cellpadding,align,bgcolor,class,style,href,type,name,String,Number,Boolean,RegExp,Error,Math,Date" '对象      
  19. SplitWords = " ,.?!;:/<>(){}[]""'=+-|*%@#$^&"&VBCRLF&CHR(9) '分隔符      
  20. LineComment = "//" '行注释      
  21. CommentOn = "/*" '多行注释      
  22. CommentOff = "*/" '多行注释结束      
  23. Ignore = 0 '是否区分大小写      
  24. 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" '标记      
  25. StrOn = """'" '字符串标记      
  26. Escape = "" '字符串界定符转义      
  27. CodeContent = ""     
  28. End Sub     
  29.      
  30. Public Function Execute      
  31. Dim S      
  32. Dim T, Key, X, Str      
  33. Dim Flag      
  34. Flag = 1: S = 1      
  35. For i = 1 to Len(CodeContent)      
  36. If Instr(1, SplitWords, Mid(CodeContent, i, 1) , 0)>0 Then     
  37. If Flag = 1 Then     
  38. Key = Mid(Codecontent, S, i - S)      
  39. If Keywords<>"" And Instr(1, ","& Keywords "," , ","&Key"," , Ignore)>0 Then     
  40. Content = Content& "<font color=""blue"">"&Key"</font>"     
  41. ElseIf Objects<>"" And Instr(1,","& Objects ","","&Key"," , Ignore)>0 Then     
  42. Content = Content & "<font color=""red"">"&Key"</font>"     
  43. ElseIf Tags <>"" And Instr(1, ","& Tags ","","&Key"," , Ignore)>0 Then     
  44. Content = Content & "<font color=""#996600"">"&Key"</font>"     
  45. Else     
  46. Content = Content & Key      
  47. End If     
  48. End if      
  49. Flag = 0      
  50. X = Mid(CodeContent, i, 1)      
  51. If LineComment<>"" And Mid(CodeContent, i, Len(LineComment)) = LineComment Then     
  52. S = Instr(i ,CodeContent, VBCRLF)      
  53. if S = 0 Then     
  54. S = Len(CodeContent)      
  55. End if      
  56. Content = Content & "<font color=""Green"">"& HtmlEnCode(Mid(CodeContent,i ,S - i ))"</font>"     
  57. i = S      
  58. ElseIf StrOn<>"" And Instr(StrOn,Mid(CodeContent, i, 1))>0 Then     
  59. Str = Mid(CodeContent, i, 1)      
  60. S = i      
  61. Do     
  62. S = Instr(S + 1 ,CodeContent, Str, 1)      
  63. if S <> 0 Then     
  64. T = S - 1      
  65. Do While Mid(CodeContent, T, 1) = Escape      
  66. T = T-1      
  67. Loop     
  68. If (S -T) Mod 2 = 1 Then     
  69. Exit Do     
  70. End If     
  71. Else     
  72. S = Len(CodeContent)      
  73. Exit Do     
  74. End If     
  75. Loop While 1      
  76. Content = Content & "<font color=""#FF00FF"">"& HtmlEnCode(Mid(CodeContent,i, S - i + 1))"</font>"     
  77. i = S      
  78. ElseIf CommentOn<>"" And Mid(CodeContent, i, Len(CommentOn)) = CommentOn Then     
  79. S = Instr(i ,CodeContent, CommentOff, 1)      
  80. if S = 0 Then     
  81. S = Len(CodeContent)      
  82. End if      
  83. Content = Content & "<font color=""Green"">"& HtmlEnCode(Mid(CodeContent,i, S - i + Len(CommentOff) ))"</font>"     
  84. i = S + Len(CommentOff)      
  85. ElseIf X = "" Then     
  86. Content = Content & " "     
  87. ElseIf X = """" Then     
  88. Content = Content & ""    
  89. ElseIf X = "" Then     
  90. Content = Content & "    
  91. ElseIf X = "<" Then     
  92. Content = Content & "<    
  93. ElseIf X = ">" Then     
  94. Content = Content & ">    
  95. ElseIf X = Chr(9) Then     
  96. Content = Content & "      
  97. ElseIf X = VBLF Then     
  98. Content = Content & "<br />    
  99. Else     
  100. Content = Content & X     
  101. End If     
  102. Else     
  103. If Flag = 0 Then     
  104. S = i     
  105. Flag = 1     
  106. End if     
  107. End If     
  108. Next     
  109. if Flag = 1 Then     
  110. Execute = Content & Mid(CodeContent, S)     
  111. Else     
  112. Execute = content     
  113. End If     
  114. End Function     
  115.     
  116. Private Function HtmlEnCode(Str)     
  117. If IsNull(Str) Then     
  118. HtmlEnCode = "": Exit Function     
  119. End if     
  120. Str = Replace(Str ,"","")     
  121. Str = Replace(Str ,"<","<")     
  122. Str = Replace(Str ,">",">")     
  123. Str = Replace(Str ,"""",""")      
  124. Str = Replace(Str ,Chr(9),"  ")      
  125. Str = Replace(Str ," "," ")      
  126. Str = Replace(Str ,VBLF,"<br />")      
  127. HtmlEnCode = Str      
  128. End Function     
  129.      
  130. Public Property Let Language(Str)      
  131. Dim S      
  132. S = UCase(Str)      
  133. Select Case true      
  134. Case S = "VB" Or S = "VBS" OR S = "VBSCRIPT":      
  135. 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"     
  136. Objects ="String,Number,Boolean,Date,Integert,Long,Double,Single"     
  137. SplitWords = ",.?!;:/<>(){}[]""'=+-|*%@#$^& "&VBCRLF&Chr(9)      
  138. LineComment = "'"     
  139. CommentOn = ""     
  140. CommentOff = ""     
  141. StrOn = """"     
  142. Escape = ""     
  143. Ignore = 1      
  144. CodeContent = ""     
  145. Tags = ""     
  146.      
  147. Case s = "C#":      
  148. 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" '关键字      
  149. Objects = "String,Boolean,DateTime,Int32,Int64,Exception,DataTable,DataReader" '对象      
  150. SplitWords = " ,.?!;:/<>(){}[]""'=+-|*%@#$^&"&VBCRLF&CHR(9) '分隔符      
  151. LineComment = "//" '行注释      
  152. CommentOn = "/*" '多行注释      
  153. CommentOff = "*/" '多行注释结束      
  154. Ignore = 0 '是否区分大小写      
  155. Tags = "" '标记      
  156. StrOn = """" '字符串标记      
  157. Escape = "" '字符串界定符转义      
  158.      
  159. Case S = "JAVA" :      
  160. 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" '关键字      
  161. Objects = "String,Boolean,DateTime,Int32,Int64,Exception,DataTable,DataReader" '对象      
  162. SplitWords = " ,.?!;:/<>(){}[]""'=+-|*%@#$^&"&VBCRLF&CHR(9) '分隔符      
  163. LineComment = "//" '行注释      
  164. CommentOn = "/*" '多行注释      
  165. CommentOff = "*/" '多行注释结束      
  166. Ignore = 0 '是否区分大小写      
  167. Tags = "" '标记      
  168. StrOn = """" '字符串标记      
  169. Escape = "" '字符串界定符转义      
  170.      
  171. Case S = "JS" OR S = "JSCRIPT" OR S = "JAVASCRIPT":      
  172. Keywords = "function,void,this,boolean,while,if,return,new,true,false,try,catch,throw,null,else,int,long,do,var" '关键字      
  173. Objects = "String,Number,Boolean,RegExp,Error,Math,Date" '对象      
  174. SplitWords = " ,.?!;:/<>(){}[]""'=+-|*%@#$^&"&VBCRLF&CHR(9) '分隔符      
  175. LineComment = "//" '行注释      
  176. CommentOn = "/*" '多行注释      
  177. CommentOff = "*/" '多行注释结束      
  178. Ignore = 0 '是否区分大小写      
  179. Tags = "" '标记      
  180. StrOn = """" '字符串标记      
  181. Escape = "" '字符串界定符转义      
  182.      
  183. Case S = "XML":      
  184. Keywords = "!DOCTYPE,?xml,script,version,encoding" '关键字      
  185. Objects = "String,Number,Boolean,RegExp,Error,Math,Date" '对象      
  186. SplitWords = " ,.?!;:/<>(){}[]""'=+-|*%@#$^&"&VBCRLF&CHR(9) '分隔符      
  187. LineComment = "//" '行注释      
  188. CommentOn = "<!--" '多行注释      
  189. CommentOff = "-->" '多行注释结束      
  190. Ignore = 0 '是否区分大小写      
  191. Tags = "" '标记      
  192. StrOn = """" '字符串标记      
  193. Escape = "" '字符串界定符转义      
  194.      
  195. Case S = "HTML":      
  196. Case S = "SQL":      
  197. 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" '关键字      
  198. Objects = "" '对象      
  199. SplitWords = " ,.?!;:\/<>(){}[]""'=+-|*%@#$^&"&VBCRLF&CHR(9) '分隔符      
  200. LineComment = "--" '行注释      
  201. CommentOn = "/*" '多行注释      
  202. CommentOff = "*/" '多行注释结束      
  203. Ignore = 1 '是否区分大小写      
  204. Tags = "" '标记      
  205. StrOn = "'" '字符串标记      
  206. Escape = "" '字符串界定符转义      
  207. End Select     
  208. End Property     
  209. End Class     
  210.      
  211. Function highlightcode(divid,codetype)      
  212. Dim code,HL      
  213. code = document.getElementById(divid).innerText;      
  214. 'code = Document.all.code.value      
  215. Set HL = New Highlight      
  216. HL.Language = codetype      
  217. HL.CodeContent = code      
  218. document.getElementById("highlight").innerHTML = HL.Execute      
  219. End Function   

 





发表评论
昵 称:
密 码:    游客发言不需要密码.
邮 箱:    支持Gravatar头像
网 址:
内 容:
您一共可以输入1000个字
选 项:
不想保留信息请删除cookie
发表评论后您发表的内容自动复制到了剪贴板
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭