使用 HTML Tidy 仅缩进 HTML 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7151180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use HTML Tidy to just indent HTML code?
提问by cwd
Is it possible to use HTML Tidy to just indent HTML code?
是否可以使用 HTML Tidy 来缩进 HTML 代码?
Sample Code
示例代码
<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>
Desired Result
想要的结果
<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>
If I run it with the standard command, tidy -f errs.txt -m index.html
then I get this
如果我用标准命令运行它,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>
How can I omit all the extra stuff and actually get it to indent the code?
我怎样才能省略所有额外的东西并实际让它缩进代码?
Forgive me if that's not a feature that it's supposed to support, what library / tool am I looking for?
如果这不是它应该支持的功能,请原谅我,我在寻找什么库/工具?
采纳答案by Paul Sweatte
Use a config file with just the indent
, tidy-mark
, and quiet
options:
使用配置文件只用indent
,tidy-mark
和quiet
选项:
indent: auto
indent-spaces: 2
quiet: yes
tidy-mark: no
Name it tidy_config.txt
and save it the same directory as the .html file. Run it like this:
为其命名并将其tidy_config.txt
保存在与 .html 文件相同的目录中。像这样运行它:
tidy -config tidy_config.txt index.html
For more customization, use the tidy man pageto find other relevant options such as markup: no
or force-output: yes
.
如需更多自定义,请使用tidy 手册页查找其他相关选项,例如markup: no
或force-output: yes
。
回答by kobame
I didn't found a possibility "only reindent - without any changes". The next config file will "repair" as low as possible and (mostly) only re-indent the html. Tidy
still correcting some errorish conditions, like duplicated (repeated) attributes.
我没有发现“只重新缩进 - 没有任何变化”的可能性。下一个配置文件将“修复”尽可能低,并且(大部分)只重新缩进 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
For example the next html-fragment
例如下一个 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>
will changed to
将更改为
<div>
<div>
<p>
not closed para
<h1>
h1 head
</h1>
<ul>
<li>not closed li
<li>closed li
</ul>some text
</div>
</div>
As you can notice, the hide-endtags: yes
hides the closing </li>
from the second bullet in the input. Setting the hide-endtags: no
- will get the next:
如您所见,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>
so, tidy
adds closing </p>
and closing </li>
to first bullet.
因此,tidy
将关闭</p>
和关闭添加</li>
到第一个项目符号。
I didn't found a possibility preserve everything on input and only reindent the file.
我没有发现保留输入中的所有内容并仅重新缩进文件的可能性。
回答by Tomasz Gandor
You need the following option:
您需要以下选项:
tidy --show-body-only yes -i 4 -w 80 -m file.html
http://tidy.sourceforge.net/docs/quickref.html#show-body-only
http://tidy.sourceforge.net/docs/quickref.html#show-body-only
-i 4
- indents 4 spaces (tidy never uses tabs)
-i 4
- 缩进 4 个空格(tidy 从不使用制表符)
-w 80
- wrap at column 80 (default on my system: 68, very narrow)
-w 80
- 在第 80 列换行(我系统的默认值:68,非常窄)
-m
- modify file inplace
-m
- 就地修改文件
(you may want to leave out the last option, and examine the output first)
(您可能希望省略最后一个选项,并首先检查输出)
Showing only body, will naturally leave out the tidy-mark
(generator meta
).
只显示 body,自然会省略tidy-mark
(generator meta
)。
Another cool options are:
--quiet yes
- doesn't print W3C advertisements and other unnecessary output
(errors still reported)
另一个很酷的选项是:
--quiet yes
- 不打印 W3C 广告和其他不必要的输出(仍然报告错误)
回答by thdoan
To answer the poster's original question, using Tidy to just indentHTML code, here's what I use:
为了回答海报的原始问题,使用 Tidy缩进HTML 代码,这是我使用的:
tidy --indent auto --quiet yes --show-body-only auto --show-errors 0 --wrap 0 input.html
tidy --indent auto --quiet yes --show-body-only auto --show-errors 0 --wrap 0 input.html
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>
Output:
输出:
<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>
No extra HTML code added. Errors are suppressed. To find out what each option does, it's best to refer to the official reference.
没有添加额外的 HTML 代码。错误被抑制。要了解每个选项的作用,最好参考官方参考。
回答by user1785636
I am very late to the party :)
我参加聚会很晚:)
But in your tidy config file set
但是在你整洁的配置文件集中
tidy-mark: no
整洁标记:没有
by default this is set to yes.
默认情况下,这设置为是。
Once done, tidy will not add meta generator tag to your html.
完成后,tidy 不会将元生成器标记添加到您的 html。