回到以往的好日子,那時(shí)的HTML標(biāo)準(zhǔn)還是一個(gè)移動(dòng)中的目標(biāo),無(wú)論您正確地結(jié)束了<p>標(biāo)簽或者讓您的格式規(guī)則與樣式代碼相分離都不會(huì)造成麻煩。不匹配的標(biāo)簽、丟失的屬性、糟糕的嵌套元素--缺乏廣泛采用的標(biāo)準(zhǔn)造成了這些和其他的錯(cuò)誤,但因?yàn)榇蟛糠譃g覽器都內(nèi)建了智能功能來(lái)避免這些錯(cuò)誤,所以大部分開(kāi)發(fā)者也根本不會(huì)覺(jué)察到它們的存在。
盡管瀏覽器自身在盡量修正這些錯(cuò)誤,但是,這并不是您可以忽略這些問(wèn)題的理由。為了讓您的網(wǎng)頁(yè)在所有的瀏覽器中表現(xiàn)一致,您的HTML必需與W3C 標(biāo)準(zhǔn)中界定的規(guī)則和語(yǔ)法完全一致,有很多工具可以實(shí)現(xiàn)這一需求,有在線(xiàn)的也有離線(xiàn)的,這篇文章將討論它們中的一個(gè):非??岬腍TML Tidy。
HTML Tidy是一個(gè)免費(fèi)的HTML檢查工具,它是設(shè)計(jì)用來(lái)檢查您的HTML代碼,并可以指出其中沒(méi)有完全符合W3C發(fā)布標(biāo)準(zhǔn)的地方,它可以用來(lái)分析一個(gè)HTML文件或者一個(gè)包含HTML語(yǔ)句的字符串,還可以自動(dòng)進(jìn)行必需的修改以使代碼符合相關(guān)標(biāo)準(zhǔn)的要求。
安裝
HTML Tidy是免費(fèi)的,它可以運(yùn)行在Windows,Macintosh和*NIX平臺(tái)上,有二進(jìn)制版本可供直接使用,如果您運(yùn)行的是*NIX平臺(tái),那您可能更希望親自從源代碼進(jìn)行編譯和安裝,可以這樣操作:將源文件抽取到您的臨時(shí)文件夾并執(zhí)行基本的編譯-安裝過(guò)程,如下所示:
shell> cd /tmp/tidy/build/gmake
shell> make
shell> make install
當(dāng)這一過(guò)程結(jié)束的時(shí)候,您應(yīng)該可以在/tmp/tidy/bin/tidy文件夾中找到一個(gè)編譯好了的Tidy的二進(jìn)制版本,將這個(gè)文件拷貝到您的系統(tǒng)文件夾/usr/local/bin/中,這樣就更易于訪(fǎng)問(wèn)了?,F(xiàn)在您已經(jīng)準(zhǔn)備好使用這個(gè)工具了。
基本用法
一旦安裝了二進(jìn)制版本,您馬上就可以開(kāi)始使用它來(lái)檢驗(yàn)HTML代碼,列表A展示了一個(gè)簡(jiǎn)單的例子:
列表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
在這個(gè)例子中,Tidy發(fā)現(xiàn)了文件中八個(gè)潛在的錯(cuò)誤,并為每個(gè)錯(cuò)誤打印出了一個(gè)警告,注意,這些錯(cuò)誤并不是嚴(yán)重錯(cuò)誤,只是警告代碼中的某些部分并不是非常正確。
您可以通過(guò)在命令行中添加-m(修飾符)這個(gè)選項(xiàng)來(lái)實(shí)現(xiàn)自動(dòng)修正原始文件:
shell> tidy -m -q index.html
如果您需要測(cè)試一個(gè)很大的網(wǎng)站,可以使用命令行中的通配符來(lái)測(cè)試一個(gè)文件夾中的所有文件(而不是僅僅一個(gè)):
shell> tidy -m -q *.html
如果您希望那個(gè)Tidy幫助寫(xiě)出修正過(guò)的網(wǎng)頁(yè)到一個(gè)新的文件(而不是覆蓋原有的),那就在使用-output選項(xiàng)和新的文件名,如下例所示:
shell> tidy -output index.html.new -q index.html
您可以通過(guò)-e(“錯(cuò)誤”)選項(xiàng)將所有的錯(cuò)誤輸出到一個(gè)單獨(dú)的日志文件供以后查看:
shell> tidy -f error.log index.html
另外要注意,如果您的HTML代碼中含有內(nèi)嵌的PHP,ASP或JSP代碼,Tidy會(huì)簡(jiǎn)單地忽略它們并將它們留在適當(dāng)?shù)奈恢?,這意味著您甚至可以在服務(wù)器端腳本上運(yùn)行Tidy工具,來(lái)檢驗(yàn)其中的HTML代碼部分,這是一個(gè)例子:
shell> tidy -e -q processor.php
您還可以以交互方式運(yùn)行Tidy工具,只調(diào)用程序文件,而不附加任何參數(shù),在這個(gè)例子中,Tidy等待控制臺(tái)的輸入并檢查其錯(cuò)誤,列表B展示了這樣一個(gè)例子:
列表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!
注意,除了給您實(shí)時(shí)的錯(cuò)誤警告之外,Tidy還可以在輸入結(jié)束的時(shí)候打印出正確版本的代碼:
<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>
高級(jí)應(yīng)用
您還可以控制Tidy對(duì)一個(gè)文件修改的方式,這可以通過(guò)在命令行傳遞特定的參數(shù)來(lái)實(shí)現(xiàn),例如,讓Tidy來(lái)重新對(duì)代碼進(jìn)行正確的縮進(jìn),可以添加-i(“縮進(jìn)”)選項(xiàng):
shell> tidy -output new.html -i index.html
替換<font>和其他與CSS樣式規(guī)則相關(guān)的格式元素,可以使用-c(“清除”)選項(xiàng):
shell> tidy -output new.html -c index.html
在默認(rèn)情況下,Tidy對(duì)HTML文件中所有的標(biāo)簽和屬性都使用小寫(xiě)字母,如果您希望使用大寫(xiě)字母,可以添加-u(“大寫(xiě)字母”)選項(xiàng),如下例所示:
shell> tidy -output new.html -c -u index.html
使文字在特定的行寬進(jìn)行換行,可以添加-w(“換行”)選項(xiàng)與指定的行寬,如下例所示:
shell> tidy -output new.html -w 40 index.html
您可以通過(guò)添加-asxhtml選項(xiàng)來(lái)將一個(gè)HTML文檔轉(zhuǎn)換為格式良好的(well-formed)XHTML文檔:
shell> tidy -output new.html -asxhtml index.html
通過(guò)-ashtml選項(xiàng)可以進(jìn)行反向操作:
shell> tidy -output new.html -ashtml index.html
如果您需要對(duì)Tidy的默認(rèn)選項(xiàng)進(jìn)行大量調(diào)整,那么最好將這些選項(xiàng)放在一個(gè)單獨(dú)的配置文件中,您可以在每次調(diào)用程序的時(shí)候進(jìn)行參考,列表C展示了一個(gè)配置文件的例子:
列表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
當(dāng)整理一個(gè)文件的時(shí)候,可以通過(guò)在命令行中添加-config選項(xiàng)來(lái)告訴Tidy使用這些設(shè)置:
shell> tidy -output a.html -configconfig.tidy index.html
您可以通過(guò)-help-config選項(xiàng)來(lái)獲取一個(gè)配置選項(xiàng)的列表:
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選項(xiàng)來(lái)查看當(dāng)前配置設(shè)定的快照(snapshot):
shell> tidy -show-config...show-body-only
Boolean noshow-errors Integer
6show-warnings Boolean yesslide-style
Stringsplit Boolean no...
最后,您可以使用-h選項(xiàng)來(lái)獲得命令行的幫助:
shell> tidy -h
目前,這就是所有的功能了,希望您會(huì)發(fā)現(xiàn)Tidy在輔助您的網(wǎng)站達(dá)到完全符合W3C發(fā)布標(biāo)準(zhǔn)方面是一個(gè)非常有價(jià)值的工具,這篇指南所介紹的要點(diǎn)可以讓您了解如何控制HTML Tidy工具來(lái)操作您的代碼,同時(shí)也會(huì)幫助您更有效率地使用這個(gè)工具。
新聞熱點(diǎn)
疑難解答