需要: 命令行 HTML5美化器

通缉

在 Linux 下运行的命令行 HTML5美化器。

输入

乱码,丑陋的 HTML5代码。可能是多个模板的结果。你不喜欢它,它也不喜欢你。

输出

纯粹的美。代码很好地缩进了,有足够的换行符,注意它的空格。与其在浏览器中查看代码,不如直接在网站上显示代码。

嫌疑人

  • Clean 做得太多了(见鬼,它改变了我的 doctype!),它不能很好地与 HTML5一起工作。也许有一种方法可以让它合作而不改变 什么都行
  • Vim 做得太少。它只会缩进去。我希望程序能够添加和删除换行符,并在标记中使用空格。

不管是死是活!

20970 次浏览

If you use Haml as your nanoc-filter, your html will automatically be pretty-printed. You can set html5 output as an option.

I suspect tidy can be made to work with the right command-line parameters.

http://tidy.sourceforge.net/docs/quickref.html

You can specify an arbitrary doctype and add new block, inline, and empty tags, and turn on and off lots of tidy's cleaning options.

Depending on what you want it to "beautify" you can probably get decent results. It probably won't be able to do some of the more advanced things like rewriting the html content to eliminate spurious elements or combining them, if it doesn't recognize them.

Copied from a live website I did using HTML5 that is validated as proper HTML5 on all pages thanks to this snippet (PHP in this case but the options and logic is the same for any language used):

    $options = array(
'hide-comments' => true,
'tidy-mark' => false,
'indent' => true,
'indent-spaces' => 4,
'new-blocklevel-tags' => 'article,header,footer,section,nav',
'new-inline-tags' => 'video,audio,canvas,ruby,rt,rp',
'new-empty-tags' => 'source',
'doctype' => '<!DOCTYPE HTML>',
'sort-attributes' => 'alpha',
'vertical-space' => false,
'output-xhtml' => true,
'wrap' => 180,
'wrap-attributes' => false,
'break-before-br' => false,
);


$buffer = tidy_parse_string($buffer, $options, 'utf8');
tidy_clean_repair($buffer);
// Fix a tidy doctype bug
$buffer = str_replace('<html lang="en" xmlns="http://www.w3.org/1999/xhtml">', '<!DOCTYPE HTML>', $buffer);

HTML Tidy has been forked by the w3c and now has support for HTML5 validation.

https://github.com/w3c/tidy-html5