gajus/dindent
Composer 安装命令:
composer require gajus/dindent
包简介
HTML indentation library for development and testing.
README 文档
README
Dindent (aka., "HTML beautifier") will indent HTML for development and testing. Dedicated for those who suffer from reading a template engine produced markup.
Abuse Case
Dindent will not sanitize or otherwise manipulate your output beyond indentation.
If you are looking to remove malicious code or make sure that your document is standards compliant, consider the following alternatives:
If you need to indent your code in the development environment, beware that earlier mentioned libraries will attempt to fix your markup (that's their primary purpose; indentation is a by-product).
Regex
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.
The above is also the reason why Chrome DevTools is not a direct replacement for Dindent.
Use
$indenter = new \Gajus\Dindent\Indenter(); $indenter->indent('[..]');
In the above example, [..] is a placeholder for:
<!DOCTYPE html> <html> <head></head> <body> <script> console.log('te> <st'); function () { test; <!-- <a> --> } </script> <div> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <div><table border="1" style="background-color: red;"><tr><td>A cell test!</td> <td colspan="2" rowspan="2"><table border="1" style="background-color: green;"><tr> <td>Cell</td><td colspan="2" rowspan="2"></td></tr><tr> <td><input><input><input></td></tr><tr><td>Cell</td><td>Cell</td><td>Ce ll</td></tr></table></td></tr><tr><td>Test <span>Ce ll</span></td></tr><tr><td>Cell</td><td>Cell</td><td>Cell</td></tr></table></div></div> </body> </html>
Dindent will convert it to:
<!DOCTYPE html> <html> <head></head> <body> <script> console.log('te> <st'); function () { test; <!-- <a> --> } </script> <div> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <div> <table border="1" style="background-color: red;"> <tr> <td>A cell test!</td> <td colspan="2" rowspan="2"> <table border="1" style="background-color: green;"> <tr> <td>Cell</td> <td colspan="2" rowspan="2"></td> </tr> <tr> <td> <input> <input> <input> </td> </tr> <tr> <td>Cell</td> <td>Cell</td> <td>Ce ll</td> </tr> </table> </td> </tr> <tr> <td>Test <span>Ce ll</span></td> </tr> <tr> <td>Cell</td> <td>Cell</td> <td>Cell</td> </tr> </table> </div> </div> </body> </html>
Options
Indenter constructor accepts the following options that control indentation:
| Name | Description |
|---|---|
indentation_character |
Character(s) used for indentation. Defaults to 4 whitespace characters. |
Set element type
HTML elements are either "inline" elements or "block-level" elements.
An inline element occupies only the space bounded by the tags that define the inline element. The following example demonstrates the inline element's influence:
<p>This is an <span>inline</span> element within a block element.</p>
A block-level element occupies the entire space of its parent element (container), thereby creating a "block." Browsers typically display the block-level element with a new line both before and after the element. The following example demonstrates the block-level element's influence:
<div> <p>This is a block element within a block element.</p> </div>
Dindent identifies the following elements as "inline":
- b, big, i, small, tt
- abbr, acronym, cite, code, dfn, em, kbd, strong, samp, var
- a, bdo, br, img, span, sub, sup
This is a subset of the inline elements defined in the MDN.
All other elements are treated as block.
You can set element type to either block or inline using setElementType method:
$indenter = new \Gajus\Dindent\Indenter(); /** * @param string $element_name Element name, e.g. "b". * @param ELEMENT_TYPE_BLOCK|ELEMENT_TYPE_INLINE $type * @return null */ $indenter->setElementType('foo', \Gajus\Dindent\Indenter::ELEMENT_TYPE_BLOCK); $indenter->setElementType('bar', \Gajus\Dindent\Indenter::ELEMENT_TYPE_INLINE);
CLI
Dindent can be used via the CLI script ./bin/dindent.php.
php ./bin/dindent.php
Indent HTML.
Options:
--input=./input_file.html
Input file
--indentation_character=" "
Character(s) used for indentation. Defaults to 4 whitespace characters.
--inline
A list of comma separated "inline" element names.
--block
A list of comma separated "block" element names.
Examples:
./dindent.php --input="./input.html"
Indent "input.html" file and print the output to STDOUT.
./dindent.php --input="./input.html" | tee ./output.html
Indent "input.html" file and dump the output to "output.html".
./dindent.php --input="./input.html" --indentation_character="\t"
Indent "input.html" file using tab to indent the markup.
./dindent.php --input="./input.html" --inline="div,p"
Indent "input.html" file treating <div> and <p> elements as inline.
./dindent.php --input="./input.html" --block="span,em"
Indent "input.html" file treating <span> and <em> elements as block.
Known issues
- Does not treat comments nicely and IE conditional blocks.
Installation
The recommended way to use Dindent is through Composer.
{
"require": {
"gajus/dindent": "2.0.*"
}
}
gajus/dindent 适用场景与选型建议
gajus/dindent 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.03M 次下载、GitHub Stars 达 178, 最近一次更新时间为 2014 年 02 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「html」 「format」 「indent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gajus/dindent 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gajus/dindent 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gajus/dindent 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Provides a simple decimal/roman number converter
Softonic OpenApi validation formats extension
HTML indentation library for development and testing. Forked from https://github.com/gajus/dindent
Tools to convert between .NET and PHP date formats
PSR-3 logger for adding colors and indentation on PHP CLI output.
统计信息
- 总下载量: 2.03M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 179
- 点击次数: 17
- 依赖项目数: 50
- 推荐数: 2
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2014-02-19