This commit is contained in:
Minecon724 2025-01-10 14:43:24 +01:00
parent dd28fde073
commit e6cd257238
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
4 changed files with 101 additions and 11 deletions

View file

@ -2,5 +2,6 @@
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" /> <mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/m724" vcs="Git" />
</component> </component>
</project> </project>

View file

@ -3,6 +3,8 @@ package eu.m724.blog;
import eu.m724.blog.data.Post; import eu.m724.blog.data.Post;
import eu.m724.blog.data.Site; import eu.m724.blog.data.Site;
import eu.m724.blog.data.Template; 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.apache.commons.io.file.PathUtils;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.RepositoryBuilder; import org.eclipse.jgit.lib.RepositoryBuilder;
@ -16,6 +18,12 @@ import java.util.Comparator;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class Main { 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 { public static void main(String[] args) throws IOException {
System.out.println("Hello world!"); System.out.println("Hello world!");
@ -27,7 +35,7 @@ public class Main {
// //
var repository = new RepositoryBuilder() var repository = new RepositoryBuilder()
.setGitDir(workingDirectory.toFile()) .setGitDir(workingDirectory.resolve(".git").toFile())
.build(); .build();
var git = new Git(repository); var git = new Git(repository);
@ -83,12 +91,51 @@ public class Main {
new Server(outputDirectory).start(); 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 { try (var walk = Files.walk(srcDir)) {
System.out.print(src); for (var src : walk.collect(Collectors.toSet())) {
var rel = srcDir.relativize(src);
var dest = destDir.resolve(rel);
if (Files.exists(src)) { if (Files.isRegularFile(src)) {
PathUtils.copyDirectory(src, dest); 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"); System.out.println(" copied");
} else { } else {
System.out.println(" doesn't exist, not copying"); System.out.println(" doesn't exist, not copying");

View file

@ -35,7 +35,7 @@ public record Post(
var slug = path.getFileName().toString().split("\\.")[0]; var slug = path.getFileName().toString().split("\\.")[0];
path = Path.of("posts").resolve(path); 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<String, String>(); var properties = new HashMap<String, String>();

View file

@ -1,22 +1,64 @@
package eu.m724.blog.data; package eu.m724.blog.data;
import com.google.gson.Gson; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; 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( public record Site(
String name, String name,
String baseUrl, String baseUrl,
JsonObject customProperties Map<String, Object> custom
) { ) {
public static Site fromConfig(Git git) throws IOException { 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<String, Object>();
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<String, Object> 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;
} }
} }