傳統靜態化需建立模板、標記插入點。本方案直接解析現有 ASP 頁面原始碼,將 `<%=...%>`、`<%...%>` 區塊轉換為字串拼接代碼,再用 Execute 執行並捕獲輸出,無需修改原頁面邏輯。
<%@ Language=VBScript CodePage=65001 %>
<%
' === 遞迴處理 #include 指令 ===
Function GetExecCode(virtualPath)
Dim fso, f, content, re, ms, m, tmp, execCode
Dim aspStart, aspEnd, reWrite
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(Server.MapPath(virtualPath))
content = f.ReadAll
f.Close: Set f = Nothing: Set fso = Nothing
' 移除 <%@ ... %> 指令
Set re = New RegExp
re.Global = True: re.IgnoreCase = True
re.Pattern = "<%@[^%]*%>"
content = re.Replace(content, "")
' 遞迴展開 #include
re.Global = False
re.Pattern = ""
Do
Set ms = re.Execute(content)
If ms.Count = 0 Then Exit Do
Set m = ms(0)
tmp = GetExecCode(m.SubMatches(0))
content = re.Replace(content, tmp)
Loop
Set m = Nothing: Set ms = Nothing
' 將 HTML 文字轉為 Response.Write 陳述式
' 將 <% ... %> 區塊保留為代碼
execCode = ""
aspEnd = 1
aspStart = InStr(aspEnd, content, "<%") + 1 ' +1 因為 InStr 從 1 開始
Set reWrite = New RegExp
reWrite.Global = False: reWrite.IgnoreCase = True
reWrite.Pattern = "Response\.Write\s*\((.+)\)"
Do While aspStart > aspEnd
' HTML 文字區段 → hughchiu_rtcode = hughchiu_rtcode & "文字"
Dim htmlPart : htmlPart = Mid(content, aspEnd, aspStart - aspEnd - 2)
htmlPart = Replace(htmlPart, """", """""")
htmlPart = Replace(htmlPart, vbCrLf, """" & vbCrLf & """")
execCode = execCode & vbCrLf & " hughchiu_rtcode = hughchiu_rtcode & """ & htmlPart & """" & vbCrLf
aspEnd = InStr(aspStart, content, "%>") + 1
If aspEnd = 1 Then aspEnd = Len(content) + 2 ' 找不到結尾
' ASP 代碼區段
Dim codePart : codePart = Mid(content, aspStart, aspEnd - aspStart - 2)
' 將 Response.Write(xxx) 轉為 hughchiu_rtcode = hughchiu_rtcode & xxx
Do
Set ms = reWrite.Execute(codePart)
If ms.Count = 0 Then Exit Do
Set m = ms(0)
codePart = reWrite.Replace(codePart, " hughchiu_rtcode = hughchiu_rtcode & " & m.SubMatches(0))
Loop
execCode = execCode & vbCrLf & reWrite.Replace(codePart, " hughchiu_rtcode = hughchiu_rtcode & ") & vbCrLf
aspStart = InStr(aspEnd, content, "<%") + 1
Loop
' 尾部 HTML
Dim tailHtml : tailHtml = Mid(content, aspEnd)
tailHtml = Replace(tailHtml, """", """""")
tailHtml = Replace(tailHtml, vbCrLf, """" & vbCrLf & """")
execCode = execCode & vbCrLf & " hughchiu_rtcode = hughchiu_rtcode & """ & tailHtml & """" & vbCrLf
GetExecCode = "<%" & execCode & "%>"
End Function
' === 主入口 ===
Function ASP2HTML(virtualPath)
Dim code
code = GetExecCode(virtualPath)
' 移除輔助變數初始化行
code = Replace(code, " hughchiu_rtcode = hughchiu_rtcode & """" & vbCrLf, "")
code = Replace(code, "<%", ""): code = Replace(code, "%>", "")
Dim hughchiu_rtcode : hughchiu_rtcode = ""
On Error Resume Next
Execute code
If Err.Number <> 0 Then
ASP2HTML = ""
Err.Clear
Else
ASP2HTML = hughchiu_rtcode
End If
On Error GoTo 0
End Function
%>
<%@ Language=VBScript CodePage=65001 %>
<!--#include file="asp2html.asp"-->
<%
Dim html, fso, f
html = ASP2HTML("news_detail.asp?id=123")
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.CreateTextFile(Server.MapPath("static/news_123.html"), True, True) ' Unicode
f.Write html
f.Close
Set f = Nothing: Set fso = Nothing
Response.Write "靜態頁生成完成: static/news_123.html"
%>
原始 ASP (news.asp)
│
▼
GetExecCode() 解析
│
├─ 移除 <%@ ... %>
├─ 遞迴展開 #include
├─ HTML 文字 → hughchiu_rtcode = hughchiu_rtcode & "文字"
├─ <% code %> → 保留原代碼
└─ Response.Write(x) → hughchiu_rtcode = hughchiu_rtcode & x
│
▼
產生純 VBScript 字串 (含 hughchiu_rtcode 累加)
│
▼
Execute 執行 → hughchiu_rtcode 變數持有完整 HTML
│
▼
寫入 .html 檔案