Java小强个人技术博客站点    手机版
当前位置: 首页 >> 开源 >> HTML压缩库HtmlCompressor

HTML压缩库HtmlCompressor

24700 开源 | 2021-5-15

HTML 压缩库 HtmlCompressor

HtmlCompressor 是一个 Java 类库,用来对 HTML 内容进行压缩。HtmlCompressor 会删除 HTML 中无用的空格、空行、注释等内容。


崔素强博客使用


官方说明地址,国内看不了想看下载TXT:

https://code.google.com/archive/p/htmlcompressor/wikis/Documentation.wiki 

htmlcompressor Documentation wiki.txt


Maven引入:

<dependency>
	<groupId>com.googlecode.htmlcompressor</groupId>
	<artifactId>htmlcompressor</artifactId>
	<version>1.5.2</version>
</dependency>


htmlcompressor最新版本是v.1.5.3,如果是Maven的话用1.5.2


示例代码与说明

HtmlCompressor compressor = new HtmlCompressor();

compressor.setEnabled(true);                   // 如果false,则关闭所有压缩(默认值为true)
compressor.setRemoveComments(true);            // 如果false保留HTML注释(默认值为true)
compressor.setRemoveMultiSpaces(true);         // 如果false保留多个空格字符(默认值为true)
compressor.setRemoveIntertagSpaces(true);      // 删除iter标记空白字符
compressor.setRemoveQuotes(true);              // 删除不必要的标记属性引号
compressor.setSimpleDoctype(true);             // 简化现有doctype
compressor.setRemoveScriptAttributes(true);    // 从script标签中移除可选属性
compressor.setRemoveStyleAttributes(true);     // 从style标签中移除可选属性
compressor.setRemoveLinkAttributes(true);      // 从link标签中移除可选属性
compressor.setRemoveFormAttributes(true);      // 从form标签中移除可选属性
compressor.setRemoveInputAttributes(true);     // 从input标签中移除可选属性
compressor.setSimpleBooleanAttributes(true);   // 从布尔标签属性中移除值
compressor.setRemoveJavaScriptProtocol(true);  // 从内联事件处理程序中删除“javascript:”
compressor.setRemoveHttpProtocol(true);        // 将“http://”替换为“//”内部标记属性
compressor.setRemoveHttpsProtocol(true);       // 将“https://”替换为“//”内部标记属性
compressor.setPreserveLineBreaks(true);        // 保留原始换行符
compressor.setRemoveSurroundingSpaces("br,p"); // 删除提供的标记周围的空格
compressor.setCompressCss(true);               // 压缩内联css 
compressor.setCompressJavaScript(true);        // 压缩内联js
compressor.setYuiCssLineBreak(80);             // Yahoo YUI压缩机的换行参数
compressor.setYuiJsDisableOptimizations(true); // 禁用Yahoo YUI压缩器的优化参数
compressor.setYuiJsLineBreak(-1);              // Yahoo YUI压缩机的换行参数
compressor.setYuiJsNoMunge(true);              //--nomunge param for Yahoo YUI Compressor 
compressor.setYuiJsPreserveAllSemiColons(true);// 为Yahoo YUI Compressor保留半参数

// 使用Google闭包编译器进行javascript压缩
compressor.setJavaScriptCompressor(new ClosureJavaScriptCompressor(CompilationLevel.SIMPLE_OPTIMIZATIONS));
// 使用您自己的css压缩程序实现
compressor.setCssCompressor(new MyOwnCssCompressor());

String compressedHtml = compressor.compress(html);


还有Maven插件,Maven HTMLCompressor Plugin

https://github.com/alextunyk/htmlcompressor-maven-plugin 


Overview

Maven HTMLCompressor Plugin allows to compress HTML/XML files by adding a few lines to the pom file. This plugin uses htmlcompressor library.

Getting started

The simplest way to start using this plugin is:

1.Enable plugin in your pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>com.github.hazendaz.maven</groupId>
            <artifactId>htmlcompressor-maven-plugin</artifactId>
            <version>1.4.0</version>
            <configuration>
                <goalPrefix>htmlcompressor</goalPrefix>
            </configuration>
        </plugin>
    </plugins>
</build>

2.Put XML and HTML files under src/main/resources into any underlying structure as HTMLCompressor will recursively process files

3.For HTML compression, create integration.js file under src/main/resources where html is stored with the contents like below. It will integrate HTML templates into JavaScript (%s will be replaced with JSON object and copied to the target folder).

var htmlTemplatesInjector = {
    htmlTemplates: %s
};

4.Run maven goals:

mvn htmlcompressor:html
mvn htmlcompressor:xml

5.Check the target folder for output where resources are stored.

Here is demo you can download


END


推荐您阅读更多有关于“ maven 压缩 html HtmlCompressor ”的文章

上一篇:定制js的alert和confirm 下一篇:SpringBoot和WebSocket开发的聊天室

猜你喜欢

发表评论: