繁体   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