生成语法高亮代码

把下面代码保存为HightLightCode.asp:

None.gif < html >
None.gif
< head >
None.gif
< title > 生成语法高亮代码 </ title >
None.gif
< meta http - equiv = " Content-Type "  content = " text/html; charset=gb2312 " >
None.gif
</ head >
None.gif
< body >
None.gif
< FORM name = form1 METHOD = POST action = "" >
None.gif
< TEXTAREA NAME = " Content "  ROWS = " 10 "  COLS = " 20 " >< % = Request( " Content " )% ></ TEXTAREA >
None.gif
< br >< br >< INPUT TYPE = " submit "  value = " 生成语法高亮代码 "  name = " make " >
None.gif
</ FORM >
None.gif
< input name = " Increase "  title = " 增大编辑框 "  type = " button "  value = "  +  "  onClick = " javascript:form1.Content.rows=form1.Content.rows+2;form1.Content.cols=form1.Content.cols+4; " >   < input name = " Decrease "  title = " 缩小编辑框 "  type = " button "  value = "  -  "  onClick = " javascript:if((form1.Content.rows>10)&&(form1.Content.cols>20)){ form1.Content.rows=form1.Content.rows-2;form1.Content.cols=form1.Content.cols-4} " >< Br >
None.gif
< %
None.gifClass Wyd_AspCodeHighLight
None.gif
Private  RegEx
None.gif
Public  Keyword,ObjectCommand,Strings,VBCode
None.gif
Public  KeyWordColor,ObjectCommandColor,StringsColor,Comment,CodeColor
None.gif  
Private   Sub  Class_Initialize()
None.gif    
Set  RegEx  =   New  RegExp
None.gifRegEx.IgnoreCase 
=   True     '  设置是否区分字母的大小写 True 不区分。
None.gif
    RegEx.Global  =   True     '  设置全程性质。
None.gif
    KeyWordColor = " #0000FF"
None.gif
    ObjectCommandColor = " #FF0000"
None.gif
    StringsColor = " #FF00FF"
None.gif
Comment = " #008000"
None.gif
CodeColor = " #993300"
None.gif
Keyword = " Set|Private|If|Then|Sub|End|Function|For|Next|Do|While|Wend|True|False|Nothing|Class "   ' 关建字 请自己添加
None.gif
ObjectCommand = " Left|Mid|Right|Int|Cint|Clng|String|Join|Array "   ' 函数 请自己添加
None.gif
VBCode = " "
None.gif
   End Sub
None.gif  
Private   Sub  Class_Terminate()
None.gif    
Set  RegEx  =   Nothing
None.gif  
End Sub
None.gif  
Private   Function  M_Replace(Str,Pattern,Color)
None.gif    RegEx.Pattern 
=  Pattern   '  设置模式。
None.gif
    M_Replace = RegEx. Replace (Str, " <font color= " & Color & " >$1</font> " )
None.gif  
End Function  
None.gif
None.gif
None.gif  
Private   Function  String_Replace(Str,Pattern,Pattern1,Color,IsString)
None.gif  
Dim  Temp,RetStr
None.gifRegEx.Pattern 
= Pattern1
None.gif    
Set  Matches  =  RegEx. Execute (Str)
None.gif    
For   Each  Match In Matches    '  遍历 Matches 集合
None.gif
       Temp = Re(Match.value)
None.gif       Str 
=   Replace (Str,Match.value,Temp)
None.gif    
Next
None.gifRegEx.Pattern 
=  Pattern   '  设置模式。
None.gif
If  IsString = 1   Then
None.gif       String_Replace
= RegEx. Replace (Str, " <font color= " & Color & " >&quot;$1&quot;</font> " )
None.gif
Else
None.gif    String_Replace
= RegEx. Replace (Str, " <font color= " & Color & " >$1</font> " )
None.gif
End   If
None.gif  
End Function
None.gif
None.gif
None.gif  
Private   Function  Re(Str)
None.gif   
Dim  TRegEx,Temp
None.gif   
Set  TRegEx  =   New  RegExp
None.gif   TRegEx.IgnoreCase 
=   True    '  设置是否区分字母的大小写。
None.gif
   TRegEx.Global  =   True     '  设置全程性质。
None.gif
   TRegEx.Pattern = " <.*?>"
None.gif
   Temp = TRegEx. Replace (Str, "" )
None.gif   Temp
= Replace (Temp, " < " , "" )
None.gif   Temp
= Replace (Temp, " > " , "" )
None.gif   Re
= Temp
None.gif   
Set  TRegEx = Nothing
None.gif  
End Function
None.gif  
None.gif  
Public   Function  MakeLi()
None.gif    
Dim  Temp
None.gif
If  VBCode = ""   Then
None.gif    MakeLi
= " "
None.gif
     Exit   Function
None.gif
End   If
None.gif    VBCode
= HTMLEncode(VBCode)
None.gif    Temp
= M_Replace(VBCode, " \b( " & Keyword & " )\b " ,KeyWordColor)
None.gif    Temp
= M_Replace(Temp, " \b( " & ObjEctCommand & " )\b " ,ObjectCommandColor)
None.gif    Temp
= String_Replace(Temp, " ""(.*?)"" " , " ""(.*)(<.+?>)( " & KeyWord & ObjectCommand & " )+(<.+?>)(.*)"" " ,StringsColor, 1 ) '  字符串
None.gif
    Temp = String_Replace(Temp, " (('|rem).*) " , " '(.*)(<.+?>)( " & KeyWord & ObjectCommand & " )+(<.+?>)(.*) " ,Comment, 0 ' 注释
None.gif
    MakeLi = " <FONT  COLOR= " & CodeColor & " > " & RepVbCrlf(Temp) & " </FONT>"
None.gif
   End Function
None.gif  
Public   Function  RepVbCrlf(fString)
None.gif     RepVbCrlf 
=   Replace (fString,  CHR ( 10 ),  " <BR>  " )
None.gif  
End Function
None.gif  
Public   Function  HTMLEncode(fString)
None.gif     
If   IsNull (fString)  Or  fString = ""   Then
None.gif     HTMLEncode
= " "
None.gif
   Exit   Function
None.gif     
End   If
None.gif     fString 
=   replace (fString,  " > " " &gt; " )
None.gif     fString 
=   replace (fString,  " < " " &lt; " )
None.gif     
' fString = Replace(fString, CHR(32), "&nbsp;")
None.gif
      ' fString = Replace(fString, CHR(9), "&nbsp;")
None.gif
      ' fString = Replace(fString, CHR(34), "&quot;")
None.gif
      ' fString = Replace(fString, CHR(39), "&#39;")
None.gif
      ' fString = Replace(fString, CHR(13), "")
None.gif
      ' fString = Replace(fString, CHR(10) & CHR(10), "</P><P> ")
None.gif
      ' fString = Replace(fString, CHR(10), "<BR> ")
None.gif
     HTMLEncode  =  fString
None.gif   
End Function
None.gif
End  Class
None.gif
None.gifstar
= timer ()
None.gif
Set  TT  =   New  Wyd_AspCodeHighLight
None.gif
If  Request( " Content " ) <> ""   Then
None.gif  TT.VBCode
= Request( " Content " )
None.gif  Response.write TT.MakeLi()
None.gif  REsponse.write 
" <br>耗时: " & FormatNumber ( timer () - star, 2 ) * 1000
None.gif
End   If % >
None.gif
</ body >
None.gif
</ html >

转载于:https://www.cnblogs.com/Dicky/archive/2005/03/17/122531.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值