Skip to content

Commit

Permalink
feat(html): html template automatic compress
Browse files Browse the repository at this point in the history
1. Introduces the core code of Google html compressor
2. Custom Beetl engine to compress html
3. After Beetl reads the template, the HTML is automatically compressed before parameters are injected

Closes #991
  • Loading branch information
jasonkung22 committed Dec 13, 2024
1 parent bd314f3 commit 99d0f59
Show file tree
Hide file tree
Showing 8 changed files with 3,024 additions and 11 deletions.
29 changes: 24 additions & 5 deletions src/main/java/com/ly/doc/utils/BeetlTemplateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@

import com.ly.doc.constants.DocGlobalConstants;
import com.power.common.util.FileUtil;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Resource;
import org.beetl.core.Template;
import org.beetl.core.engine.FastRuntimeEngine;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.core.statement.Program;

/**
* Beetl template handle util
Expand All @@ -40,6 +43,8 @@
*/
public class BeetlTemplateUtil {

private final static String HTML_SUFFIX = ".html";

/**
* private constructor
*/
Expand All @@ -58,6 +63,9 @@ public static Template getByName(String templateName) {
Configuration cfg = Configuration.defaultConfiguration();
cfg.add("/smart-doc-beetl.properties");
GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
if (templateName.endsWith(HTML_SUFFIX)) {
gt.setEngine(new HtmlCompressTemplateEngine());
}
return gt.getTemplate(templateName);
}
catch (IOException e) {
Expand Down Expand Up @@ -105,4 +113,15 @@ private static GroupTemplate getGroupTemplate(String path) {
}
}

public static class HtmlCompressTemplateEngine extends FastRuntimeEngine {

@Override
public Program createProgram(Resource rs, Reader reader, Map<Integer, String> textMap, String cr,
GroupTemplate gt) {
textMap.replaceAll((k, v) -> HtmlCompressorUtil.compress(textMap.get(k)));
return super.createProgram(rs, reader, textMap, cr, gt);
}

}

}
Loading

0 comments on commit 99d0f59

Please sign in to comment.