Created
June 24, 2014 04:22
-
-
Save jatazoulja/d339dcec2da5f281a964 to your computer and use it in GitHub Desktop.
HTML minifier that includes tpl files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> | |
<component name="NewModuleRootManager" inherit-compiler-output="false"> | |
<output url="file://$MODULE_DIR$/target/classes" /> | |
<output-test url="file://$MODULE_DIR$/target/test-classes" /> | |
<content url="file://$MODULE_DIR$"> | |
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> | |
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" /> | |
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> | |
<excludeFolder url="file://$MODULE_DIR$/target" /> | |
</content> | |
<orderEntry type="inheritedJdk" /> | |
<orderEntry type="sourceFolder" forTests="false" /> | |
<orderEntry type="module-library"> | |
<library> | |
<CLASSES> | |
<root url="jar://$MODULE_DIR$/../apacheds-all-1.5.4.jar!/" /> | |
</CLASSES> | |
<JAVADOC /> | |
<SOURCES /> | |
</library> | |
</orderEntry> | |
<orderEntry type="library" name="apacheds-all-1.5.4" level="project" /> | |
<orderEntry type="library" name="Maven: org.apache.maven:maven-plugin-api:2.0" level="project" /> | |
<orderEntry type="library" name="Maven: org.apache.commons:commons-io:1.3.2" level="project" /> | |
</component> | |
</module> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<artifactId>htmlminify</artifactId> | |
<groupId>com.asurion.web.htmlminify</groupId> | |
<version>1.0-SNAPSHOT</version> | |
<packaging>maven-plugin</packaging> | |
<properties> | |
<slf4jVersion>1.6.1</slf4jVersion> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.apache.maven</groupId> | |
<artifactId>maven-plugin-api</artifactId> | |
<version>2.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.commons</groupId> | |
<artifactId>commons-io</artifactId> | |
<version>1.3.2</version> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.asurion.ama; | |
import org.apache.commons.io.FileUtils; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.Collection; | |
import java.util.HashMap; | |
import java.util.Map; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: PeterEman.Abastillas | |
* Date: 6/23/14 | |
* Time: 1:36 PM | |
* To change this template use File | Settings | File Templates. | |
*/ | |
public class FileMinify { | |
private String rootDirPath; | |
private String[] fileExt; | |
private boolean recursive; | |
private String fileEncoding = "utf-8"; | |
public FileMinify(String rootDir, String[] fileExt) throws IOException { | |
this.setRootDirPath(rootDir); | |
this.fileExt = fileExt; | |
this.recursive = true; | |
} | |
public void setRootDirPath(String rootDirPath) throws IOException { | |
File file = new File(rootDirPath); | |
this.rootDirPath = file.getCanonicalPath().replaceAll("\\\\", "/").replaceAll("/$", ""); | |
} | |
public Map<String, String> getFiles() throws IOException { | |
Map<String, String> map = new HashMap<String, String>(); | |
File rootDir = new File(rootDirPath); | |
Collection<File> files = FileUtils.listFiles(rootDir, fileExt, recursive); | |
int truncationIndex = 0; | |
for (File file : files) { | |
String normalizedFilePath = file.getCanonicalPath().replaceAll("\\\\", "/"); | |
if (truncationIndex == 0) { | |
truncationIndex = normalizedFilePath.indexOf(rootDirPath) + rootDirPath.length() + 1; | |
} | |
String key = normalizedFilePath.substring(truncationIndex); | |
String value = FileUtils.readFileToString(file, fileEncoding); | |
System.out.print("\n [OPENING]: " + key); | |
System.out.print("\n[FSIZE]: " + file.length()); | |
map.put(key, value); | |
} | |
return map; | |
} | |
public void writeFiles(Map<String, String> map, String targetDir) throws IOException { | |
for(String key : map.keySet()) { | |
File file = new File(targetDir + "/" + key); | |
FileUtils.writeStringToFile(file, map.get(key), fileEncoding); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.asurion.ama; | |
import java.util.Map; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: PeterEman.Abastillas | |
* Date: 6/23/14 | |
* Time: 1:36 PM | |
* To change this template use File | Settings | File Templates. | |
*/ | |
public class HtmlMinifier { | |
private static final String[] FILE_EXT = {"htm", "html", "tpl"}; | |
private String srcFolder; | |
private String[] pattern = {"\n", "\r", "\t", "\\s+"}; | |
private String targetFolder; | |
private String Logs = ""; | |
public HtmlMinifier(String srcFolder, String targetFolder, String[] pattern) throws Exception { | |
this.srcFolder = srcFolder; | |
this.targetFolder = targetFolder; | |
this.pattern = pattern; | |
} | |
public void compress() throws Exception { | |
String[] fileExt = FILE_EXT; | |
FileMinify fileTool = new FileMinify(srcFolder, FILE_EXT); | |
Map<String, String> map = fileTool.getFiles(); | |
System.out.print("\r=============================================================\n"); | |
for(String key : map.keySet()) { | |
map.put(key, compressHtml(map.get(key))); | |
} | |
fileTool.writeFiles(map, targetFolder); | |
System.out.print("\r=============================================================\n"); | |
} | |
/** | |
* Process and minify html string; | |
* @param htmlString | |
* @return String | |
*/ | |
public String compressHtml(String htmlString) { | |
/* | |
for( int i = 0; i < pattern.length - 1; i++) { | |
} | |
*/ | |
htmlString = htmlString.replaceAll("//.*?s\r\n", ""); | |
htmlString = htmlString.replaceAll("(\\/\\*[\\w\\'\\s\\r\\n\\*]*\\*\\/)|(\\/\\/[\\w\\s\\']*)|(\\<![\\-\\-\\s\\w\\>\\/]*\\>)", ""); | |
htmlString = htmlString.replaceAll("\r\n", ""); | |
htmlString = htmlString.replaceAll("\\s+", " "); | |
htmlString = htmlString.replaceAll("> <", "><"); | |
htmlString = htmlString.replaceAll("(?s)<!--.*?-->", ""); | |
return htmlString; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.asurion.ama; | |
import org.apache.maven.plugin.AbstractMojo; | |
import org.apache.maven.plugin.MojoExecutionException; | |
import org.apache.maven.plugin.MojoFailureException; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: PeterEman.Abastillas | |
* Date: 6/23/14 | |
* Time: 1:15 PM | |
* To change this template use File | Settings | File Templates. | |
*/ | |
/** | |
* Goal which touches a timestamp file. | |
* | |
* @goal compress | |
* | |
* @phase package | |
*/ | |
public class HtmlMinify extends AbstractMojo { | |
/** | |
* @parameter expression="${HtmlMinify.pattern}" default-value="{"\n", "\r", "\t", "\n", "\\s+"}" | |
*/ | |
private String[] pattern = {"\n", "\r", "\t", "\n", "\\s+"}; | |
/** | |
* source folder where html files are located. | |
* | |
* @parameter expression="${HtmlMinify.srcFolder}" default-value="${basedir}/src/main/webapp" | |
*/ | |
private String srcFolder = "src/main/webapp"; | |
/** | |
* target folder where compressed html files will be placed. | |
* | |
* @parameter expression="${htmlcompressor.targetFolder}" default-value="${project.build.directory}-work/" | |
*/ | |
private String targetFolder = "/"; | |
@Override | |
public void execute() throws MojoExecutionException, MojoFailureException { | |
//To change body of implemented methods use File | Settings | File Templates. | |
getLog().info("Initiating minification"); | |
getLog().info("================================================"); | |
getLog().info("Target Directory: " + targetFolder); | |
getLog().info("Source Directory: " + srcFolder); | |
getLog().info("================================================"); | |
try { | |
HtmlMinifier minify = new HtmlMinifier(srcFolder, targetFolder, pattern); | |
minify.compress(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
���� 1 � | |