欢迎光临
我们一直在努力

VBS、ASP代码语法加亮显示的类

建站超值云服务器,限时71元/月

<% class cbuffer
    private objfso, objfile, objdict
    private m_strpathtofile, m_tablebgcolor, m_starttime
    private m_endtime, m_linecount, m_intkeymin, m_intkeymax
    private m_codecolor, m_commentcolor, m_stringcolor, m_tabspaces
    
    private sub class_initialize()
        tablebgcolor = "white"
        codecolor = "blue"
        commentcolor = "green"
        stringcolor = "gray"
        tabspaces = "    "
        pathtofile = ""
        
        m_starttime = 0
        m_endtime = 0
        m_linecount = 0
        
        keymin = 2
        keymax = 8
        
        set objdict = server.createobject("scripting.dictionary")
        objdict.comparemode = 1
        
        createkeywords
        
        set objfso = server.createobject("scripting.filesystemobject")
    end sub

    private sub class_terminate()
        set objdict = nothing
        set objfso = nothing
    end sub
    
   
    public property let codecolor(incolor)
        m_codecolor = "<font color=" & incolor & "><strong>"
    end property
    private property get codecolor()
        codecolor = m_codecolor
    end property

    public property let commentcolor(incolor)
        m_commentcolor = "<font color=" & incolor & ">"
    end property
    private property get commentcolor()
        commentcolor = m_commentcolor
    end property

    public property let stringcolor(incolor)
        m_stringcolor = "<font color=" & incolor & ">"
    end property
    private property get stringcolor()
        stringcolor = m_stringcolor
    end property

    public property let tabspaces(inspaces)
        m_tabspaces = inspaces
    end property
    private property get tabspaces()
        tabspaces = m_tabspaces
    end property

    public property let tablebgcolor(incolor)
        m_tablebgcolor = incolor
    end property

    private property get tablebgcolor()
        tablebgcolor = m_tablebgcolor
    end property

    public property get processingtime()
        processingtime = second(m_endtime – m_starttime)
    end property

    public property get linecount()
        linecount = m_linecount
    end property

    public property get pathtofile()
        pathtofile = m_strpathtofile
    end property
    public property let pathtofile(inpath)
        m_strpathtofile = inpath
    end property

    private property let keymin(inmin)
        m_intkeymin = inmin
    end property
    private property get keymin()
        keymin = m_intkeymin
    end property
    private property let keymax(inmax)
        m_intkeymax = inmax
    end property
    private property get keymax()
        keymax = m_intkeymax
    end property

    private sub createkeywords()
        objdict.add "abs", "abs"
        objdict.add "and", "and"
        objdict.add "array", "array"
        objdict.add "call", "call"
        objdict.add "cbool", "cbool"
        objdict.add "cbyte", "cbyte"
        objdict.add "ccur", "ccur"
        objdict.add "cdate", "cdate"
        objdict.add "cdbl", "cdbl"
        objdict.add "cint", "cint"
        objdict.add "class", "class"
        objdict.add "clng", "clng"
        objdict.add "const", "const"
        objdict.add "csng", "csng"
        objdict.add "cstr", "cstr"
        objdict.add "date", "date"
        objdict.add "dim", "dim"
        objdict.add "do", "do"
        objdict.add "loop", "loop"
        objdict.add "empty", "empty"
        objdict.add "eqv", "eqv"
        objdict.add "erase", "erase"
        objdict.add "exit", "exit"
        objdict.add "false", "false"
        objdict.add "fix", "fix"
        objdict.add "for", "for"
        objdict.add "next", "next"
        objdict.add "each", "each"
        objdict.add "function", "function"
        objdict.add "global", "global"
        objdict.add "if", "if"
        objdict.add "then", "then"
        objdict.add "else", "else"
        objdict.add "elseif", "elseif"
        objdict.add "imp", "imp"
        objdict.add "int", "int"
        objdict.add "is", "is"
        objdict.add "lbound", "lbound"
        objdict.add "len", "len"
        objdict.add "mod", "mod"
        objdict.add "new", "new"
        objdict.add "not", "not"
        objdict.add "nothing", "nothing"
        objdict.add "null", "null"
        objdict.add "on", "on"
        objdict.add "error", "error"
        objdict.add "resume", "resume"
        objdict.add "option", "option"
        objdict.add "explicit", "explicit"
        objdict.add "or", "or"
        objdict.add "private", "private"
        objdict.add "property", "property"
        objdict.add "get", "get"
        objdict.add "let", "let"
        objdict.add "set", "set"
        objdict.add "public", "public"
        objdict.add "redim", "redim"
        objdict.add "select", "select"
        objdict.add "case", "case"
        objdict.add "end", "end"
        objdict.add "sgn", "sgn"
        objdict.add "string", "string"
        objdict.add "sub", "sub"
        objdict.add "true", "true"
        objdict.add "ubound", "ubound"
        objdict.add "while", "while"
        objdict.add "wend", "wend"
        objdict.add "with", "with"
        objdict.add "xor", "xor"
    end sub

    private function min(x, y)
        dim tempmin
        if x < y then tempmin = x else tempmin = y
        min = tempmin
    end function
    
    private function max(x, y)
        dim tempmax
        if x > y then tempmax = x else tempmax = y
        max = tempmax
    end function
    
    public sub addkeyword(inkeyword, intoken)
        keymin = min(len(inkeyword), keymin)
        keymax = max(len(inkeyword), keymax)
        
        objdict.add lcase(inkeyword), intoken
    end sub
    
    public sub parsefile(blnoutputhtml)
        dim m_strreadline, tempstring, blninscriptblock, blngoodextension, i
        dim blnemptyline
        
        m_linecount = 0
        
        if len(pathtofile) = 0 then
            err.raise 5, "cbuffer: pathtofile length zero"
            exit sub
        end if
        
        select case lcase(right(pathtofile, 3))
            case "asp", "inc"
                blngoodextension = true
            case else
                blngoodextension = false
        end select
        
        if not blngoodextension then
            err.raise 5, "cbuffer: file extension not asp or inc"
            exit sub
        end if
        
        set objfile = objfso.opentextfile(server.mappath(pathtofile))
        
        response.write "<table nowrap bgcolor=" & tablebgcolor & " cellpadding=0 cellspacing=0>"
        response.write "<tr><td><pre>"
        
        m_starttime = time()
        
        do while not objfile.atendofstream
            m_strreadline = objfile.readline
            
            blnemptyline = false
            if len(m_strreadline) = 0 then
                blnemptyline = true
            end if
            
            m_strreadline = replace(m_strreadline, vbtab, tabspaces)
            m_linecount = m_linecount + 1
            tempstring = ltrim(m_strreadline)
            
             check for the top script line that sets the default script language
             for the page.
            if left( tempstring, 3 ) = chr(60) & "%@" and right(tempstring, 2) = "%" & chr(62) then
                response.write "<table><tr bgcolor=yellow><td>"
                response.write server.htmlencode(m_strreadline)
                response.write "</td></tr></table>"
                blninscriptblock = false
             check for an opening script tag
            elseif left( tempstring, 2) = chr(60) & "%" then
                 check for a closing script tag on the same line
                if right( rtrim(tempstring), 2 ) = "%" & chr(62) then
                    response.write "<table><tr><td bgcolor=yellow><%</td>"
                    response.write "<td>"
                    response.write characterparse(mid(m_strreadline, 3, len(m_strreadline) – 4))
                    response.write "</td>"
                    response.write "<td bgcolor=yellow>%gt;</td></tr></table>"
                    blninscriptblock = false
                else
                    response.write "<table><tr bgcolor=yellow><td><%</td></tr></table>"
                     weve got an opening script tag so set the flag to true so
                     that we know to start parsing the lines for keywords/comments
                    blninscriptblock = true
                end if
            else
                if blninscriptblock then
                    if blnemptyline then
                        response.write vbcrlf
                    else
                        if right(tempstring, 2) = "%" & chr(62) then
                            response.write "<table><tr bgcolor=yellow><td>%></td></tr></table>"
                            blninscriptblock = false
                        else
                            response.write characterparse(m_strreadline) & vbcrlf
                        end if
                    end if
                else
                    if blnoutputhtml then
                        if blnemptyline then
                            response.write vbcrlf
                        else
                            response.write server.htmlencode(m_strreadline) & vbcrlf
                        end if
                    end if
                end if
            end if
        loop
        
         grab the time at the completion of processing
        m_endtime = time()
        
         close the outside table
        response.write "</pre></td></tr></table>"
        
         close the file and destroy the file object
        objfile.close
        set objfile = nothing
    end sub
    
     this function parses a line character by character
    private function characterparse(inline)
        dim charbuffer, tempchar, i, outputstring
        dim insidestring, workstring, holdchar
        
        insidestring = false
        outputstring = ""
        
        for i = 1 to len(inline)
            tempchar = mid(inline, i, 1)
            select case tempchar
                case " "
                    if not insidestring then
                        charbuffer = charbuffer & " "
                        if charbuffer <>" "  then
                            if left(charbuffer, 1) = " " then outputstring = outputstring & " "
                            
                             check for a rem style comment marker
                            if lcase(trim(charbuffer)) = "rem" then
                                outputstring = outputstring & commentcolor
                                outputstring = outputstring & "rem"
                                workstring = mid( inline, i, len(inline))
                                workstring = replace(workstring, "<", "&lt;")
                                workstring = replace(workstring, ">", "&gt;")
                                outputstring = outputstring & workstring & "</font>"
                                charbuffer = ""
                                exit for
                            end if
                            
                            outputstring = outputstring & findreplace(trim(charbuffer))
                            if right(charbuffer, 1) = " " then outputstring = outputstring & " "
                            charbuffer = ""
                        end if
                    else
                        outputstring = outputstring & " "
                    end if
                case "("
                    if left(charbuffer, 1) = " " then
                        outputstring = outputstring & " "
                    end if
                    outputstring = outputstring & findreplace(trim(charbuffer)) & "("
                    charbuffer = ""
                case chr(60)
                    outputstring = outputstring & "<"
                case chr(62)
                    outputstring = outputstring & ">"
                case chr(34)
                     catch quote chars and flip a boolean variable to denote that
                     whether or not were "inside" a quoted string
                    insidestring = not insidestring
                    if insidestring then
                        outputstring = outputstring & stringcolor
                        outputstring = outputstring & "&quot;"
                    else
                        outputstring = outputstring & """"
                        outputstring = outputstring & "</font>"
                    end if
                case ""
                     catch comments and output the rest of the line
                     as a comment if were not inside a string.
                    if not insidestring then
                        outputstring = outputstring & commentcolor
                        workstring = mid( inline, i, len(inline))
                        workstring = replace(workstring, "<", "&lt;")
                        workstring = replace(workstring, ">", "&gt;")
                        outputstring = outputstring & workstring
                        outputstring = outputstring & "</font>"
                        exit for
                    else
                        outputstring = outputstring & ""
                    end if
                case else
                     weve dealt with special case characters so now
                     well begin adding characters to our outputstring
                     or charbuffer depending on the state of the insidestring
                     boolean variable
                    if insidestring then
                        outputstring = outputstring & tempchar
                    else
                        charbuffer = charbuffer & tempchar
                    end if
            end select
        next
        
         deal with the last part of the string in the character buffer
        if left(charbuffer, 1) = " " then
            outputstring = outputstring & " "
        end if
         check for closing parentheses at the end of a string
        if right(charbuffer, 1) = ")" then
            charbuffer = left(charbuffer, len(charbuffer) – 1)
            characterparse = outputstring & findreplace(trim(charbuffer)) & ")"
            exit function
        end if
        
        characterparse = outputstring & findreplace(trim(charbuffer))
    end function
    
     return true or false if a passed in number is between keymin and keymax
    private function inrange(inlen)
        if inlen >= keymin and inlen <= keymax then
            inrange = true
            exit function
        end if
        inrange = false
    end function
    
     evaluate the passed in string and see if its a keyword in the
     dictionary. if it is we will add html formatting to the string
     and return it to the caller. otherwise just return the same
     string as was passed in.
    private function findreplace(intoken)
         check the length to make sure its within the range of keymin and keymax
        if inrange(len(intoken)) then
            if objdict.exists(intoken) then
                findreplace = codecolor & objdict.item(intoken) & "</strong></font>"
                exit function
            end if
        end if
         keyword is either too short or too long or doesnt exist in the
         dictionary so well just return what was passed in to the function
        findreplace = intoken
    end function
    
end class
%>

使用前把里面的全角字符转换成半角的 

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » VBS、ASP代码语法加亮显示的类
分享到: 更多 (0)

相关推荐

  • 暂无文章