整理HTML中的HTML5输出缩进 [英] Tidying HTML5 Output Indentation in PHP

查看:512
本文介绍了整理HTML中的HTML5输出缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的web应用程序生成HTML5输出作为可变数量视图的串联。最后的结果是缩进:

My web application produces HTML5 output as a concatenation of a variable number of views. The end result is a mess of indentation:

            </div>
        </div>

        <div id="content">

<div id="question-header">
    <h1>

我想缩进代码以遮蔽各个视图的原点并使输出更易于遵循。

I want to indent the code to obscure the origin of individual views and to make the output easier to follow.

我看了整洁的PHP扩展,但是我所有试图使它与HTML5一起工作的尝试都产生了不正确的缩进。

I have looked into the Tidy PHP extension but all my attempts to make it work with HTML5 have produced improper indenting.

推荐答案

最接近您在PHP域名中寻找的是Dindent, https://github.com/gajus/dindent 。 Dindent是一个使用正则表达式缩进标记的HTML美化工具。这与Tidy不同,它充当DOM解析器。

The closest to what you are looking for in the PHP land is Dindent, https://github.com/gajus/dindent. Dindent is a HTML beautifier that uses regular expressions to indent the markup. This is different from Tidy, that acts as a DOM parser.

从文档:

From the documentation:


不使用正则表达式解析HTML是有原因的。
但是,DOM解析器将重建整个HTML文档。它会添加
缺少的标签,关闭开放的块标签,或删除不是
有效HTML的任何内容。这就是Tidy所做的,DOM等等。当调试HTML输出时,这种行为是
不合需要的。基于正则表达式的解析器不会
重建文档。 Dindent只会添加缩进,而不会影响标记。

There is a good reason not to use regular expression to parse HTML. However, DOM parser will rebuild the whole HTML document. It will add missing tags, close open block tags, or remove anything that's not a valid HTML. This is what Tidy does, DOM, etc. This behavior is undesirable when debugging HTML output. Regex based parser will not rebuild the document. Dindent will only add indentation, without otherwise affecting the markup.

Dindent的唯一目的是缩进HTML标记。它允许配置什么元素作为内联处理,哪些元素被视为块。

Dindent sole purpose is to indent the HTML markup. It allows to configure what elements to treat as inline and what elements to treat as block.

这篇关于整理HTML中的HTML5输出缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆