HTML Tidy-HTML语言检查工具

来源:互联网 发布:网络预警系统 编辑:程序博客网 时间:2024/05/22 10:24


导读:


早期的时候,HTML标准还没有完全形成,无论是否正确地关闭了<p>标记,或设计代码与格式化规则完全背离,都是没有关系的。标记的不匹配,缺少属性设置,不正确的嵌套等,这样或那样的错误都是由于缺少一个被广泛接受的标准而引起的,因为大多数浏览器都带有内置智能性,有一定的容错能力,很多网站开发者甚至都没意识到这些错误。就算浏览器有一定容错能力,但我们却不能忽视这个问题。要使页面可以在所有浏览器里显示,就必须确保HTML代码完全符合W3C标准所定义的规则和语法。目前,有很多离线或在线工具可以完成这一工作,这里我们讨论其中的一种,即HTML Tidy。


  HTML Tidy是一个免费的HTML语言检查工具,检查代码并指出一些与已发布的W3C标准不一致的地方。它可以用来解析包含HTML标记的HTML文件或字符串,并可以自动改正错误,使其与相关标准完全一致。


  安装


  HTML Tidy是跨平台的,在Windows、Macintosh、UNIX等平台上都可以使用。二进制版本是很实用的,但是如果使用的是UNIX系统,你可能宁愿从源程序进行编译和安装。若要做这个工作,可以把下面的源代码复制到你的临时路径,再进行标准的编译/安装操作,如下所示:


  shell> cd /tmp/tidy/build/gmake


  shell> make


  shell> make install


  在这个过程之后,需要在/tmp/tidy/bin/tidy下找到Tidy的编译版本,并将其复制到系统目录/usr/local/bin/下,使其可以被快速调用,之后就可以使用了。


  基本用法


  安装好Tidy的二进制版本,就可以开始用它来测试HTML文件了。列表A中的代码就是一个简单的例子:


  列表A


  shell> tidy -e -q index.html


  line 1 column 1 - Warning: missing <!DOCTYPE> declaration


  line 2 column 1 - Warning: inserting missing 'title' element


  line 4 column 1 - Warning: <body> proprietary attribute "leftmargin"


  line 6 column 1 - Warning: <table> proprietary attribute "height"


  line 6 column 1 - Warning: <table> lacks "summary" attribute


  line 11 column 37 - Warning: <img> lacks "alt" attribute


  line 15 column 1 - Warning: <table> lacks "summary" attribute


  line 17 column 50 - Warning: <img> lacks "alt" attribute


  在这个例子中,Tidy已经在文件中找到了八处潜在的错误,并对每一处给出了错误警告。要注意的是,由于这些错误都不是很关键的错误,所以只是给出警告,指出某些代码可能不正确。


  通过在命令行中增加-m选项,可以让Tidy自动更正原文件中的错误。


  shell> tidy -m -q index.html


  如果需要测试一个大的站点,可以通过在命令行中使用通配符,使其所在目录下的所有文件都运行Tidy。


  shell> tidy -m -q *.html


  如果希望Tidy将某页面的HTML的正确版本重写到一个新的文件中(而不是直接在原文件上进行覆盖),可以使用-output选项加上新文件的名字,正如下面的例子:


  shell> tidy -output index.html.new -q index.html


  还可以使用-e(error)选项,使Tidy将错误写到一个单独的日志文件,如下面你所看到的例子:


  shell> tidy -f error.log index.html


  有必要注意的是,如果HTML代码中嵌入了PHP,ASP,或是JSP标记,HTML Tidy将会简单地忽略掉这些标记,不予检查。这意味着你甚至可以在服务器脚本上运行Tidy,校验其中的HTML代码。这里有一个例子:


  shell> tidy -e -q processor.php


  还可以交互式地运行Tidy,通过调用二进制文件实现。在这种情况下,Tidy将会等待控制台的输入,随后检查输入有无错误。列表B出示了一个例子:


  列表B


  shell> tidy


  <html>


  line 1 column 1 - Warning: missing <!DOCTYPE> declaration


  <head>


  <title>This is a test


  </head>


  line 3 column 1 - Warning: missing </title> before </head>


  <body leftmargin=0>


  <p>


  This is a badly terminated paragraph


  </body>


  </html>


  line 5 column 1 - Warning: <body> proprietary attribute "leftmargin"


  Info: Document content looks like HTML Proprietary


  3 warnings, 0 errors were found!


  值得注意的是,除了给出实时的错误警告外,Tidy还会在输入结束时,会将代码的正确版本打印出来:


  <html>


  <head>


  
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">


  <title>This is a test</title>


  </head>


  <body leftmargin="0">


  <p>This is a badly terminated paragraph</p>


  </body>


  </html>


       高级应用


       可以通过在命令行中明确的指示,控制HTML Tidy输出文件中的更正类型。例如,要使Tidy适当的缩进代码,可以通过在命令行中增加-i("indent")选项实现:


shell> tidy -output new.html -i index.html


       用-c ("clean") 选项可以将<font>和其他格式要素替换为CSS风格:


       shell> tidy -output new.html -c index.html 


       在缺省情况下,Tidy会在HTML文件的标记和属性中使用小写字母。如果习惯大写,可通过增加-u ("upper case") 选项加以更改,如下例所示:


        shell> tidy -output new.html -c -u index.html


        若要在某一特定列换行,可以增加 -w ("wrap") 选项和目标列的标号,如下所示:


        shell> tidy -output new.html -w 40 index.html


         还可以通过增加-asxhtml选项将普通HTML文件转化为格式非常标准的XHTML文件:


         shell> tidy -output new.html -asxhtml index.html


         反之,通过增加-ashtml选项来完成,如下所示:


         shell> tidy -output new.html -ashtml index.html


         如果想将很多设置变成HTML Tidy的缺省动作,一个很好的办法就是把它们写进一个单独的配置文件,这样,每次要调用程序的时候都可以引用这个配置文件。列表C中出示的例子就是这样的一个配置文件:


        列表C


bare: yes # remove proprietary HTML


doctype: auto # set the doctype


drop-empty-paras: yes # automatically delete empty <p> tags


fix-backslash: yes # replace by / in URLs


literal-attributes: yes # retain whitespace in attribute values


lower-literals: yes # convert attribute values to lower case


output-xhtml: yes # produce valid XHTML output


quote-ampersand: yes # replace & with &


quote-marks: yes # replace " with "


repeated-attributes: keep-last # use the last of duplicated attributes


indent: yes # automatically indent code


indent-spaces: 2 # number of spaces to indent by


wrap-php: no # wrap text contained in PHP tags


char-encoding: ascii # character encoding to use


tidy-mark: no # omit Tidy meta information in corrected code


        清除文件时若要进行如上设置,可以通过在命令行中添加-config 选项来完成,如下所示:


        shell> tidy -output a.html -configconfig.tidy index.html


        也可以通过添加-help-confi选项来获取一系列可用的配置文件:


        shell> tidy -help-config...quote-ampersand Boolean y/n,


        yes/no, t/f, true/false, 1/0quote-marks Boolean y/n,


        yes/no, t/f, true/false, 1/0quote-nbsp Boolean y/n,


        yes/no, t/f, true/false, 1/0repeated-attributesenum keep-first, keep-lastreplace-color Boolean y/n,


        yes/no, t/f, true/false, 1/0show-body-only Boolean y/n,


        yes/no, t/f, true/false, 1/0...


        若要查看当前的配置设置,则需要添加-show-config选项:


        shell>tidy-show-config...show-body-only Boolean noshow-errors Integer 6show-warnings Boolean yesslide-style Stringsplit Boolean no...


       最后,还可以通过添加-h选项,获得命令行帮助:


       shell> tidy -h


       暂时就写这么多。希望使用者感觉到HTML Tidy的确是一个很有价值的工具,能够完善自己的站点代码,使其完全符合已发布的W3C标准。以上这些小技巧是一些参考意见,教你如何使用HTML Tidy来优化代码,帮助你更高效地使用这个工具。


(责任编辑:陈毅东)


查看本文的国际来源


 





        本文转自


http://www.techupdate.com.cn/techupdate/2006/0321/232024.shtml

 
原创粉丝点击