需要:命令行 HTML5 美化器

发布于 2024-08-29 05:15:35 字数 385 浏览 11 评论 0原文

想要

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

输入

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

输出

纯粹之美。代码缩进得很好,有足够的换行符,关心它的空白。您不想在网络浏览器中查看它,而是希望直接在您的网站上显示代码。

怀疑

  • tidy 做了太多事情(哎呀,它改变了我的文档类型!),而且它不能很好地与 HTML5 配合使用。也许有一种方法可以让它合作而不改变任何东西?
  • vim 做得太少了。它只是缩进。我希望程序添加和删除换行符,并使用标签内的空格。

死或生!

Wanted

A command line HTML5 beautifier running under Linux.

Input

Garbled, ugly HTML5 code. Possibly the result of multiple templates. You don't love it, it doesn't love you.

Output

Pure beauty. The code is nicely indented, has enough line breaks, cares for it's whitespace. Rather than viewing it in a webbrowser, you would like to display the code on your website directly.

Suspects

  • tidy does too much (heck, it alters my doctype!), and it doesn't work well with HTML5. Maybe there is a way to make it cooperate and not alter anything?
  • vim does too little. It only indents. I want the program to add and remove line breaks, and to play with the whitespace inside of tags.

DEAD OR ALIVE!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

还在原地等你 2024-09-05 05:15:35

HTML Tidy 已由 w3c 分叉,现在支持 HTML5 验证。

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

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

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

叫嚣ゝ 2024-09-05 05:15:35

我怀疑可以使用正确的命令行参数来使 tidy 工作。

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

您可以指定任意文档类型并添加新的块、内联和空标签,并打开和关闭许多 tidy 的清理选项。

根据你想要它“美化”的内容,你可能会得到不错的结果。如果它无法识别它们,它可能无法执行一些更高级的操作,例如重写 html 内容以消除虚假元素或组合它们。

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.

献世佛 2024-09-05 05:15:35

从我使用 HTML5 制作的实时网站复制而来,由于此代码片段,该代码在所有页面上都被验证为正确的 HTML5(在本例中为 PHP,但选项和逻辑对于所使用的任何语言都是相同的):

    $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);

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);
一张白纸 2024-09-05 05:15:35

如果您使用 Haml 作为您的 nanoc 过滤器,您的 html 将自动打印漂亮。您可以将 html5 输出设置为选项。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文