Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to minify Thymeleaf generated HTML

Tags:

java

thymeleaf

Does any one know how to minify my Thymleaf generated HTML?

When I used JSP, I could use googleCompressor in this dependency:

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

But now I'ved moved to Thymeleaf and I do not know how minify my Thymeleaf pages.

like image 935
Java jansen Avatar asked Aug 23 '16 10:08

Java jansen


1 Answers

I don't know an existing lib for that unfortunately, but you could create a small Dialect to achieve a simple minification like this:

First create a handler

import java.util.regex.Pattern;

import org.thymeleaf.engine.AbstractTemplateHandler;
import org.thymeleaf.model.IComment;
import org.thymeleaf.model.IText;
import org.thymeleaf.util.StringUtils;

public class SimpleMinifierHandler extends AbstractTemplateHandler {

  private static final Pattern TAB_OR_NEW_LINE = Pattern.compile("[\\t\\n]+\\s");

  @Override
  public void handleComment(IComment comment) {
    // do not print comments at all
  }

  @Override
  public void handleText(IText text) {
    // ignore white spaces, tabs and new lines
    if (!ignorable(text)) {
        super.handleText(text);
    }
  }

  private boolean ignorable(IText text) {
    return StringUtils.isEmptyOrWhitespace(text.getText()) || TAB_OR_NEW_LINE.matcher(text.getText()).matches();
  }
}

Then create a your own Dialect and attach your handler:

import java.util.HashSet;
import java.util.Set;

import org.thymeleaf.dialect.IPostProcessorDialect;
import org.thymeleaf.engine.ITemplateHandler;
import org.thymeleaf.postprocessor.IPostProcessor;
import org.thymeleaf.templatemode.TemplateMode;

public class SimpleMinifierDialect implements IPostProcessorDialect {

  @Override
  public String getName() {
    return "simple-minifier";
  }

  @Override
  public int getDialectPostProcessorPrecedence() {
    return 1000;
  }

  @Override
  public Set<IPostProcessor> getPostProcessors() {
    Set<IPostProcessor> set = new HashSet<>(1);
    set.add(new IPostProcessor() {
        @Override
        public TemplateMode getTemplateMode() {
            return TemplateMode.HTML;
        }

        @Override
        public int getPrecedence() {
            return 1000;
        }

        @Override
        public Class<? extends ITemplateHandler> getHandlerClass() {
            return SimpleMinifierHandler.class;
        }
    });
    return set;
  }
}

Now you can easily add your dialect to your existing TemplateEngine:

@Bean
public TemplateEngine templateEngine() {
    SpringTemplateEngine engine = new SpringTemplateEngine();
    ...
    ...
    ...
    engine.addDialect(new SimpleMinifierDialect());
    return engine;
}

This Dialect does not handle any resources like CSS/JS and it still prints unnecessary characters within the HTML. But I think it shows how you can use Dialects to customize your output.

Hope that helps, Regards

like image 198
martinspielmann Avatar answered Oct 17 '22 20:10

martinspielmann