Prøv at smide denne i en vbs-fil og køre den fra kommandolinjen:
option explicit
const tabs = 3
dim fso, f
dim code, firstPos, codePos, codeLen, codeBlockEnd
dim pretext
set fso = CreateObject("Scripting.FileSystemObject")
set f = fso.OpenTextFile(wscript.Arguments(0))
code = f.ReadAll()
f.close
firstPos = 1
codepos = instr(firstPos, code, "<%")
codelen = len(code)
do while codepos < codelen and codepos > 0
pretext = mid(code, firstPos, codepos - firstPos)
codeBlockEnd = instr(codepos+2, code, "%>")
if len(pretext) > 0 then
wscript.echo trim(pretext)
end if
wscript.echo beautifyASP(trim(mid(code, codepos, codeBlockEnd-codePos+2)))
if codeBlockEnd > 0 then
firstPos = codeBlockEnd + 2
codepos = instr(firstPos, code, "<%")
else
codepos = 0
end if
loop
if len(code) > firstPos then
wscript.echo trim(mid(code,firstPos))
end if
function beautifyAsp(s)
dim res: res = ""
dim line, lines, indent
dim re, mc, m, v, idx
set re = new RegExp
re.Global = true
re.IgnoreCase = true
re.Pattern = "([^\:""]*(""[^""]*"")*?)+(\:|$)"
lines = split(s, vbCrLf)
indent = 0
for each line in lines
set mc = re.Execute(line)
if mc.Count > 0 then
idx = 0
for each m in mc
v = m.Value
if right(v, 1) = ":" then v = mid(v, 1, len(v)-1)
if v = "" and idx > 0 then exit for
indent = getIndentBefore(v, indent)
wscript.echo string(indent, " ") & trim(v)
indent = getIndentAfter(v, indent)
idx = idx + 1
next
end if
next
beautifyAsp = res
end function
function getIndentAfter(ln, byval indent)
dim mc
set mc = getFirstWord(ln)
getIndentAfter = indent
if mc.Count > 0 then
select case mc.item(0).Value
case "while", "if", "function", "sub", "do", "for", _
"select", "case", "else", "elseif", "with", _
"class", "public", "private"
getIndentAfter = indent + tabs
end select
end if
end function
function getIndentBefore(ln, byval indent)
dim mc
set mc = getFirstWord(ln)
getIndentBefore = indent
if mc.Count > 0 then
select case mc.item(0).Value
case "end", "loop", "wend", "next", "elseif", "else"
getIndentBefore = indent - tabs
end select
end if
end function
function getFirstWord(ln)
dim re
set re = new RegExp
re.Global = true
re.IgnoreCase = true
re.Pattern = "^[a-z][a-z0-9_]*\b"
set getFirstWord = re.execute(trim(ln))
end function
Du kan evt. omdirigere den til en ny fil med flg. kommando:
cscript beautify.vbs gammel-asp-fil.asp > ny-asp-fil.asp
Scriptet er råt og upoleret, så det skal nok mest opfattes som et udgangspunkt for videre arbejde. Hvis du kan bruge det, er det jo fint...