Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] replace thymeleaf by jte #24

Merged
merged 4 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions jnotebook-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@
<groupId>io.undertow</groupId>
<artifactId>undertow-websockets-jsr</artifactId>
</dependency>

<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<groupId>gg.jte</groupId>
<artifactId>jte</artifactId>
</dependency>

<dependency>
Expand Down Expand Up @@ -172,6 +171,23 @@
</execution>
</executions>
</plugin>
<!-- build pre-compiled templates -->
<plugin>
<groupId>gg.jte</groupId>
<artifactId>jte-maven-plugin</artifactId>
<configuration>
<sourceDirectory>${basedir}/src/main/jte</sourceDirectory>
<contentType>Html</contentType>
</configuration>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,9 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static tech.catheu.jnotebook.server.HtmlTemplateEngine.TEMPLATE_KEY_CONFIG;
import static tech.catheu.jnotebook.server.HtmlTemplateEngine.TEMPLATE_KEY_RENDERED;
import static tech.catheu.jnotebook.utils.JavaUtils.optional;

public class NotebookRenderer {
Expand Down Expand Up @@ -78,10 +75,7 @@ public void render(final Main.RenderConfiguration config) {
final Interpreted interpreted = interpreter.interpret(staticParsing);
final Rendering render = renderer.render(interpreted);
final HtmlTemplateEngine templateEngine = new HtmlTemplateEngine();
String html = templateEngine.render(Map.of(TEMPLATE_KEY_RENDERED,
render.html(),
TEMPLATE_KEY_CONFIG,
config));
String html = templateEngine.render(config, false, render.html());
if (!config.noOptimize) {
html = optimizeHtml(html);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,44 @@
*/
package tech.catheu.jnotebook.server;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;

import java.util.Map;
import gg.jte.CodeResolver;
import gg.jte.ContentType;
import gg.jte.TemplateEngine;
import gg.jte.TemplateOutput;
import gg.jte.output.StringOutput;
import gg.jte.resolve.DirectoryCodeResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.catheu.jnotebook.Main;
import tech.catheu.jnotebook.utils.JavaUtils;

import java.nio.file.Path;

public class HtmlTemplateEngine {

public final static String TEMPLATE_KEY_CONFIG = "jnb_config";
public final static String TEMPLATE_KEY_INTERACTIVE = "jnb_interactive";
public final static String TEMPLATE_KEY_RENDERED = "jnb_rendered";

private static final Logger LOG = LoggerFactory.getLogger(Main.class);
private final TemplateEngine delegate;

public HtmlTemplateEngine() {
ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
// frontend/index.html is the path of the only template used for the moment
resolver.setPrefix("frontend/");
resolver.setSuffix(".html");
if (JavaUtils.RUN_IN_JAR) {
this.delegate = TemplateEngine.createPrecompiled(ContentType.Html);
} else {
LOG.warn("Using dynamic templates. This should only happen in development.");
final CodeResolver codeResolver = new DirectoryCodeResolver(Path.of("jnotebook-core/src/main/jte"));
this.delegate = TemplateEngine.create(codeResolver, ContentType.Html);
}
}

this.delegate = new TemplateEngine();
this.delegate.setTemplateResolver(resolver);
// render is the generated notebook html
public String render(final Main.SharedConfiguration config, final boolean interactive,
final String render) {
final TemplateModel model = new TemplateModel(config, interactive, render);
final TemplateOutput output = new StringOutput();
delegate.render("index.jte", model, output);
return output.toString();
}

public String render(final Map<String, Object> context) {
final Context tlContext = new Context();
context.forEach(tlContext::setVariable);
public record TemplateModel(Main.SharedConfiguration config, boolean interactive, String render) {}

return delegate.process("index", tlContext);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static tech.catheu.jnotebook.server.HtmlTemplateEngine.TEMPLATE_KEY_INTERACTIVE;
import static tech.catheu.jnotebook.server.HtmlTemplateEngine.TEMPLATE_KEY_CONFIG;

public class InteractiveServer {

Expand Down Expand Up @@ -89,19 +85,16 @@ public void run() {
private static class TemplatedHttpHandler implements HttpHandler {

final HtmlTemplateEngine templateEngine;
final Map<String, Object> context;
final Main.InteractiveConfiguration configuration;

TemplatedHttpHandler(final Main.InteractiveConfiguration configuration) {
this.configuration = configuration;
this.templateEngine = new HtmlTemplateEngine();
this.context = Map.of(TEMPLATE_KEY_INTERACTIVE, true,
TEMPLATE_KEY_CONFIG, this.configuration);
}

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
final String html = templateEngine.render(context);
final String html = templateEngine.render(this.configuration, true, null);
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html");
exchange.getResponseSender().send(html);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

public class JavaUtils {

public static boolean RUN_IN_JAR = JavaUtils.class.getProtectionDomain()
.getCodeSource()
.getLocation()
.toString()
.endsWith(".jar");

public static <T> Optional<T> optional(final T obj) {
return Optional.ofNullable(obj);
}
Expand Down
Loading