================================================== ================================================== '文件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
'-------------------------------------本部分得到全文源代码,开始------------------------------------ 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
'----------------------------进入这里时应该已经得到一个完整的标签-------------------------------- '--------在此建立了一个大函数用来处理一个完整的标签里的属性值。一直到页面尾部为止。 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
'--------『这个循环是处理上面处理完毕的属性值字符数组(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
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '-------------------以下是处理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值的多个函数---------【保留】----结束 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %>