2011-08-22 111 views
25

是否可以使用HTML Tidy來縮進HTML代碼?使用HTML Tidy來縮進HTML代碼?

示例代碼

<form action="?" method="get" accept-charset="utf-8"> 

<ul> 
<li> 
<label class="screenReader" for="q">Keywords</label><input type="text" name="q" value="" id="q" /> 
</li> 
<li><input class="submit" type="submit" value="Search" /></li> 
</ul> 


</form> 

所需的結果

<form action="?" method="get" accept-charset="utf-8"> 
    <ul> 
     <li> 
     <label class="screenReader" for="q">Keywords</label><input type="text" name="q" value="" id="q"/> 
     </li> 
     <li><input class="submit" type="submit" value="Search"/></li> 
    </ul> 
</form> 

如果我用標準的命令來運行它,然後tidy -f errs.txt -m index.html我得到這個

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"> 
<html> 
<head> 
<meta name="generator" content= 
"HTML Tidy for Mac OS X (vers 31 October 2006 - Apple Inc. build 15.3.6), see www.w3.org"> 
<title></title> 
</head> 
<body> 
<form action="?" method="get" accept-charset="utf-8"> 
<ul> 
<li><label class="screenReader" for= 
"q">Keywords</label><input type="text" name="q" value="" id= 
"q"></li> 
<li><input class="submit" type="submit" value="Search"></li> 
</ul> 
</form> 
</body> 
</html> 

我怎樣才能省略所有額外的東西,並真正讓它縮進代碼?

請原諒我,如果這不是它應該支持的功能,我在找什麼庫/工具?

+0

其他人建議,如果你只是想縮進,http://prettydiff.com/?m=beautify&html可能是更好的選擇。 – Amanda

回答

17

使用配置文件只用indenttidy-markquiet選項:

indent: auto 
indent-spaces: 2 
quiet: yes 
tidy-mark: no 

將它命名爲tidy_config.txt,並在同一目錄保存它作爲.html文件。像這樣運行:

tidy -config tidy_config.txt index.html 

需要更多定製,使用tidy man page找到其他相關的選項,如markup: noforce-output: yes

+6

這不回答問題。它仍在添加元發生器標籤。有沒有辦法關閉除縮進以外的所有更改? –

+1

使用[tidy手冊頁](http://tidy.sourceforge.net/docs/tidy_man.html)來引用和測試標誌。嘗試通過在配置文件中添加'markup:no'或'input-xml:yes'和'force-output:yes'來關閉默認值。 –

+4

如果你是沒有中間文件的oneliner的粉絲,你可以寫成'tidy -xml --indent auto --indent-spaces 2 --quiet yes index.html'。 – tlwhitec

3

我非常遲到了:)

但在整潔的配置文件設置

整潔的標誌:沒有

默認情況下此設置爲yes。

一旦完成,整潔將不會添加meta生成器標記到您的html。

+3

我的'tidy'版本(可能還有其他的)會接受配置選項,也可以作爲命令行選項(有時候比拖拽配置文件更有用),比如:'tidy --tidy-mark no -utf8 - w 80 -i file.html'。 –

8

您需要以下選項:

tidy --show-body-only yes -i 4 -w 80 -m file.html 

http://tidy.sourceforge.net/docs/quickref.html#show-body-only

-i 4 - 縮進4個空格(整齊從不使用選項卡)

-w 80 - 換在我的系統上列80(默認:68 ,非常窄)

-m - 修改文件

(你可能要離開了最後一個選項,並第一次檢查輸出)

顯示唯一機構,自然會離開了tidy-mark(發電機meta)。

另一個很酷的選項有: --quiet yes - 不打印W3C廣告和其他不必要的輸出 (錯誤仍報)

+0

這是最簡單的解決方案。 – 10basetom

+0

「只顯示身體:是」是正確答案(即使部分 - 整齊無法修復損壞的標籤)。 – AnrDaemon

17

我沒有找到的可能性「只有重新縮進 - 沒有任何變化」。下一個配置文件將盡可能低地「修復」,並且(大部分)只會重新縮進html。 Tidy仍在糾正一些錯誤的情況,如重複(重複)的屬性。

#based on http://tidy.sourceforge.net/docs/quickref.html 
#HTML, XHTML, XML Options Reference 
anchor-as-name: no #? 
doctype: omit 
drop-empty-paras: no 
fix-backslash: no 
fix-bad-comments: no 
fix-uri:no 
hide-endtags: yes #? 
#input-xml: yes  #? 
join-styles: no 
literal-attributes: yes 
lower-literals: no 
merge-divs: no 
merge-spans: no 
output-html: yes 
preserve-entities: yes 
quote-ampersand: no 
quote-nbsp: no 
show-body-only: auto 

#Diagnostics Options Reference 
show-errors: 0 
show-warnings: 0 

#Pretty Print Options Reference 
break-before-br: yes 
indent: yes 
indent-attributes: no #default 
indent-spaces: 4 
tab-size: 4 
wrap: 132 
wrap-asp: no 
wrap-jste: no 
wrap-php: no 
wrap-sections: no 

#Character Encoding Options Reference 
char-encoding: utf8 

#Miscellaneous Options Reference 
force-output: yes 
quiet: yes 
tidy-mark: no 

例如下一個HTML片段

<div> 
<div> 
<p> 
not closed para 
<h1> 
h1 head 
</h1> 
<ul> 
<li>not closed li 
<li>closed li</li> 
</ul> 
some text 
</div> 
</div> 

將變爲

<div> 
    <div> 
     <p> 
      not closed para 
     <h1> 
      h1 head 
     </h1> 
     <ul> 
      <li>not closed li 
      <li>closed li 
      </ul>some text 
    </div> 
</div> 

正如可以注意到,在hide-endtags: yes隱藏從輸入所述第二子彈閉合</li>。設置hide-endtags: no - 將獲得下一個:

<div> 
    <div> 
     <p> 
      not closed para 
     </p> 
     <h1> 
      h1 head 
     </h1> 
     <ul> 
      <li>not closed li 
      </li> 
      <li>closed li 
      </li> 
     </ul>some text 
    </div> 
</div> 

所以,tidy增加了關閉</p>和關閉</li>到第一發子彈。

我沒有發現一個可能性保留輸入的所有內容,只重新保留該文件。

+0

謝謝你。在我的PHP腳本中使用它,並且html輸出看起來很棒。花了我一段時間來複制屬性,所以這裏有一個pastebin給那些希望像這樣配置整齊的人:https://pastebin.com/JP8ucTzc – bbruman

2

要回答海報的原題,用整潔至只是縮進 HTML代碼,這裏是我使用的:

tidy --indent auto --quiet yes --show-body-only auto --show-errors 0 --wrap 0 input.html

input.html

<form action="?" method="get" accept-charset="utf-8"> 

<ul> 
<li> 
<label class="screenReader" for="q">Keywords</label><input type="text" name="q" value="" id="q" /> 
</li> 
<li><input class="submit" type="submit" value="Search" /></li> 
</ul> 


</form> 

輸出:

<form action="?" method="get" accept-charset="utf-8"> 
    <ul> 
    <li><label class="screenReader" for="q">Keywords</label><input type="text" name="q" value="" id="q"></li> 
    <li><input class="submit" type="submit" value="Search"></li> 
    </ul> 
</form> 

沒有額外添加HTML代碼。錯誤被抑制。要了解每個選項的作用,最好參考official reference

+0

在vim: '%!tidy --show-errors 0 --show-body -only auto -qi -w 0' – unagi

+0

不幸的是,這些選項是不夠的。 tidy在此代碼的周圍插入表單標籤: '' 但是這個很好: '' 'tidy --version'輸出'2009年3月25日發佈的HTML Tidy for Linux' – unagi