made it work

This commit is contained in:
Minecon724 2025-01-08 18:40:11 +01:00
parent 8978c7ae4c
commit b6031b93e6
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
7 changed files with 113 additions and 101 deletions

3
.gitignore vendored
View file

@ -35,4 +35,5 @@ build/
.vscode/
### Mac OS ###
.DS_Store
.DS_Store
/m724/

View file

@ -12,6 +12,7 @@
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<exec.mainClass>eu.m724.blog.Main</exec.mainClass>
</properties>
<dependencies>
@ -26,9 +27,9 @@
<version>7.1.0.202411261347-r</version>
</dependency>
<dependency>
<groupId>com.hubspot.jinjava</groupId>
<artifactId>jinjava</artifactId>
<version>2.7.4</version>
<groupId>io.pebbletemplates</groupId>
<artifactId>pebble</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>

View file

@ -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");
}
}
}

View file

@ -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<String, Object> context = Map.of(
"site", site,
"article", article
);
String template = Resources.toString(Resources.getResource("my-template.html"), Charsets.UTF_8);
return jinjava.render(template, context);
}
}

View file

@ -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
}
}

View file

@ -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<String, String> customProperties,
Map<String, String> custom,
String rawContent
) {
public String renderHtml(String template) {
Jinjava jinjava = new Jinjava();
Map<String, Object> 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<String, String>();
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, String>();
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();

View file

@ -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<String, Object> 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<String, Object> context = Map.of(
"site", site,
"article", post
);
var writer = new StringWriter();
articleTemplate.evaluate(writer, context);
return writer.toString();
}
}