將下面的四個文件存在同一級目錄下,再在同目錄下建立一個文件txt.txt。當要處理html代碼時,先將源代碼拷入txt.txt,再進入index_transform.asp, 即可看到處理完的代碼。
寫這個東西的本意是因為:經常要對美工用切圖軟件生成的網頁文件轉換成xsl,很頭疼要花大量的時間去改寫不規范的html代碼。 這個東西對全文所有的html代碼進行改動: 1.把所有標記都變成小寫; 2.把標簽的屬性值都加上雙引號; 3.把單端標簽<hr>、<img……>、<input……>等,改成<hr/>……; 4.把單獨屬性selected變成:selected="selected";
功能不完善之處:對html代碼中,屬性值內包含空格的情況不能正常處理; 對<script>、<style>標簽里的不能正常處理。 因為是以空格為標志將標簽里的各個屬性值split成的數組,所以對屬性值中 包含空格的還沒做進一步處理。
OK,耽誤大家時間了,看看這個東西能派上用場嗎? 圣誕快樂~! :)
================================================== ================================================== '文件1:transform.asp◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎ <% '***************************************** 'Author:小乙 '時間:2000.12.20 '功能:初步完成對要被轉換成XSL文件的:普通html代碼語法規范化的功能 '運行環境:可運行asp的機子。在同級目錄下把要處理的html代碼copy到 'txt.txt文件里。 '*************************************** '================================================================================================ ''''''''''''''''''''''''''''''''【對全文所有html源代碼進行語法規范化】'''''''''''''''''''''''''''' '在這個函數里,調用了另外一個主要函數alone_tag,來處理從中摘出來的單個標簽。 Function transform(txt) dim alltmp '定義此字符串變量,隨著被處理的大字符串減少而減短——只保留未處理的字符串部分。 alltmp=txt alltmp=replace(alltmp," "," ") 'nbsp_tmp是替換掉文本中的字符實體&#nbsp; '□■■■■■——進入全文的處理htm源代碼的大處理循環——■■■■■□ do while trim(alltmp)<>"" 'msgbox alltmp index=0 index=InStr(1,alltmp,"<",1)
'根據index的值,判斷"<"前面是否有文本?有:加到txt1;無:進行標簽處理(index=1)——即進入標簽處理分支 if index=1 then index_right=InStr(1,alltmp,">",1) tag=left(alltmp,index_right) '取出alltmp臨時串中">"前面的字符串 '對到這里的標簽,判斷如果標簽不是后端標簽,就調用處理標簽大函數alone_tag if mid(tag,2,1)<>"/" then tag1=alone_tag(tag) 'tag1=tag+",,,,,|" txt1=txt1+tag1 del_tag=len(tag) else '否則對其它標簽,就轉為小寫后,簡單的加在txt1后面 txt1=txt1+LCase(tag) del_tag=len(tag) end if else if index>1 then str_tmp=left(alltmp,index-1) txt1=txt1+str_tmp 'index<>1,說明前面有文本。 del_tag=len(left(alltmp,index-1)) '把"<"前面的屬于文本的添加到新txt1大字符串中去。 end if if index=0 then '當再也找不到<時(到了末尾),把剩下的字符串全部加到txt1里,結束循環。 txt1=txt1+alltmp del_tag=len(alltmp) end if end if
'把處理完的部分從原字符串中減掉 'response.write "alltmp="+alltmp
alltmp=right(alltmp,len(alltmp)-del_tag) '(如果標簽長大于等于2個字符)這里有問題!12.14,下次再作!!
loop ''□■■■■■——離開全文的處理htm源代碼的大處理循環——■■■■■□
'transform=txt1 txt1=replace(txt1," ="""" "," ") '【這句是對付=""漏網之魚 2000.12.15 txt1=replace(txt1," >",">") '【這句是對付 > 2000.12.19 txt1=replace(txt1,"<tbody>","") '【這句是對付<tbody> 2000.12.19 transform=replace(txt1,"</tbody>","") '【這句是對付</tbody> 2000.12.19
End Function ''''''''''''''''''''''''''''''''【對全文所有html源代碼進行語法規范化,結束】'''''''''''''''''''''''' %>
================================================== ================================================== '文件2:index_transform.asp◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎ <%@ Language="VBScript" %> <!-- #include file="transform.asp" --> <!-- #include file="alone_tag.asp" --> <%
'-------------------------------------本部分得到全文源代碼,開始------------------------------------ dim txt '源文件中的文本 dim txt1 '經過html語法規范化后的文件字符串。 dim tmpreadline '=thisfile.readline txt="":txt1="":tmpReadAll="" '取得源文件名稱,及所在路徑------------- sourcefile="txt.txt" sourcefile=Request.form("txtname")
'--------------------------新增部分,獲得上傳文本文件的內容------------2000.12.15 'txt=request.form("filecontent") 'if len(txt)<>"" then 'response.write "---------------" 'end if 'response.end '--------------------------新增部分結束------------2000.12.15
'-----------------------------------------------------【正式開始操作文件】---------------------- whichfile=server.mappath("txt.txt") 'whichfile=server.mappath(sourcefile) Set fs = CreateObject("Scripting.FileSystemObject") Set thisfile = fs.OpenTextFile(whichfile, 1, False) counter=0
tmpReadAll=thisfile.readall 'ReadAll是讀取全部文件內容。 txt=tmpReadAll txt1=transform(cstr(tmpReadAll)) txt=server.htmlencode(txt)+" 【文件內容到此結束】"
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'如果要看打印出來長字符串的效果,請取消上面這行注釋。 '-------------------------------------本部分得到全文源代碼,結束------------------------------------ %>
<%''''''''''''''''''''''''''''''【這里正式html頁面開始】'''''''''''''''''''''''''''''''''''''''''%>
<html> <head> <title>倒數第二版</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head>
<body bgcolor="#BFA49a"> <form method="post" action="index_transform.asp" name="form1"> <table border="1" cellspacing="0" cellpadding="5" align="center" bgcolor="#99CCCC" bordercolor="#330066"> <tr> <td> <input type="text" name="txtname" value="txt.txt"> <input type="file" name="filecontent"> <input type="submit" name="Submit" value="提交"> <a href="#pagedown">到下面</a> </td> </tr> </table> </form> <br> <!-------------------頁面表單2開始(form2)---------------------> <form method="post" action="trans2.asp" name="form2" enctype="multipart/form-data"> <table width="753" border="1" cellspacing="0" cellpadding="5" align="center" bgcolor="#99CCCC" bordercolor="#330066"> <tr bgcolor="#98AFE7" bordercolor="#FFCC99"> <td bordercolor="#FFFFFF">原文:</td> <td bordercolor="#FFFFFF">處理后:</td> </tr> <tr> <td> <textarea name="txt" cols="50" rows="25" onFocus="this.select()" onclick="this.focus()"> <%=txt%> </textarea> </td> <td> <%''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''%> <textarea name="txt" cols="50" rows="25" onFocus="this.select()" onclick="this.focus()"> <%=txt1%> </textarea> </td> </tr> </table> <div id="Layer1" style="position:absolute; width:68px; height:35px; z-index:1; left: 349px; top: 411px; background-color: #E7E7E9; layer-background-color: #E7E7E9; border: 1px none #000000"> <div align="center"> <input type="submit" name="Submit2" value="提交"> <INPUT TYPE=button NAME="view" VALUE="看源碼" OnClick='window.location="view-source:" +window.location.href'> </div> </div> </form>
<p> </p>
<br><a name="pagedown"> <hr size="1" align="center" width="90%" color="#88ff99"> <!-------以下是處理完的源代碼----------->
<%=txt1%>
<!-------處理完的源代碼到此為止-------> </body> </html>
<%'釋放資源 Erase strtag1 Erase strtag2 Erase strtag3
thisfile.Close set thisfile=nothing set fs=nothing %> ================================================== ================================================== '文件3:alone_tag.asp◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎ <!-- #include file="func_flag.asp" -->
<% 'tag="<hr bgcolor=""#ccFFFF"" size=4568481,dfafd selected>"
'----------------------------進入這里時應該已經得到一個完整的標簽-------------------------------- '--------在此建立了一個大函數用來處理一個完整的標簽里的屬性值。一直到頁面尾部為止。 function alone_tag(tag) dim tag1 '定義處理完以后的標簽,并在本函數末尾,將此值返還給alone_tag。 tag=LCase(tag) '將標簽命名為tag,并且為了美觀,把所有字符串都改寫成小寫形式
'---------到此先準備好標簽的自身,以備后面拼回標簽的時候使用(減一是因為后面拼屬性時把空格加上了) dim tmpattri '此變量是臨時字符串,包含一個標簽中的所有屬性值,利用它來求“屬性數組:attribute” index=InStr(1,tag," ",1) tmpattri=right(tag,len(tag)-index) '除去左側標簽 if len(tmpattri)>1 then tmpattri=trim(left(tmpattri,len(tmpattri)-1)) '除去右側">",并去除兩端空格(如果標簽長大于等于2個字符) end if
tmpattri=replace(tmpattri,chr(13)," ") '對源碼中,一個標簽不在一行里的情況,尚等待另行考慮處理。 tmpattri=replace(tmpattri,chr(10)," ") tmpattri=replace(tmpattri,chr(10)&chr(13)," ") tmpattri=replace(tmpattri," "," ") '【這兩句是對付當屬性串里有多個空格的時候, tmpattri=replace(tmpattri," "," ") '【替換成一個空格,不過只能處理不超過16個空格的情況。 tmpattri=replace(tmpattri," "," ") tmpattri=replace(tmpattri," "," ") tmpattri=replace(tmpattri," "," ") tmpattri=replace(tmpattri," "," ") tmpattri=replace(tmpattri," "," ") attribute=Split(tmpattri, " ", -1, 1) '新定義一個數組,是專為裝載屬性值而設的。Dim attribute 'msgbox "這里得到準備拆分屬性數組的長字符串: "+tmpattri
'--------------------到這里已經得到一個關于屬性值的數組:attribute-------------------------------
'--------『這個循環是處理上面處理完畢的屬性值字符數組(attribute)的』------------------- 'flag=0:說明單個屬性有等于號,且有雙引號——語法正常,后面對此標志忽略。 (例:width="325") 'flag=1:說明單個屬性有等于號,且沒有雙引號——需要處理,對語法規范化 (例:width=325) 'flag=2:說明單個屬性沒有等于號(例:selected) 'flag=3:說明是單端標簽,(例:<hr width="80%" size="1">)此語句在前面設標志,并進行處理 For count=0 to UBound(attribute, 1) '一個元素的屬性不多. If InStr(1,attribute(count),"=",1)=0 Then flag=2 '單個屬性串中沒找到等于號。(例:selected) Else IF InStr(1,attribute(count),"""",1)=0 Then flag=1 '單個屬性串中沒找到等于號。(例:width=325) Else flag=0 '單個屬性串找到了等于號。(例:width="325") IF InStr(1,attribute(count),"""",1)>0 Then 'attribute(count)=attribute(count)+attribute(count+1) 'attribute(count+1)="" '這兩句是說,把下一個屬性串的賦給它,把下一個屬性串置為零。 flag=4 '單個屬性串找到了等于號,并且包含分號。(例:content="text/html; charset=gb2312") End IF End If End If
'------------------對屬性數組,根據上面打的不同標志來調用不同函數進行處理-------------- Select case flag case 0 attribute(count)=attribute(count) case 1 attribute(count)=func_flag1(attribute(count)) '調用函數func_flag1處理。(例:width=325) case 2 attribute(count)=func_flag2(attribute(count)) '調用函數func_flag2處理。(例:selected) case 3 attribute(count)=func_flag3(attribute(count)) '調用函數func_flag3處理單端標簽(例:<img…) case 4 attribute(count)=(attribute(count)) '另行處理屬性串之間包含分號、空格的情況 End Select Next
count=0 for count=0 to UBound(attribute, 1) attribute_tmp=attribute_tmp+" "+attribute(count) '屬性值之間要有空格 next '----------到這里已經把各個符合屬性值規范的屬性拼成一個串attribute_tmp,下面進行拼標簽------ index=InStr(1,tag," ",1) if InStr(1,tag," ",1)=0 and len(tag)<>"" then '當空格沒找到(意味著是空屬性值標簽),且沒有到末尾時。 tag1=Replace(tag,">"," >") '在此類標簽(例<hr>)后尾加上一個空格 else tag_self=left(tag,index-1) '拼標簽與屬性串。 tag1=tag_self+attribute_tmp+">" end if 'msgbox "tag_self"+tag_self 'msgbox "(要輸出的標簽串)tag1ssssssssssss:"+tag1
'-----------------到這里已經得到一個屬性值規范的標簽,但要開始對單端標簽進行處理------------ '----替換單端標簽-------- 'count=0 for count=0 to UBound(strtag3,1) if InStr(1,tag1,strtag3(count),1)<>0 then '這里利用到前面已切分好的屬性標簽 tag1=func_flag3(tag1) '對付單端標簽——flag=3(例:<img…) end if next '-----------------到這里已經得到一個完全語法規范化的標簽。單端標簽處理完畢------------ alone_tag=tag1 end function
'<SCRIPT LANGUAGE="javaScript"> '<!-- 'tag='<%=alone_tag(tag)% >' 'alert (tag) '//--> '</SCRIPT>
%>
================================================== ================================================== '文件4:func_flag.asp◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎◎ <% strtag_source1="<html ,<body ,<table ,<td ,<tr ,<option ,<font ,<div ,<span ,<h1 ,<h2 ,<form " strtag1 = Split(strtag_source1, ",", -1, 1)
strattri_source2="selected,checked,norwap,readonly,noshade" '單獨屬性 strtag2 = Split(strattri_source2, ",", -1, 1)
strtag_source3="<input ,<img ,<hr ,<br ,<meta" '單端標簽 strtag3 = Split(strtag_source3, ",", -1, 1)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '-------------------以下是處理flag值的多個函數---------【保留】----開始 function func_flag1(tmp)'處理單個屬性:(例:width=325) index=InStr(1,tmp,"=",1) z1=left(tmp,index) z2="""" z3=mid(tmp,index+1,len(tmp)-len(z1)) func_flag1=z1+z2+z3+z2 end function
function func_flag2(tmp)'(例:selected) func_flag2=tmp+"="+""""+tmp+"""" end function
function func_flag3(tmp)'處理單端標簽(例:<img…) func_flag3=replace(cstr(tmp),">","/>") end function '-------------------以上是處理flag值的多個函數---------【保留】----結束 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %>
|