diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..2ca6797 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -2,5 +2,6 @@ + \ No newline at end of file diff --git a/src/main/java/eu/m724/blog/Main.java b/src/main/java/eu/m724/blog/Main.java index 94cdcf0..8a450b6 100644 --- a/src/main/java/eu/m724/blog/Main.java +++ b/src/main/java/eu/m724/blog/Main.java @@ -3,6 +3,8 @@ package eu.m724.blog; import eu.m724.blog.data.Post; import eu.m724.blog.data.Site; import eu.m724.blog.data.Template; +import in.wilsonl.minifyhtml.Configuration; +import in.wilsonl.minifyhtml.MinifyHtml; import org.apache.commons.io.file.PathUtils; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.lib.RepositoryBuilder; @@ -16,6 +18,12 @@ import java.util.Comparator; import java.util.stream.Collectors; public class Main { + // TODO put this somewhere else + private static final Configuration configuration = new Configuration.Builder() + .setMinifyCss(true) + .setMinifyJs(true) + .build(); + public static void main(String[] args) throws IOException { System.out.println("Hello world!"); @@ -27,7 +35,7 @@ public class Main { // var repository = new RepositoryBuilder() - .setGitDir(workingDirectory.toFile()) + .setGitDir(workingDirectory.resolve(".git").toFile()) .build(); var git = new Git(repository); @@ -83,12 +91,51 @@ public class Main { new Server(outputDirectory).start(); } + private static boolean copyTree(Path srcDir, Path destDir) throws IOException { + if (!Files.isDirectory(srcDir)) { + return false; + } - private static void copyIfExists(Path src, Path dest) throws IOException { - System.out.print(src); + try (var walk = Files.walk(srcDir)) { + for (var src : walk.collect(Collectors.toSet())) { + var rel = srcDir.relativize(src); + var dest = destDir.resolve(rel); - if (Files.exists(src)) { - PathUtils.copyDirectory(src, dest); + if (Files.isRegularFile(src)) { + var parent = dest.getParent(); + + if (!Files.isDirectory(parent)) { + Files.createDirectories(parent); + } + + var type = Files.probeContentType(src); + String content = null; + + if (type.equals("text/html")) { + content = MinifyHtml.minify(Files.readString(src), configuration); + } else if (type.equals("text/css")) { + content = MinifyHtml.minify(Files.readString(src), configuration); + } else if (type.equals("text/javascript")) { + content = MinifyHtml.minify(Files.readString(src), configuration); + } + + if (content != null) { + Files.writeString(dest, content); + } else { + Files.copy(src, dest); + } + } + } + } + + return true; + } + + + private static void copyIfExists(Path srcDir, Path destDir) throws IOException { + System.out.print(srcDir); + + if (copyTree(srcDir, destDir)) { System.out.println(" copied"); } else { System.out.println(" doesn't exist, not copying"); diff --git a/src/main/java/eu/m724/blog/data/Post.java b/src/main/java/eu/m724/blog/data/Post.java index dd8d9cc..bf1ab59 100644 --- a/src/main/java/eu/m724/blog/data/Post.java +++ b/src/main/java/eu/m724/blog/data/Post.java @@ -35,7 +35,7 @@ public record Post( var slug = path.getFileName().toString().split("\\.")[0]; path = Path.of("posts").resolve(path); - var lines = Files.readAllLines(git.getRepository().getDirectory().toPath().resolve(path)); + var lines = Files.readAllLines(git.getRepository().getDirectory().toPath().getParent().resolve(path)); var properties = new HashMap(); diff --git a/src/main/java/eu/m724/blog/data/Site.java b/src/main/java/eu/m724/blog/data/Site.java index 5513fec..06e2508 100644 --- a/src/main/java/eu/m724/blog/data/Site.java +++ b/src/main/java/eu/m724/blog/data/Site.java @@ -1,22 +1,64 @@ package eu.m724.blog.data; -import com.google.gson.Gson; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import org.eclipse.jgit.api.Git; -import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.util.AbstractMap; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; public record Site( String name, String baseUrl, - JsonObject customProperties + Map custom ) { public static Site fromConfig(Git git) throws IOException { - var content = Files.readString(new File(git.getRepository().getDirectory(), "config.json").toPath()); + var content = Files.readString(git.getRepository().getDirectory().toPath().getParent().resolve("config.json")); + var json = JsonParser.parseString(content).getAsJsonObject().asMap(); - return new Gson().fromJson(content, Site.class); + String name = null; + String baseUrl = null; + var custom = new HashMap(); + + for (var entry : json.entrySet()) { + switch (entry.getKey()) { + case "name": + name = entry.getValue().getAsString(); + break; + case "baseUrl": + baseUrl = entry.getValue().getAsString(); + break; + default: + custom.put(entry.getKey(), eToO(entry.getValue())); + } + } + + return new Site( + name, baseUrl, custom + ); + } + + private static Map deep(JsonObject jsonObject) { + return jsonObject.entrySet().stream() + .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), eToO(e.getValue()))) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + } + + private static Object eToO(JsonElement element) { + if (element.isJsonArray()) { + return element.getAsJsonArray().asList().stream().map(Site::eToO).toList(); + } else if (element.isJsonObject()) { + return deep(element.getAsJsonObject()); + } else if (element.isJsonPrimitive()) { + // TODO + } + + return null; } }