diff --git a/.gitignore b/.gitignore index 5ff6309..685d039 100644 --- a/.gitignore +++ b/.gitignore @@ -35,4 +35,5 @@ build/ .vscode/ ### Mac OS ### -.DS_Store \ No newline at end of file +.DS_Store +/m724/ diff --git a/pom.xml b/pom.xml index bfaf990..0764683 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,7 @@ 21 21 UTF-8 + eu.m724.blog.Main @@ -26,9 +27,9 @@ 7.1.0.202411261347-r - com.hubspot.jinjava - jinjava - 2.7.4 + io.pebbletemplates + pebble + 3.2.2 com.google.code.gson diff --git a/src/main/java/eu/m724/blog/Main.java b/src/main/java/eu/m724/blog/Main.java index 468235e..3743d10 100644 --- a/src/main/java/eu/m724/blog/Main.java +++ b/src/main/java/eu/m724/blog/Main.java @@ -1,45 +1,89 @@ package eu.m724.blog; -import org.apache.commons.io.FileUtils; +import eu.m724.blog.data.Post; +import eu.m724.blog.data.Site; +import eu.m724.blog.data.Template; +import org.apache.commons.io.file.PathUtils; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.lib.RepositoryBuilder; -import java.io.File; import java.io.IOException; import java.nio.file.FileAlreadyExistsException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.stream.Collectors; public class Main { public static void main(String[] args) throws IOException { System.out.println("Hello world!"); - var workingDirectory = new File("m724"); - var templateDirectory = new File(workingDirectory, "template"); - var outputDirectory = new File(workingDirectory, "generated_out"); + var workingDirectory = Path.of("m724"); + var templateDirectory = workingDirectory.resolve("template"); + var outputDirectory = workingDirectory.resolve("generated_out"); var force = true; // var repository = new RepositoryBuilder() - .setGitDir(workingDirectory) + .setGitDir(workingDirectory.toFile()) .build(); var git = new Git(repository); // - if (outputDirectory.exists()) { + if (outputDirectory.toFile().exists()) { if (force) { - outputDirectory.delete(); + PathUtils.deleteDirectory(outputDirectory); } else { - throw new FileAlreadyExistsException(outputDirectory.getAbsolutePath(), null, "Output directory already exists"); + throw new FileAlreadyExistsException(outputDirectory.toString(), null, "Output directory already exists"); } } - outputDirectory.mkdir(); - FileUtils.copyDirectory(new File(workingDirectory, "assets"), new File(outputDirectory, "assets")); - FileUtils.copyDirectory(new File(templateDirectory, "static"), new File(outputDirectory, "static")); - + var site = Site.fromConfig(git); + var template = new Template(templateDirectory); + + Files.createDirectory(outputDirectory); + + copyIfExists(workingDirectory.resolve("assets"), outputDirectory.resolve("assets")); + copyIfExists(templateDirectory.resolve("static"), outputDirectory.resolve("static")); + + Files.createDirectory(outputDirectory.resolve("post")); + var postDirectory = workingDirectory.resolve("posts"); + try (var stream = Files.walk(postDirectory)) { + for (var path : stream.collect(Collectors.toSet())) { + if (!Files.isRegularFile(path)) continue; + + if (!path.toString().endsWith(".html")) { + System.out.println("Post " + path.getFileName() + ": unsupported file type"); + continue; + } + + path = postDirectory.relativize(path); + var post = Post.fromFile(git, path); + + var render = template.renderPost(site, post); + var outFile = outputDirectory.resolve("post").resolve(path); + + try { + Files.createDirectory(outFile.getParent()); + } catch (FileAlreadyExistsException e) { + + } + + Files.writeString(outFile, render); + } + } + } + private static void copyIfExists(Path src, Path dest) throws IOException { + System.out.print(src); + if (Files.exists(src)) { + PathUtils.copyDirectory(src, dest); + System.out.println(" copied"); + } else { + System.out.println(" doesn't exist, not copying"); + } } } \ No newline at end of file diff --git a/src/main/java/eu/m724/blog/Renderer.java b/src/main/java/eu/m724/blog/Renderer.java deleted file mode 100644 index 564735a..0000000 --- a/src/main/java/eu/m724/blog/Renderer.java +++ /dev/null @@ -1,28 +0,0 @@ -package eu.m724.blog; - -import com.google.common.io.Resources; -import com.hubspot.jinjava.Jinjava; -import eu.m724.blog.data.Site; -import org.apache.commons.net.nntp.Article; - -import java.util.Map; - -public class Renderer { - private final - - public void renderIndex(Site site, Article... articles) { - - } - - public void renderArticle(Site site, Article article) { - Jinjava jinjava = new Jinjava(); - Map context = Map.of( - "site", site, - "article", article - ); - - String template = Resources.toString(Resources.getResource("my-template.html"), Charsets.UTF_8); - - return jinjava.render(template, context); - } -} diff --git a/src/main/java/eu/m724/blog/Template.java b/src/main/java/eu/m724/blog/Template.java deleted file mode 100644 index 0d88568..0000000 --- a/src/main/java/eu/m724/blog/Template.java +++ /dev/null @@ -1,25 +0,0 @@ -package eu.m724.blog; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; - -public class Template { - private final Path templateDirectory; - - public Template(Path templateDirectory) { - this.templateDirectory = templateDirectory; - } - - public String postTemplate() throws IOException { - return Files.readString(templateDirectory.resolve("article_template.html")); - } - - public String indexTemplate() throws IOException { - return Files.readString(templateDirectory.resolve("index.html")); - } - - public void copyStatic() { - templateDirectory.resolve("static").co - } -} diff --git a/src/main/java/eu/m724/blog/data/Post.java b/src/main/java/eu/m724/blog/data/Post.java index ddb683a..210689e 100644 --- a/src/main/java/eu/m724/blog/data/Post.java +++ b/src/main/java/eu/m724/blog/data/Post.java @@ -1,13 +1,11 @@ package eu.m724.blog.data; -import com.google.common.collect.Maps; -import com.google.common.io.Resources; -import com.hubspot.jinjava.Jinjava; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.GitAPIException; import java.io.*; import java.nio.file.Files; +import java.nio.file.Path; import java.time.*; import java.util.HashMap; import java.util.Map; @@ -24,29 +22,20 @@ public record Post( String modifiedBy, ZonedDateTime modifiedAt, - Map customProperties, + Map custom, String rawContent ) { - public String renderHtml(String template) { - Jinjava jinjava = new Jinjava(); - - Map context = Map.of( - "article", this - ) - - return jinjava.render(template, context); - } - public String getHtmlContent() { return rawContent; } - public static Post fromFile(Git git, String slug) throws IOException { + public static Post fromFile(Git git, Path path) throws IOException { /* read properties before filtering */ - var properties = new HashMap(); + var slug = path.getFileName().toString().split("\\.")[0]; + var lines = Files.readAllLines(git.getRepository().getDirectory().toPath().resolve("posts").resolve(path)); - var lines = Files.readAllLines(new File(git.getRepository().getDirectory(), "archive/" + slug + ".html").toPath()); + var properties = new HashMap(); String line; while ((line = lines.removeFirst()) != null) { @@ -59,7 +48,7 @@ public record Post( break; if (properties.putIfAbsent(key, data) != null) - System.out.printf("%s: Duplicate property \"%s\". Only the first one will be used.\n", slug, key); + System.out.printf("Post %s: Duplicate property \"%s\". Only the first one will be used.\n", slug, key); } var content = String.join("\n", lines).strip(); @@ -99,7 +88,7 @@ public record Post( ZonedDateTime modifiedAt = null; try { - var commits = git.log().addPath("archive/" + slug + ".html").call().iterator(); + var commits = git.log().addPath("posts/" + slug + ".html").call().iterator(); while (commits.hasNext()) { var commit = commits.next(); diff --git a/src/main/java/eu/m724/blog/data/Template.java b/src/main/java/eu/m724/blog/data/Template.java index 426b3be..5b02e95 100644 --- a/src/main/java/eu/m724/blog/data/Template.java +++ b/src/main/java/eu/m724/blog/data/Template.java @@ -1,23 +1,53 @@ package eu.m724.blog.data; -import com.google.gson.Gson; -import com.google.gson.JsonObject; +import io.pebbletemplates.pebble.PebbleEngine; +import io.pebbletemplates.pebble.loader.FileLoader; +import io.pebbletemplates.pebble.template.PebbleTemplate; import java.io.IOException; -import java.nio.file.Files; +import java.io.StringWriter; import java.nio.file.Path; +import java.util.Map; -public record Template( - String name, - int version, +public class Template { + private final PebbleEngine pebbleEngine; - int articlesPerPage, + private final PebbleTemplate indexTemplate, articleTemplate; - JsonObject customProperties -) { - public static Template fromDirectory(Path templateDirectory) throws IOException { - var content = Files.readString(templateDirectory.resolve("template.json")); + public Template(Path directory) { + var loader = new FileLoader(); + loader.setPrefix(directory.toString()); + loader.setSuffix(".html"); - return new Gson().fromJson(content, Template.class); + this.pebbleEngine = new PebbleEngine.Builder() + .loader(loader) + .build(); + + this.indexTemplate = pebbleEngine.getTemplate("index_template"); + this.articleTemplate = pebbleEngine.getTemplate("article_template"); + } + + public String renderIndex(Site site, Post... posts) throws IOException { + Map context = Map.of( + "site", site, + "articles", posts + ); + + var writer = new StringWriter(); + indexTemplate.evaluate(writer, context); + + return writer.toString(); + } + + public String renderPost(Site site, Post post) throws IOException { + Map context = Map.of( + "site", site, + "article", post + ); + + var writer = new StringWriter(); + articleTemplate.evaluate(writer, context); + + return writer.toString(); } }