【问题标题】:Use HTML Tidy to just indent HTML code?使用 HTML Tidy 来缩进 HTML 代码?
【发布时间】:2011-11-01 08:17:06
【问题描述】:

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

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

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

【问题讨论】:

标签: html htmltidy


【解决方案1】:

使用indent、tidy-mark 和quiet 选项:

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 man page 查找其他相关选项,例如markup: no 或force-output: yes。

【讨论】:

  • 这没有回答问题。它仍在添加元生成器标签。有没有办法关闭除缩进之外的所有更改?
  • 使用tidy man page 来引用和测试标志。尝试通过将markup: no 或input-xml: yes 和force-output: yes 添加到配置文件来关闭默认值。
  • 如果你是没有中间文件的oneliners爱好者,可以写成tidy -xml --indent auto --indent-spaces 2 --quiet yes index.html。
  • tidy-mark: no 应该关闭元生成器标签,
  • @DaveBurton 很好。谢谢!
【解决方案2】:

我没有发现“仅重新缩进 - 没有任何更改”的可能性。下一个配置文件将“修复”尽可能低,并且(大部分)只重新缩进 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 隐藏了输入中第二个项目符号的结束 &lt;/li&gt;。设置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 将关闭 &lt;/p&gt; 和关闭 &lt;/li&gt; 添加到第一个项目符号。

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

【讨论】:

  • 谢谢。在我的 PHP 脚本中使用它,html 输出看起来很棒。我花了一些时间来复制属性,所以这里有一个 pastebin 供任何希望像这样配置整洁的人使用:pastebin.com/JP8ucTzc
  • 隐藏结束标签:否
  • 感谢您的解释。但是如何将“some text”变成“\nsometext”呢?以及如何保留源代码中的空行?
【解决方案3】:

您需要以下选项:

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 个空格(EDIT:tidy 从不使用制表符)
或
--indent-with-tabs yes - 改为(--tab-size 可能会影响换行)

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

-m - 就地修改文件

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

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

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

【讨论】:

  • “show-body-only: yes”是正确答案(即使部分 - tidy 也无法修复损坏的标签)。
  • Tidy 确实使用标签,只需将--indent-with-tabs yes 添加到命令行参数即可。
【解决方案4】:

为了回答发帖人的原始问题,使用 Tidy 来缩进 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。

【讨论】:

  • 在 vim 中:%!tidy --show-errors 0 --show-body-only auto -qi -w 0
  • 不幸的是,这些选项还不够。 tidy 在此代码周围插入一个表单标签:&lt;input id="a" type="checkbox"&gt;&lt;label for="a"&gt;a&lt;/label&gt; 但这个很好:&lt;label&gt;&lt;input id="a" type="checkbox"&gt;a&lt;/label&gt; tidy --version 输出 HTML Tidy for Linux released on 25 March 2009
【解决方案5】:

我迟到了 :)

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

整洁标记:没有

默认设置为yes。

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

【讨论】:

  • 我的tidy 版本(可能还有其他版本)也将接受配置选项作为命令行选项(有时这比拖动配置文件更可取),例如:tidy --tidy-mark no -utf8 -w 80 -i file.html。
【解决方案6】:

如果你想简单地格式化你收到的任何 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

【讨论】:

  • 输入文件在第一个命令中去哪里了?
  • 第二个命令以curl -s someUrl | 开头,| 是对命令其余部分的重定向。所以,你可以卷曲一些网站并重定向它,或者你可以说cat index.html | tidy --show-body-only yes -i 4 -w 80 -m -quiet --force-output y -wrap 0 2&gt;/dev/null 例如
猜你喜欢
  • 2019-12-11
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-17
  • 2011-08-14
  • 2011-01-19
相关资源
最近更新 更多