首页 > 开发 > HTML > 正文

使用Tidy工具来照料您的网站

2018-10-16 21:10:36
字体:
来源:转载
供稿:网友

回到以往的好日子,那时的HTML标准还是一个移动中的目标,无论您正确地结束了<p>标签或者让您的格式规则与样式代码相分离都不会造成麻烦。不匹配的标签、丢失的属性、糟糕的嵌套元素--缺乏广泛采用的标准造成了这些和其他的错误,但因为大部分浏览器都内建了智能功能来避免这些错误,所以大部分开发者也根本不会觉察到它们的存在。

尽管浏览器自身在尽量修正这些错误,但是,这并不是您可以忽略这些问题的理由。为了让您的网页在所有的浏览器中表现一致,您的HTML必需与W3C 标准中界定的规则和语法完全一致,有很多工具可以实现这一需求,有在线的也有离线的,这篇文章将讨论它们中的一个:非常酷的HTML Tidy。

HTML Tidy是一个免费的HTML检查工具,它是设计用来检查您的HTML代码,并可以指出其中没有完全符合W3C发布标准的地方,它可以用来分析一个HTML文件或者一个包含HTML语句的字符串,还可以自动进行必需的修改以使代码符合相关标准的要求。

安装

HTML Tidy是免费的,它可以运行在Windows,Macintosh和*NIX平台上,有二进制版本可供直接使用,如果您运行的是*NIX平台,那您可能更希望亲自从源代码进行编译和安装,可以这样操作:将源文件抽取到您的临时文件夹并执行基本的编译-安装过程,如下所示:

shell> cd /tmp/tidy/build/gmake

shell> make

shell> make install

当这一过程结束的时候,您应该可以在/tmp/tidy/bin/tidy文件夹中找到一个编译好了的Tidy的二进制版本,将这个文件拷贝到您的系统文件夹/usr/local/bin/中,这样就更易于访问了。现在您已经准备好使用这个工具了。

基本用法

一旦安装了二进制版本,您马上就可以开始使用它来检验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(修饰符)这个选项来实现自动修正原始文件:

shell> tidy -m -q index.html

如果您需要测试一个很大的网站,可以使用命令行中的通配符来测试一个文件夹中的所有文件(而不是仅仅一个):

shell> tidy -m -q *.html

如果您希望那个Tidy帮助写出修正过的网页到一个新的文件(而不是覆盖原有的),那就在使用-output选项和新的文件名,如下例所示:

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

您可以通过-e(“错误”)选项将所有的错误输出到一个单独的日志文件供以后查看:

shell> tidy -f error.log index.html

另外要注意,如果您的HTML代码中含有内嵌的PHP,ASP或JSP代码,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>

<meta name="generator" content=

"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>

高级应用

您还可以控制Tidy对一个文件修改的方式,这可以通过在命令行传递特定的参数来实现,例如,让Tidy来重新对代码进行正确的缩进,可以添加-i(“缩进”)选项:

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

替换<font>和其他与CSS样式规则相关的格式元素,可以使用-c(“清除”)选项:

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

在默认情况下,Tidy对HTML文件中所有的标签和属性都使用小写字母,如果您希望使用大写字母,可以添加-u(“大写字母”)选项,如下例所示:

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

使文字在特定的行宽进行换行,可以添加-w(“换行”)选项与指定的行宽,如下例所示:

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

您可以通过添加-asxhtml选项来将一个HTML文档转换为格式良好的(well-formed)XHTML文档:

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

通过-ashtml选项可以进行反向操作:

shell> tidy -output new.html -ashtml index.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选项来告诉Tidy使用这些设置:

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

您可以通过-help-config选项来获取一个配置选项的列表:

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选项来查看当前配置设定的快照(snapshot):

shell> tidy -show-config...show-body-only             

Boolean    noshow-errors                 Integer   

6show-warnings               Boolean    yesslide-style             

    Stringsplit                       Boolean    no...

最后,您可以使用-h选项来获得命令行的帮助:

shell> tidy -h

目前,这就是所有的功能了,希望您会发现Tidy在辅助您的网站达到完全符合W3C发布标准方面是一个非常有价值的工具,这篇指南所介绍的要点可以让您了解如何控制HTML Tidy工具来操作您的代码,同时也会帮助您更有效率地使用这个工具。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表