tidy初步

1.概要
[url=http://tidy.sourceforge.net/]HTML Tidy[/url]是一个能够修正不规范的HTML文件,以及调整缩进的工具。

2.tidy的安装
unix或linux下很有可能已经自带tidy了。无需安装。
windows下安装的一个办法则是通过cygwin来安装tidy。
在cygwin安装界面里搜索tidy即可找到。
[img]http://dl2.iteye.com/upload/attachment/0103/6384/6ff6eb30-56c0-36df-bcc3-4a82a974e5c8.png[/img]

3.测试tidy
查看帮助

tidy -h


现在准备一个不规范的html
bad.html

<html>
<h1>heading
<h2>subheading</h3>
<a href="#refs">References<a>
</html>


输入

tidy -indent -output good.html bad.html


查看生成的good.html


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">

<html>
<head>
<meta name="generator" content=
"HTML Tidy for Cygwin (vers 25 March 2009), see www.w3.org">

<title></title>
</head>

<body>
<h1>heading</h1>

<h2>subheading</h2><a href="#refs">References</a>
</body>
</html>


4. python调用
python上有一些库对tidy进行了封装,如utidy, mxtidy,但是都比较老了,所以很可能不能用。
另一种保证能用的方法就是利用python的subprocess模块直接调用命令行。


import subprocess

text = open("bad.html").read()
tidy = subprocess.Popen(['tidy', '-indent'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
tidy.stdin.write(text)
tidy.stdin.close()
print tidy.stdout.read()


5. 链接
[url=http://tidy.sourceforge.net/]tidy[/url] 原版c语言写的tidy
[url=http://sourceforge.net/projects/utidylib/]utidy[/url] python包装的库,比较老了
[url=http://www.egenix.com/products/python/mxExperimental/mxTidy/]mxtidy[/url] python包装的库,比较老了,只支持到python2.5
[url=http://jtidy.sourceforge.net/]jtidy[/url] 用java写的tidy
[url=http://w3c.github.io/tidy-html5/]tidy-html5[/url] c语言写的支持html5的tidy
[url=https://github.com/davegb3/NppTidy2]npp-tidy2[/url] notepad编辑器的tidy插件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值