- 2024-12-05 11:58:30
- 9024 热度
- 0 评论
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.
END
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Spring Cloud(82)
- Java(82)
- Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- Dalston(24)
- IDE(24)
- mongoDB(22)
- MVC(22)
- JDBC(22)
- IDEA(22)
- Web(21)
- CLI(20)
- Alibaba(19)
- SpringMVC(19)
- Docker(17)
- SpringBoot(17)
- Git(16)
- Eclipse(16)
- Vue(16)
- JPA(15)
- Apache(15)
- ORA(15)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- Mybatis(14)
- Oracle(14)
- jdk(14)
- OAuth(13)
- Nacos(13)
- Pro(13)
- XML(13)
- JdbcTemplate(13)
- JSON(12)
- OAuth2(12)
- Data(12)
- int(11)
- Myeclipse(11)
- stream(11)
- not(10)
- Bug(10)
- Hystrix(9)
- ast(9)
- maven(9)
- Map(9)
- Swagger(8)
- APP(8)
- Bit(8)
- API(8)
- session(8)
- Window(8)
- windows(7)
- too(7)
- HTML(7)
- Github(7)
- JavaMail(7)
- Cache(7)
- File(7)
- IntelliJ(7)
- mail(7)
- Server(6)
- nginx(6)
- jar(6)
- ueditor(6)
- ehcache(6)
- UDP(6)
- RabbitMQ(6)
- and(6)
- star(6)
- Excel(6)
- Log4J(6)
- pushlet(6)
- apt(6)
- Freemarker(6)
- read(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- are(5)
- SVN(5)
- for(5)
- DOM(5)
- Sentinel(5)
- the(5)
- JWT(5)
- rdquo(5)
- PHP(5)
- Struts(5)
- string(5)
- script(5)