From 38493f623fd86349f7d47135dcce913928ef71a1 Mon Sep 17 00:00:00 2001 From: Cyril de Catheu Date: Fri, 1 Dec 2023 23:13:19 +0100 Subject: [PATCH 1/4] [core] replace thymeleaf by jte --- jnotebook-core/pom.xml | 22 +- .../catheu/jnotebook/NotebookRenderer.java | 8 +- .../jnotebook/server/HtmlTemplateEngine.java | 49 +- .../jnotebook/server/InteractiveServer.java | 9 +- .../catheu/jnotebook/utils/JavaUtils.java | 6 + .../src/main/resources/frontend/index.html | 772 ------------------ pom.xml | 15 +- 7 files changed, 68 insertions(+), 813 deletions(-) delete mode 100644 jnotebook-core/src/main/resources/frontend/index.html diff --git a/jnotebook-core/pom.xml b/jnotebook-core/pom.xml index 7c21c63..4dc29db 100644 --- a/jnotebook-core/pom.xml +++ b/jnotebook-core/pom.xml @@ -61,10 +61,9 @@ io.undertow undertow-websockets-jsr - - org.thymeleaf - thymeleaf + gg.jte + jte @@ -172,6 +171,23 @@ + + + gg.jte + jte-maven-plugin + + ${basedir}/src/main/jte + Html + + + + generate-sources + + generate + + + + diff --git a/jnotebook-core/src/main/java/tech/catheu/jnotebook/NotebookRenderer.java b/jnotebook-core/src/main/java/tech/catheu/jnotebook/NotebookRenderer.java index fc3bf8f..ed32a3b 100644 --- a/jnotebook-core/src/main/java/tech/catheu/jnotebook/NotebookRenderer.java +++ b/jnotebook-core/src/main/java/tech/catheu/jnotebook/NotebookRenderer.java @@ -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 { @@ -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); } diff --git a/jnotebook-core/src/main/java/tech/catheu/jnotebook/server/HtmlTemplateEngine.java b/jnotebook-core/src/main/java/tech/catheu/jnotebook/server/HtmlTemplateEngine.java index a8e3fdb..cbc3743 100644 --- a/jnotebook-core/src/main/java/tech/catheu/jnotebook/server/HtmlTemplateEngine.java +++ b/jnotebook-core/src/main/java/tech/catheu/jnotebook/server/HtmlTemplateEngine.java @@ -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 actual generated 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 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); - } } diff --git a/jnotebook-core/src/main/java/tech/catheu/jnotebook/server/InteractiveServer.java b/jnotebook-core/src/main/java/tech/catheu/jnotebook/server/InteractiveServer.java index ddbcfc4..9f37731 100644 --- a/jnotebook-core/src/main/java/tech/catheu/jnotebook/server/InteractiveServer.java +++ b/jnotebook-core/src/main/java/tech/catheu/jnotebook/server/InteractiveServer.java @@ -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 { @@ -89,19 +85,16 @@ public void run() { private static class TemplatedHttpHandler implements HttpHandler { final HtmlTemplateEngine templateEngine; - final Map 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); } diff --git a/jnotebook-core/src/main/java/tech/catheu/jnotebook/utils/JavaUtils.java b/jnotebook-core/src/main/java/tech/catheu/jnotebook/utils/JavaUtils.java index f5c1863..4bd4809 100644 --- a/jnotebook-core/src/main/java/tech/catheu/jnotebook/utils/JavaUtils.java +++ b/jnotebook-core/src/main/java/tech/catheu/jnotebook/utils/JavaUtils.java @@ -11,6 +11,12 @@ public class JavaUtils { + public static boolean RUN_IN_JAR = JavaUtils.class.getProtectionDomain() + .getCodeSource() + .getLocation() + .toString() + .endsWith(".jar"); + public static Optional optional(final T obj) { return Optional.ofNullable(obj); } diff --git a/jnotebook-core/src/main/resources/frontend/index.html b/jnotebook-core/src/main/resources/frontend/index.html deleted file mode 100644 index c54e781..0000000 --- a/jnotebook-core/src/main/resources/frontend/index.html +++ /dev/null @@ -1,772 +0,0 @@ - - - - - - - - - - jnotebook - - - - - - - - - - - - - - - - - - - - - - - -
- -
- -
- -
-
-
-
-
-
-
-

Welcome to jnotebook. Learn more on jnotebook.catheu.tech.

-

Edit a .jsh file in your [[${jnb_config.notebookPath}]] folder to make your notebook appear.

-
-
-
-
- -
- - diff --git a/pom.xml b/pom.xml index e2c783f..3786367 100644 --- a/pom.xml +++ b/pom.xml @@ -40,6 +40,9 @@ 3.1.2 + + 3.1.5 + 2.3.5.Final 4.12.0 @@ -108,9 +111,9 @@
- org.thymeleaf - thymeleaf - 3.1.1.RELEASE + gg.jte + jte + ${jte.version} @@ -419,6 +422,12 @@ + + + gg.jte + jte-maven-plugin + ${jte.version} + From 1f62a7ea7a8c5becb8a25134aa7be1c35d83bc38 Mon Sep 17 00:00:00 2001 From: Cyril de Catheu Date: Fri, 1 Dec 2023 23:18:03 +0100 Subject: [PATCH 2/4] clean --- license_style_thymeleaf_no_render.xml | 21 --------------------- pom.xml | 4 ---- 2 files changed, 25 deletions(-) delete mode 100644 license_style_thymeleaf_no_render.xml diff --git a/license_style_thymeleaf_no_render.xml b/license_style_thymeleaf_no_render.xml deleted file mode 100644 index a4c9438..0000000 --- a/license_style_thymeleaf_no_render.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - <!--/*EOL - - EOL*/--> - <!--/\*EOL - EOL\*/--> - true - true - - diff --git a/pom.xml b/pom.xml index 3786367..4c29a19 100644 --- a/pom.xml +++ b/pom.xml @@ -344,14 +344,10 @@ **/tmp/** - - ${maven.multiModuleProjectDirectory}/license_style_thymeleaf_no_render.xml - JAVADOC_STYLE JAVADOC_STYLE JAVADOC_STYLE - THYMELEAF_NO_RENDER From cd06b8bac986ce332a1eff18ae818e144a444881 Mon Sep 17 00:00:00 2001 From: Cyril de Catheu Date: Fri, 1 Dec 2023 23:20:45 +0100 Subject: [PATCH 3/4] add jte folder --- jnotebook-core/src/main/jte/index.jte | 771 ++++++++++++++++++++++++++ 1 file changed, 771 insertions(+) create mode 100644 jnotebook-core/src/main/jte/index.jte diff --git a/jnotebook-core/src/main/jte/index.jte b/jnotebook-core/src/main/jte/index.jte new file mode 100644 index 0000000..ab831a3 --- /dev/null +++ b/jnotebook-core/src/main/jte/index.jte @@ -0,0 +1,771 @@ +@import tech.catheu.jnotebook.Main.InteractiveConfiguration +@import tech.catheu.jnotebook.server.HtmlTemplateEngine.TemplateModel +@import java.util.Optional +@param TemplateModel model + + + + + + + + jnotebook + + + + + + + + + + + + + + + + + + + + + + + +
+ + @if(model.interactive()) +
+ +
+ @endif + +
+ @if(model.render() != null) +
+ $unsafe{model.render()} +
+ @else +
+
+
+
+

Welcome to jnotebook. Learn more on jnotebook.catheu.tech. +

+

Edit a .jsh file in your ${((InteractiveConfiguration) model.config()).notebookPath} folder to + make your notebook appear.

+
+
+
+ @endif +
+ @if(model.interactive()) + + @endif +
+ + From fc7e29fc6a1b88adb1121f7cff7c190e5cf53dbf Mon Sep 17 00:00:00 2001 From: Cyril de Catheu Date: Fri, 1 Dec 2023 23:31:19 +0100 Subject: [PATCH 4/4] clean --- .../java/tech/catheu/jnotebook/server/HtmlTemplateEngine.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jnotebook-core/src/main/java/tech/catheu/jnotebook/server/HtmlTemplateEngine.java b/jnotebook-core/src/main/java/tech/catheu/jnotebook/server/HtmlTemplateEngine.java index cbc3743..8d876a1 100644 --- a/jnotebook-core/src/main/java/tech/catheu/jnotebook/server/HtmlTemplateEngine.java +++ b/jnotebook-core/src/main/java/tech/catheu/jnotebook/server/HtmlTemplateEngine.java @@ -35,7 +35,7 @@ public HtmlTemplateEngine() { } } - // render is the actual generated html + // 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);