41

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

我怎样才能省略所有额外的东西并真正让它缩进代码?

如果这不是它应该支持的功能,请原谅我,我在寻找什么库/工具?

4

6 回答 6

30

使用 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

如需更多自定义,请使用整洁的手册页查找其他相关选项,例如markup: noforce-output: yes

于 2012-01-20T06:12:17.703 回答
25

我没有发现“仅重新缩进 - 没有任何更改”的可能性。下一个配置文件将“修复”尽可能低,并且(大部分)只重新缩进 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: 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>到第一个项目符号。

我没有发现保留输入中的所有内容并仅重新缩进文件的可能性。

于 2014-03-12T09:04:26.197 回答
16

您需要以下选项:

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 个空格(编辑整洁从不使用制表符

--indent-with-tabs yes- 相反(--tab-size可能会影响换行)

-w 80- 在第 80 列换行(我的系统上的默认值:68,非常窄)

-m- 就地修改文件

(您可能想省略最后一个选项,并首先检查输出)

只显示身体,自然会省略tidy-mark(生成器meta)。

另一个很酷的选项是: --quiet yes- 不打印 W3C 广告和其他不必要的输出(仍然报告错误)

于 2014-02-19T09:57:03.060 回答
6

为了回答海报的原始问题,使用 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 代码。错误被抑制。要了解每个选项的作用,最好参考官方参考

于 2016-01-19T09:13:05.587 回答
3

我参加聚会很晚了:)

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

整洁标记:没有

默认情况下,这设置为是。

完成后,tidy 不会将元生成器标签添加到您的 html 中。

于 2014-01-23T12:37:57.937 回答
1

如果你想简单地格式化你收到的任何 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
于 2020-09-22T03:07:13.247 回答