Thursday, 11 July 2024

Java Mustache Example

{{#todos}}

{{#capitalize}}{{title}}{{/capitalize}}

  • Created on {{createdOn}}
  • Lambda: {{#handleDone}}{{doneSince}}{{/handleDone}} :-)
  • Done: {{done}}

{{text}}

{{/todos}} {{^todos}}

No todos found

{{/todos}}
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;
import java.time.Duration;
import java.util.function.Function;

public class HtmlExporter {

    public static class Todo {

        public Todo() {
            createdOn = new Date();
        }

        public Todo(String title, String text) {
            this.title = title;
            this.text = text;
            createdOn = new Date();
        }

        private String title;
        private String text;
        private boolean done = false;

        private Date createdOn;
        private Date completedOn;

        public void markAsDone() {
            done = true;
            completedOn = new Date();
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public boolean getDone() {
            return done;
        }

        public Date getCreatedOn() {
            return createdOn;
        }

        public Date getCompletedOn() {
            return completedOn;
        }

        public void setDone(boolean done) {
            this.done = done;
        }

        public void setCompletedOn(Date completedOn) {
            this.completedOn = completedOn;
        }

        public long doneSince() {
            return done ? Duration
                    .between(createdOn.toInstant(), completedOn.toInstant())
                    .toMinutes() : 0;
        }

        public Function handleDone() {
            return (obj) -> done ? String.format("Done %s minutes ago", obj) : "Not done yet";
        }

    }

    public static class Context {
        List todos = new ArrayList<>();

        public Function capitalize() {
            return (obj) -> ("_" + obj + "_").toUpperCase();
        }

    }

    public String export() {

        MustacheFactory mf = new DefaultMustacheFactory();
        Mustache m = mf.compile("templates/todo.mustache");

        var context = new Context();
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                var todo = new Todo();
                todo.setTitle("Title " + i);
                todo.setText("Text " + i);
                todo.markAsDone();
                context.todos.add(todo);
                continue;
            } else {
                var todo = new Todo();
                todo.setTitle("Title " + i);
                todo.setText("Text " + i);
                context.todos.add(todo);
            }

        }

        StringWriter writer = new StringWriter();
        try {
            m.execute(writer, context).flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return writer.toString();

        // return templateEngine.process("notebook", context);
    }
}

No comments:

Post a Comment

Parse Wikipedia dump

""" This module processes Wikipedia dump files by extracting individual articles and parsing them into a structured format, ...