axy/min-html

压缩 HTML

0.1.1 2015-12-08 13:48 UTC

This package is auto-updated.

Last update: 2024-09-08 02:11:22 UTC


README

压缩 HTML.

Latest Stable Version Minimum PHP Version Build Status Coverage Status License

  • 该库不需要任何依赖。
  • 在 PHP 5.4+、PHP 7、HHVM(Linux 上)、PHP 5.5(Windows 上)进行了测试。
  • 安装: composer require axy/min-html.
  • 许可证: MIT.

压缩

简单地删除了行首和行尾的缩进。有一些例外

  • 一些标签内部(如 <pre><textarea>)的缩进是相关的。
  • 一些标签的内容可以以特殊方式处理(例如,压缩 <script><style>)。

示例

源内容

<head>
    <title>Test</title>
    <script>
    var x = 2 + 2;
    console.log(x);
    </script>
</head>
<body>
    <h1>Test</h1>
    <p>
    This is
    example
    of HTML compression.
    </p>
    <pre>
    Content of PRE
    is PRE
    </pre>
</body>

压缩内容

<head>
<title>Test</title>
<script>var x=2+2;console.log(x);</script>
</head>
<body>
<h1>Test</h1>
<p>
This is
example
of HTML compression.
</p>
<pre>
    Content of PRE
    is PRE
</pre>
</body>

API

该库定义了一个公共类 axy\min\html\HTMLMinifier

方法

  • __construct(string $content [, array $tags])
  • run(void): string
  • getOriginContent(void): string
  • getCompressedContent(void): string
  • getTags(void): array

静态方法

  • compress(string $content [, array $tags]): string
  • compressFromFile(string $content [, array $tags]): string
  • compressFile(string $source, string $destination [, array $tags]): string
  • getDefaultsTags([array $tags]): array

示例

使用静态方法

use axy\min\html\HTMLMinifier;

$source = 'index.html';
$destination = 'index.min.html';

HTMLMinifier::compressFile($source, $destination);

不使用静态方法

$min = new HTMLMinifier($content);
$min->run();
echo $min->getCompressedContent();

标签

可选数组 tags 指定了如何处理标签的内容。

默认值是

[
    'pre' => true,
    'textarea' => true,
]

参数 $tags 与默认值合并。

TRUE - 不更改。或回调。

$tags = [
    'script' => function ($content) {
        return JSMinify::minify($content);
    },
    'textarea' => null, // Remove rule for TEXTAREA
    'style' => true, // Content of STYLE does not change
];

HTMLMinifier::compressFile($source, $destination, $tags);