HTML Tidy是一个能够修正不规范的HTML文件,以及调整缩进的工具。
2.tidy的安装
unix或linux下很有可能已经自带tidy了。无需安装。
windows下安装的一个办法则是通过cygwin来安装tidy。
在cygwin安装界面里搜索tidy即可找到。
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. 链接
tidy 原版c语言写的tidy
utidy python包装的库,比较老了
mxtidy python包装的库,比较老了,只支持到python2.5
jtidy 用java写的tidy
tidy-html5 c语言写的支持html5的tidy
npp-tidy2 notepad编辑器的tidy插件