簡體   English   中英

使用 HTML Tidy 僅縮進 HTML 代碼?

[英]Use HTML Tidy to just indent HTML code?

是否可以使用 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>

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

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

使用indenttidy-markquiet選項:

tidy \
  -indent \
  --indent-spaces 2 \
  -quiet \
  --tidy-mark no \
  index.html

或者,使用配置文件而不是命令行選項:

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

將其命名為tidy_config.txt並將其保存在與 .html 文件相同的目錄中。 像這樣運行它:

tidy -config tidy_config.txt index.html

如需更多自定義,請使用tidy 手冊頁查找其他相關選項,例如markup: noforce-output: yes

我沒有發現“只重新縮進 - 沒有任何變化”的可能性。 下一個配置文件將“修復”盡可能低,並且(大部分)只重新縮進 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-fragment

<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: yeshide-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>到第一個項目符號。

我沒有發現保留輸入中的所有內容並僅重新縮進文件的可能性。

您需要以下選項:

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 個空格(編輯 tidy 從不使用制表符
或者
--indent-with-tabs yes - 相反( --tab-size可能會影響換行)

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

-m - 就地修改文件

(您可能希望省略最后一個選項,並首先檢查輸出)

只顯示 body,自然會省去tidy-mark (generator meta )。

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

為了回答海報的原始問題,使用 Tidy縮進HTML 代碼,這是我使用的:

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

輸入.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 代碼。 錯誤被抑制。 要了解每個選項的作用,最好參考官方參考

我參加聚會很晚:)

但是在你整潔的配置文件集中

整潔標記:沒有

默認情況下,這設置為是。

完成后,tidy 不會將元生成器標記添加到您的 html。

如果你想簡單地格式化你收到的任何 html,忽略錯誤並很好地縮進代碼,這是一個很好的使用tidy班輪

tidy --show-body-only yes -i 4 -w 80 -m -quiet --force-output y -wrap 0 2>/dev/null

您也可以將它與curl一起使用

curl -s someUrl | tidy --show-body-only yes -i 4 -w 80 -m -quiet --force-output y -wrap 0 2>/dev/null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM