ASP生成靜態頁(收1)

 下面的例子是將、index.asp?id=1/index.asp?id=2/index.asp?id=3/這三個動態頁面,分別生成ndex1.htm,index2.htm,index3.htm存在根目錄下面: <%
dim strUrl,Item_Classid,id,FileName,FilePath,Do_Url,Html_Temp
Html_Temp="<UL>"
For i=1 To 3
Html_Temp = Html_Temp&"<LI>"
Item_Classid = i
FileName = "Index"&Item_Classid&".htm"
FilePath = Server.MapPath("/")&"/"&FileName
Html_Temp = Html_Temp&FilePath&"</LI>"
Do_Url = "http://"
Do_Url = Do_Url&Request.ServerVariables("SERVER_NAME")&"/main/index.asp"
Do_Url = Do_Url&"?Item_Classid="&Item_Classid
strUrl = Do_Url
dim objXmlHttp
set objXmlHttp = Server.CreateObject("Microsoft.XMLHTTP")
objXmlHttp.open "GET",strUrl,false
objXmlHttp.send()
Dim binFileData
binFileData = objXmlHttp.responseBody
Dim objAdoStream
set objAdoStream = Server.CreateObject("ADODB.Stream")
objAdoStream.Type = 1
objAdoStream.Open()
objAdoStream.Write(binFileData)
objAdoStream.SaveToFile FilePath,2
objAdoStream.Close()
Next
Html_Temp = Html_Temp&"<UL>"
%>
<%
Response.Write ( "成功生成文件:" )
Response.Write ( "<BR>" )
Response.Write Html_Temp
%>
非摸板生成靜態頁 目前已經有很多生成html的新聞系統,但是都是用的模板,本函數實現把asp頁面產生的html代碼保存成爲一個html文件,這樣就沒有必要改動原來的頁面就可以輕鬆完成一個生成html的新聞系統了。^_^
由於代碼比較短,這裏就不進行註釋了

<%
'當目標頁面的包含文件即#include的頁面裏邊存在response.End()的時候本程序有問題
'注意:本文件一定要放在filename指向的文件的同一目錄下
dim hughchiu_rtcode
Function get_exe_code(filename)
dim execode
dim tmp_str
Dim re,re1,content,fso,f,aspStart,aspEnd
dim ms,m
execode = ""
set fso=CreateObject("Scripting.FileSystemObject")
set f=fso.OpenTextFile(server.mappath(filename))
content=f.ReadAll
f.close
set f=nothing
set fso=nothing set re = new regexp
re.ignorecase = true
re.global = true
re.pattern = "/</%/@[^/%]+/%/>"
content = re.replace(content,"") re.global = false
re.pattern = "/</!/-/-/s*/#include/s*file/s*=/s*/""([^/""]+)/""/s*/-/-/>"
do
set ms = re.execute(content)
if ms.count<>0 then
set m = ms(0)
tmp_str = get_exe_code(m.submatches(0))
content = re.replace(content, tmp_str)
else
exit do
end if
loop
set m = nothing
set ms = nothing re.pattern="^/s*="
aspEnd=1
aspStart=inStr(aspEnd,content,"<%")+2 set re1=new RegExp
re1.ignorecase = true
re1.global = false
re1.pattern = "response/.Write(.+)" do while aspStart>aspEnd+1
  execode = execode&vbcrlf&" hughchiu_rtcode = hughchiu_rtcode&"""&replace( replace(Mid(content,aspEnd,aspStart-aspEnd-2),"""",""""""), vbcrlf, """&vbcrlf&""")&""""&vbcrlf
  aspEnd=inStr(aspStart,content,"%/>")+2
  tmp_str = Mid(content,aspStart,aspEnd-aspStart-2)
  
do
set ms = re1.execute(tmp_str)
if ms.count<>0 then
set m = ms(0)
tmp_str = re1.replace(tmp_str, " hughchiu_rtcode = hughchiu_rtcode&"&m.submatches(0))
else
exit do
end if
loop
  
  set m = nothing
  set ms = nothing
  
  execode = execode& re.replace(tmp_str,"hughchiu_rtcode = hughchiu_rtcode&")   aspStart=inStr(aspEnd,content,"<%")+2
loop set re1 = nothing
set re=nothing execode = execode&vbcrlf&" hughchiu_rtcode = hughchiu_rtcode&"""&replace( replace(Mid(content,aspEnd), """", """"""), vbcrlf, """&vbcrlf&""" )&""""&vbcrlf
get_exe_code = "<%"&execode&"%/>"
End Function

function asp2html(filename)
dim code
code = replace( replace( replace( get_exe_code(filename), "hughchiu_rtcode = hughchiu_rtcode&"""""&vbcrlf, "" ), "<%", "" ), "%/>", "" )
'response.Write(code)
execute(code)
'response.Write( hughchiu_rtcode )
asp2html = hughchiu_rtcode
end function
%>

使用範例:
set fso=CreateObject("Scripting.FileSystemObject")
set f=fso.CreateTextFile( server.mappath( "youpage.htm" ), true )
f.WriteLine( asp2html("youpage.asp") )
f.close
set f = nothing
set fso = nothing 可見,雖然是新方法還是需要fso的支持
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章