2025-01-07 11:41:30 +01:00
|
|
|
package eu.m724.blog;
|
|
|
|
|
2025-01-10 18:50:12 +01:00
|
|
|
import eu.m724.blog.data.Feed;
|
2025-01-08 18:40:11 +01:00
|
|
|
import eu.m724.blog.data.Post;
|
|
|
|
import eu.m724.blog.data.Site;
|
2025-01-10 14:43:24 +01:00
|
|
|
import in.wilsonl.minifyhtml.Configuration;
|
|
|
|
import in.wilsonl.minifyhtml.MinifyHtml;
|
2025-01-08 18:40:11 +01:00
|
|
|
import org.apache.commons.io.file.PathUtils;
|
2025-01-07 11:41:30 +01:00
|
|
|
import org.eclipse.jgit.api.Git;
|
|
|
|
import org.eclipse.jgit.lib.RepositoryBuilder;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.FileAlreadyExistsException;
|
2025-01-08 18:40:11 +01:00
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
2025-01-09 16:15:55 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Comparator;
|
2025-01-08 18:40:11 +01:00
|
|
|
import java.util.stream.Collectors;
|
2025-01-07 11:41:30 +01:00
|
|
|
|
|
|
|
public class Main {
|
2025-01-10 14:43:24 +01:00
|
|
|
// TODO put this somewhere else
|
|
|
|
private static final Configuration configuration = new Configuration.Builder()
|
|
|
|
.setMinifyCss(true)
|
|
|
|
.setMinifyJs(true)
|
|
|
|
.build();
|
|
|
|
|
2025-01-07 11:41:30 +01:00
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
System.out.println("Hello world!");
|
|
|
|
|
2025-01-10 18:50:12 +01:00
|
|
|
var start = System.nanoTime();
|
|
|
|
|
2025-01-08 18:40:11 +01:00
|
|
|
var workingDirectory = Path.of("m724");
|
|
|
|
var templateDirectory = workingDirectory.resolve("template");
|
|
|
|
var outputDirectory = workingDirectory.resolve("generated_out");
|
2025-01-07 11:41:30 +01:00
|
|
|
var force = true;
|
|
|
|
|
2025-01-10 18:50:12 +01:00
|
|
|
var server = true;
|
|
|
|
var openBrowser = true;
|
|
|
|
|
2025-01-07 11:41:30 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
var repository = new RepositoryBuilder()
|
2025-01-10 14:43:24 +01:00
|
|
|
.setGitDir(workingDirectory.resolve(".git").toFile())
|
2025-01-07 11:41:30 +01:00
|
|
|
.build();
|
|
|
|
var git = new Git(repository);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2025-01-08 18:40:11 +01:00
|
|
|
if (outputDirectory.toFile().exists()) {
|
2025-01-07 11:41:30 +01:00
|
|
|
if (force) {
|
2025-01-08 18:40:11 +01:00
|
|
|
PathUtils.deleteDirectory(outputDirectory);
|
2025-01-07 11:41:30 +01:00
|
|
|
} else {
|
2025-01-08 18:40:11 +01:00
|
|
|
throw new FileAlreadyExistsException(outputDirectory.toString(), null, "Output directory already exists");
|
2025-01-07 11:41:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-08 18:40:11 +01:00
|
|
|
var site = Site.fromConfig(git);
|
2025-01-10 18:50:12 +01:00
|
|
|
var template = new TemplateRenderer(outputDirectory, templateDirectory);
|
2025-01-07 11:41:30 +01:00
|
|
|
|
2025-01-08 18:40:11 +01:00
|
|
|
Files.createDirectory(outputDirectory);
|
2025-01-07 11:41:30 +01:00
|
|
|
|
2025-01-08 18:40:11 +01:00
|
|
|
copyIfExists(workingDirectory.resolve("assets"), outputDirectory.resolve("assets"));
|
|
|
|
copyIfExists(templateDirectory.resolve("static"), outputDirectory.resolve("static"));
|
2025-01-07 11:41:30 +01:00
|
|
|
|
2025-01-08 18:40:11 +01:00
|
|
|
Files.createDirectory(outputDirectory.resolve("post"));
|
|
|
|
var postDirectory = workingDirectory.resolve("posts");
|
2025-01-09 16:15:55 +01:00
|
|
|
var posts = new ArrayList<Post>();
|
|
|
|
|
2025-01-08 18:40:11 +01:00
|
|
|
try (var stream = Files.walk(postDirectory)) {
|
|
|
|
for (var path : stream.collect(Collectors.toSet())) {
|
|
|
|
if (!Files.isRegularFile(path)) continue;
|
2025-01-07 11:41:30 +01:00
|
|
|
|
2025-01-08 18:40:11 +01:00
|
|
|
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());
|
2025-01-09 16:15:55 +01:00
|
|
|
} catch (FileAlreadyExistsException ignored) { }
|
2025-01-08 18:40:11 +01:00
|
|
|
|
|
|
|
Files.writeString(outFile, render);
|
2025-01-09 16:15:55 +01:00
|
|
|
posts.add(post);
|
2025-01-08 18:40:11 +01:00
|
|
|
}
|
|
|
|
}
|
2025-01-09 16:15:55 +01:00
|
|
|
|
|
|
|
posts.sort(Comparator.comparing(Post::createdAt).reversed());
|
|
|
|
Files.writeString(outputDirectory.resolve("index.html"), template.renderIndex(site, posts));
|
|
|
|
|
2025-01-10 18:50:12 +01:00
|
|
|
Files.writeString(outputDirectory.resolve("posts.rss"), Feed.generateRss(site, posts));
|
|
|
|
|
|
|
|
|
|
|
|
var end = System.nanoTime();
|
|
|
|
System.out.printf("Exported to %s (%.2f ms)\n", outputDirectory, (end - start) / 1000000.0);
|
|
|
|
|
|
|
|
if (server) {
|
|
|
|
var listenAddress = new Server(outputDirectory).start();
|
|
|
|
if (openBrowser) {
|
|
|
|
try {
|
|
|
|
var process = Runtime.getRuntime().exec(new String[] { "xdg-open", "http://" + listenAddress.getHostString() + ":" + listenAddress.getPort() });
|
|
|
|
var code = process.waitFor();
|
|
|
|
if (code != 0) {
|
|
|
|
throw new Exception("Exit code " + code);
|
|
|
|
}
|
|
|
|
System.out.println("Opened browser");
|
|
|
|
} catch (Exception e) {
|
|
|
|
System.out.println("Failed to open browser: " + e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-01-08 18:40:11 +01:00
|
|
|
}
|
|
|
|
|
2025-01-10 14:43:24 +01:00
|
|
|
private static boolean copyTree(Path srcDir, Path destDir) throws IOException {
|
|
|
|
if (!Files.isDirectory(srcDir)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.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;
|
|
|
|
}
|
|
|
|
|
2025-01-08 18:40:11 +01:00
|
|
|
|
2025-01-10 14:43:24 +01:00
|
|
|
private static void copyIfExists(Path srcDir, Path destDir) throws IOException {
|
|
|
|
System.out.print(srcDir);
|
2025-01-08 18:40:11 +01:00
|
|
|
|
2025-01-10 14:43:24 +01:00
|
|
|
if (copyTree(srcDir, destDir)) {
|
2025-01-08 18:40:11 +01:00
|
|
|
System.out.println(" copied");
|
|
|
|
} else {
|
|
|
|
System.out.println(" doesn't exist, not copying");
|
|
|
|
}
|
2025-01-07 11:41:30 +01:00
|
|
|
}
|
|
|
|
}
|