From 4ef2ef2ca4a36d6be16576db141940b286c733e8 Mon Sep 17 00:00:00 2001 From: Guilherme I F L Weizenmann Date: Wed, 29 Aug 2018 23:19:59 -0300 Subject: [PATCH] Add simple server for prototyping inspired by handlebars-proto --- pom.xml | 69 ++++++++++-- .../de/neuland/jade4j/proto/Jade4JProto.java | 101 ++++++++++++++++++ src/test/resources/proto/index.jade | 12 +++ src/test/resources/proto/index.yml | 7 ++ 4 files changed, 180 insertions(+), 9 deletions(-) create mode 100644 src/main/java/de/neuland/jade4j/proto/Jade4JProto.java create mode 100644 src/test/resources/proto/index.jade create mode 100644 src/test/resources/proto/index.yml diff --git a/pom.xml b/pom.xml index 873a157e..4a5d2807 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 de.neuland-bfi jade4j @@ -30,8 +31,8 @@ https://github.com/neuland/jade4j scm:git:git://github.com/neuland/jade4j.git scm:git:git@github.com:neuland/jade4j.git - HEAD - + HEAD + @@ -54,10 +55,18 @@ https://github.com/chbloemer chbloemer + + Guilherme I F L Weizenmann + https://github.com/giflw + giflw + UTF-8 + 1.7 + ${java.version} + ${java.version} @@ -92,8 +101,36 @@ + + jade4j-proto + + + + org.codehaus.mojo + appassembler-maven-plugin + 1.10 + + + + de.neuland.jade4j.proto.Jade4JProto + jade4j-proto + + + + + + package + + assemble + + + + + + + - + @@ -164,7 +201,9 @@ deploy deploy - deploy + + deploy + @@ -218,11 +257,23 @@ 1.4.2 - com.vladsch.flexmark - flexmark-all - 0.28.2 + com.vladsch.flexmark + flexmark-all + 0.28.2 + + + + org.yaml + snakeyaml + 1.22 + true + + + com.sparkjava + spark-core + 1.1.1 + true - org.hamcrest diff --git a/src/main/java/de/neuland/jade4j/proto/Jade4JProto.java b/src/main/java/de/neuland/jade4j/proto/Jade4JProto.java new file mode 100644 index 00000000..2d55e44f --- /dev/null +++ b/src/main/java/de/neuland/jade4j/proto/Jade4JProto.java @@ -0,0 +1,101 @@ +package de.neuland.jade4j.proto; + +import de.neuland.jade4j.Jade4J; +import org.yaml.snakeyaml.Yaml; +import spark.Request; +import spark.Response; +import spark.Route; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.Collections; +import java.util.Map; + +import static spark.Spark.*; + +public class Jade4JProto { + + private static final String[] MODEL_EXTS = {"yml", "json"}; + + + public static void main(String[] args) { + + if (args.length == 1 && (args[0].equals("-h") || args[0].equals("--help"))) { + help(); + System.exit(0); + } + + int port = 4567; + String baseDirAux = System.getProperty("user.dir"); + + for (int i = 0; i < args.length; i++) { + String arg = args[i]; + if ("--port".equals(arg)) { + i++; + port = Integer.parseInt(args[i]); + } else { + baseDirAux = arg; + } + } + + final String baseDir = Paths.get(baseDirAux).normalize().toAbsolutePath().toString(); + + System.out.println("=========== Jade4J :: PROTO ==========="); + System.out.println("PORT: " + port); + System.out.println("BASE DIR: " + baseDir); + System.out.println("======================================="); + + setIpAddress("0.0.0.0"); + setPort(port); + + get(new Route("/*") { + @Override + public Object handle(Request request, Response response) { + String relativePath = request.pathInfo(); + relativePath = relativePath.equals("/") ? "/index" : relativePath; + System.out.println("RENDERING: " + relativePath); + String absolutePath = baseDir + relativePath; + String jadeFile = absolutePath + ".jade"; + System.out.println("\t\tJADE: " + jadeFile); + try { + response.type("text/html"); + return Jade4J.render(jadeFile, load(absolutePath), true); + } catch (IOException e) { + response.type("text/plain"); + return "Error: " + e.getMessage(); + } + } + }); + } + + private static void help() { + System.out.println("jade4j-proto [directory] [--port ]"); + } + + private static Map load(String name) { + Map model = Collections.emptyMap(); + for (String ext : MODEL_EXTS) { + File file = new File(name + "." + ext); + if (file.exists()) { + System.out.println("\t\tMODEL: " + file); + model = load(file); + } + } + if (model.isEmpty()) { + System.out.println("\t\tModel is empty!"); + } + return model; + } + + private static Map load(File file) { + try { + Yaml yaml = new Yaml(); + return yaml.load(new FileReader(file)); + } catch (Exception ex) { + ex.printStackTrace(); + } + return Collections.emptyMap(); + } +} diff --git a/src/test/resources/proto/index.jade b/src/test/resources/proto/index.jade new file mode 100644 index 00000000..341b375f --- /dev/null +++ b/src/test/resources/proto/index.jade @@ -0,0 +1,12 @@ +doctype html +html + head + title= pageName + link(rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css") + body + ol#books + each book in books + li + strong #{book.author} + | + | #{book.title} diff --git a/src/test/resources/proto/index.yml b/src/test/resources/proto/index.yml new file mode 100644 index 00000000..e931122a --- /dev/null +++ b/src/test/resources/proto/index.yml @@ -0,0 +1,7 @@ +pageName: Jade4J +books: +- author: Foo + title: T +- author: Bar + title: TT +