帝国软件
  设为首页 加入收藏 关于我们
 
解密帝国网站管理系统
栏 目:
 
您的位置:首页 > 技术文档 > ASP编程
将html源代码规范化,转换成XSL代码的asp工具
作者:fjfjfj 发布时间:2005-03-12 来源:www.chinaasp.com
将下面的四个文件存在同一级目录下,再在同目录下建立一个文件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值的多个函数---------【保留】----结束
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
%>

  
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
无相关信息

   栏目导行
  PHP编程
  ASP编程
  ASP.NET编程
  JAVA编程
   站点最新
·致合作伙伴的欢迎信
·媒体报道
·帝国软件合作伙伴计划协议
·DiscuzX2.5会员整合通行证发布
·帝国CMS 7.0版本功能建议收集
·帝国网站管理系统2012年授权购买说
·PHPWind8.7会员整合通行证发布
·[官方插件]帝国CMS-访问统计插件
·[官方插件]帝国CMS-sitemap插件
·[官方插件]帝国CMS内容页评论AJAX分
   类别最新
·在ASP中使用数据库
·使用ASP脚本技术
·通过启动脚本来感受ASP的力量
·学习使用ASP对象和组件
·解析asp的脚本语言
·初看ASP-针对初学者
·ASP开发10条经验总结
·ASP之对象总结
·ASP与数据库应用(给初学者)
·关于学习ASP和编程的28个观点
 
关于帝国 | 广告服务 | 联系我们 | 程序开发 | 网站地图 | 留言板 帝国网站管理系统