14 Feb 2008
VBScript code to beautify VBScript code
Sounds redundant, but it’s a utility function you can use when NotePad, TextPad, Context or another text editor (e.g. textarea of a browser) is used for developing the script. I’ve used this function in a web page which allows building and testing small utility functions. In that page, once the code is submitted and executed, it returns the results and the beautified code. The script writer can stay focused on functionality–not format.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | Function BeautifyVBS (sSource, nTabSpacing) ' Takes VBScript source code and rebuilds the indentation. Dim sRawLine, sLine, sTest, iIndentIndex, iIndex, oS, sWhiteSpace, bAdjustIndent, bInQuote, aRows Dim aKey(34) Const INDENT_String = 0 Const INDENT_Exeception_String = 1 Const INDENT_Pre_Indent = 2 Const INDENT_Post_Indent = 3 ' The indent and unindent list is as complete as I could make it (from the MS VBScript reference). aKey (0) = ARRAY ("if ", " then", 0, 1) aKey (1) = ARRAY ("select ", "", 0, 2) aKey (2) = ARRAY ("sub ", "", 0, 1) aKey (3) = ARRAY ("function ", "", 0, 1) aKey (4) = ARRAY ("do ","", 0, 1) aKey (5) = ARRAY ("while ","", 0, 1) aKey (6) = ARRAY ("for ","", 0, 1) aKey (7) = ARRAY ("case ", "", -1, 1) aKey (8) = ARRAY ("with ","", 0, 1) aKey (9) = ARRAY ("class ","", 0, 1) aKey (10) = ARRAY ("public sub","", 0, 1) aKey (11) = ARRAY ("private sub ","", 0, 1) aKey (12) = ARRAY ("public function ","", 0, 1) aKey (13) = ARRAY ("private function ","", 0, 1) aKey (14) = ARRAY ("property get ","", 0, 1) aKey (15) = ARRAY ("public property get ","", 0, 1) aKey (16) = ARRAY ("private property get ","", 0, 1) aKey (17) = ARRAY ("property let ","", 0, 1) aKey (18) = ARRAY ("public property let ","", 0, 1) aKey (19) = ARRAY ("private property let ","", 0, 1) aKey (20) = ARRAY ("property set ","", 0, 1) aKey (21) = ARRAY ("public property set ","", 0, 1) aKey (22) = ARRAY ("private property set ","", 0, 1) aKey (23) = ARRAY ("else ","", -1, 1) aKey (24) = ARRAY ("elseif ","", -1, 1) aKey (25) = ARRAY ("end if", "", -1, 0) aKey (26) = ARRAY ("end select", "", -2, 0) aKey (27) = ARRAY ("end sub", "", -1, 0) aKey (28) = ARRAY ("end function", "", -1, 0) aKey (29) = ARRAY ("loop", "", -1, 0) aKey (30) = ARRAY ("wend", "", -1, 0) aKey (31) = ARRAY ("next", "", -1, 0) aKey (32) = ARRAY ("end class", "", -1, 0) aKey (33) = ARRAY ("end property", "", -1, 0) aKey (34) = ARRAY ("end with", "", -1, 0) sWhiteSpace = " " & vbTab Set oS = CreateObject("ADODB.Stream") oS.Type = 2 ' ASCII oS.Open iIndentIndex = 0 For Each sRawLine in Split (sSource, vbCrLf) ' Remove all whitespace on the left iIndex = 1 If Len (sRawLine) > 0 Then Do While iIndex <= Len (sRawLine) If Instr (sWhiteSpace, Mid (sRawLine, iIndex, 1)) = 0 Then Exit Do End If iIndex = iIndex + 1 Loop End If If iIndex > Len (sRawLine) Then sLine = "" Else sLine = Mid (sRawLine, iIndex) End If ' Remove all whitespace on the right iIndex = Len (sLine) Do While iIndex > 0 If Instr (sWhiteSpace, Mid (sLine, iIndex, 1)) = 0 Then Exit Do End If iIndex = iIndex - 1 Loop If iIndex < Len (sLine) Then sLine = Left (sLine, iIndex) End If sTest = LCase (LTrim (sLine)) ' Find any in-line comment marker, and truncate the comment if it exists. bInQuote = False For iIndex = 1 To Len (sTest) If Not bInQuote And Mid (sTest, iIndex, 1) = "'" Then Exit For End If If Mid (sTest, iIndex, 1) = """" Then bInQuote = Not bInQuote End If Next If iIndex < Len (sTest) Then ' Truncate comment sTest = Left (sTest, iIndex - 1) ' Truncate whitespace again iIndex = Len (sTest) Do While iIndex > 0 If Instr (sWhiteSpace, Mid (sTest, iIndex, 1)) = 0 Then Exit Do End If iIndex = iIndex - 1 Loop If iIndex < Len (sTest) Then sTest = Left (sTest, iIndex) End If End If sTest = LCase (LTrim (sTest)) & SPACE (32) ' Adjust Indentation as needed bAdjustIndent = False For iIndex = 0 To UBound (aKey, 1) If Left (sTest, LEN (aKey(iIndex)(INDENT_String))) = aKey(iIndex)(INDENT_String) Then If LEN(aKey(iIndex)(INDENT_Exeception_String)) = 0 Or Right (RTrim (sTest), LEN (aKey(iIndex)(INDENT_Exeception_String))) = aKey(iIndex)(INDENT_Exeception_String) Then bAdjustIndent = True Exit For End If End If Next If bAdjustIndent Then iIndentIndex = iIndentIndex + aKey(iIndex)(INDENT_Pre_Indent) If iIndentIndex < 0 Then iIndentIndex = 0 End If If nTabSpacing <= 0 Then oS.WriteText STRING (iIndentIndex, vbTab) & sLine & vbCrLf Else oS.WriteText SPACE (nTabSpacing * iIndentIndex) & sLine & vbCrLf End If If bAdjustIndent Then iIndentIndex = iIndentIndex + aKey(iIndex)(INDENT_Post_Indent) If iIndentIndex < 0 Then iIndentIndex = 0 End If Next oS.Position = 0 BeautifyVBS = oS.ReadText (-1) oS.Close() Set oS = Nothing End Function |
Leave a Comment
You must be logged in to post a comment.