萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> ASP 程式設計

ASP 動態生成靜態 HTML 新思路:解析 ASP 原始碼並轉換為可執行腳本

核心思想

傳統靜態化需建立模板、標記插入點。本方案直接解析現有 ASP 頁面原始碼,將 `<%=...%>`、`<%...%>` 區塊轉換為字串拼接代碼,再用 Execute 執行並捕獲輸出,無需修改原頁面邏輯。

完整實作:asp2html.asp

<%@ 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 檔案

限制與注意事項

  • Execute 安全性:被解析頁面不可包含使用者輸入直接執行的代碼
  • Response.End/Redirect:會中斷執行,需在被解析頁面避免或改用條件判斷
  • Session/Application/Request:Execute 共用當前上下文,可正常存取
  • 效能:每次生成需解析+執行,大頁面較慢,建議離線批次生成
  • 編碼:輸出檔案用 `CreateTextFile(..., True, True)` 寫入 UTF-8

適用場景

  • 舊專案快速靜態化,不想重寫模板
  • 內容頁、列表頁、Sitemap 定時生成
  • 開發環境測試、預覽靜態輸出
copyright © 萬盛學電腦網 all rights reserved