Signed-off-by: Minecon724 <git@m724.eu>
This commit is contained in:
parent
159519c94d
commit
0aadf69a42
6 changed files with 52 additions and 23 deletions
|
@ -10,6 +10,8 @@ import org.apache.commons.compress.compressors.CompressorException;
|
||||||
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;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.FileAlreadyExistsException;
|
import java.nio.file.FileAlreadyExistsException;
|
||||||
|
@ -27,6 +29,8 @@ import java.util.stream.Collectors;
|
||||||
* source for the blog's content and configuration.
|
* source for the blog's content and configuration.
|
||||||
*/
|
*/
|
||||||
public class BlogBuilder {
|
public class BlogBuilder {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(BlogBuilder.class);
|
||||||
|
|
||||||
private final Git git;
|
private final Git git;
|
||||||
private final Path workingDirectory;
|
private final Path workingDirectory;
|
||||||
|
|
||||||
|
@ -121,7 +125,7 @@ public class BlogBuilder {
|
||||||
* @throws IOException if an I/O error occurs
|
* @throws IOException if an I/O error occurs
|
||||||
*/
|
*/
|
||||||
public void build() throws IOException {
|
public void build() throws IOException {
|
||||||
System.out.println("Loading site...");
|
LOGGER.debug("Loading site...");
|
||||||
if (site == null)
|
if (site == null)
|
||||||
this.site = Site.fromConfig(workingDirectory.resolve("site.yml"));
|
this.site = Site.fromConfig(workingDirectory.resolve("site.yml"));
|
||||||
|
|
||||||
|
@ -131,21 +135,21 @@ public class BlogBuilder {
|
||||||
if (template == null)
|
if (template == null)
|
||||||
this.template = new TemplateRenderer(templateDirectory);
|
this.template = new TemplateRenderer(templateDirectory);
|
||||||
|
|
||||||
System.out.println("Copying assets...");
|
LOGGER.debug("Copying assets...");
|
||||||
copyTree(workingDirectory.resolve("assets"), outputDirectory.resolve("assets"));
|
copyTree(workingDirectory.resolve("assets"), outputDirectory.resolve("assets"));
|
||||||
copyTree(templateDirectory.resolve("static"), outputDirectory.resolve("static"));
|
copyTree(templateDirectory.resolve("static"), outputDirectory.resolve("static"));
|
||||||
|
|
||||||
System.out.println("Rendering posts...");
|
LOGGER.debug("Rendering posts...");
|
||||||
var posts = renderPosts();
|
var posts = renderPosts();
|
||||||
|
|
||||||
System.out.println("Rendering meta...");
|
LOGGER.debug("Rendering meta...");
|
||||||
posts.sort(Comparator.comparing(Post::createdAt).reversed());
|
posts.sort(Comparator.comparing(Post::createdAt).reversed());
|
||||||
Files.writeString(outputDirectory.resolve("index.html"), template.renderIndex(site, posts));
|
Files.writeString(outputDirectory.resolve("index.html"), template.renderIndex(site, posts));
|
||||||
|
|
||||||
Files.writeString(outputDirectory.resolve("posts.rss"), Feed.generateRss(site, posts));
|
Files.writeString(outputDirectory.resolve("posts.rss"), Feed.generateRss(site, posts));
|
||||||
|
|
||||||
if (!renderOptions.compress().isEmpty()) {
|
if (!renderOptions.compress().isEmpty()) {
|
||||||
System.out.println("Compressing...");
|
LOGGER.debug("Compressing...");
|
||||||
compressOutput();
|
compressOutput();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -162,7 +166,7 @@ public class BlogBuilder {
|
||||||
continue; // directory is created below
|
continue; // directory is created below
|
||||||
|
|
||||||
if (!path.toString().endsWith(".html")) {
|
if (!path.toString().endsWith(".html")) {
|
||||||
System.out.println("Post " + path.getFileName() + ": unsupported file type");
|
LOGGER.warn("Post {}: unsupported file type", path.getFileName());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -170,7 +174,7 @@ public class BlogBuilder {
|
||||||
var post = Post.fromFile(git, path);
|
var post = Post.fromFile(git, path);
|
||||||
|
|
||||||
if (post.draft() && !renderDrafts) {
|
if (post.draft() && !renderDrafts) {
|
||||||
System.out.println("Post " + path.getFileName() + ": draft, ignoring");
|
LOGGER.info("Post {}: draft, ignoring", path.getFileName());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -204,7 +208,7 @@ public class BlogBuilder {
|
||||||
try {
|
try {
|
||||||
compressor.compress(path);
|
compressor.compress(path);
|
||||||
} catch (CompressorException e) {
|
} catch (CompressorException e) {
|
||||||
System.err.printf("Exception compressing %s to %s: %s%n", path, compressor.getAlgorithm(), e.getMessage());
|
LOGGER.error("Error compressing {} to {}: {}", path, compressor.getAlgorithm(), e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,16 @@
|
||||||
package eu.m724.blog;
|
package eu.m724.blog;
|
||||||
|
|
||||||
import org.apache.commons.cli.*;
|
import org.apache.commons.cli.*;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) throws IOException {
|
private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
|
||||||
System.out.println("Hello world!");
|
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
var commandLine = getCommandLine(args);
|
var commandLine = getCommandLine(args);
|
||||||
|
|
||||||
if (commandLine == null)
|
if (commandLine == null)
|
||||||
|
@ -37,12 +39,12 @@ public class Main {
|
||||||
|
|
||||||
builder.mkdirs(force);
|
builder.mkdirs(force);
|
||||||
|
|
||||||
System.out.println("---- START BUILD ----");
|
LOGGER.info("Building...");
|
||||||
builder.build();
|
builder.build();
|
||||||
System.out.println("----- END BUILD -----");
|
|
||||||
|
|
||||||
var end = System.nanoTime();
|
var end = System.nanoTime();
|
||||||
System.out.printf("Exported to %s (%.2f ms)\n", outputDirectory, (end - start) / 1000000.0);
|
// BAD
|
||||||
|
LOGGER.info("Exported to {} in {} ms", outputDirectory.toAbsolutePath(), "%.2f".formatted((end - start) / 1000000.0));
|
||||||
|
|
||||||
/* Server process */
|
/* Server process */
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@ package eu.m724.blog;
|
||||||
|
|
||||||
import com.sun.net.httpserver.HttpServer;
|
import com.sun.net.httpserver.HttpServer;
|
||||||
import com.sun.net.httpserver.SimpleFileServer;
|
import com.sun.net.httpserver.SimpleFileServer;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
@ -13,6 +15,8 @@ import java.nio.file.Path;
|
||||||
* {@link SimpleFileServer} is used.
|
* {@link SimpleFileServer} is used.
|
||||||
*/
|
*/
|
||||||
public class Server {
|
public class Server {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(Server.class);
|
||||||
|
|
||||||
private final Path webroot;
|
private final Path webroot;
|
||||||
private InetSocketAddress listenAddress;
|
private InetSocketAddress listenAddress;
|
||||||
|
|
||||||
|
@ -48,7 +52,7 @@ public class Server {
|
||||||
server.createContext("/", SimpleFileServer.createFileHandler(webroot.toAbsolutePath()));
|
server.createContext("/", SimpleFileServer.createFileHandler(webroot.toAbsolutePath()));
|
||||||
server.start();
|
server.start();
|
||||||
|
|
||||||
System.out.println("Server started on http:/" + server.getAddress());
|
LOGGER.info("Server started on http:/{}", server.getAddress());
|
||||||
|
|
||||||
this.listenAddress = server.getAddress();
|
this.listenAddress = server.getAddress();
|
||||||
}
|
}
|
||||||
|
@ -63,9 +67,10 @@ public class Server {
|
||||||
if (code != 0) {
|
if (code != 0) {
|
||||||
throw new Exception("Exit code " + code);
|
throw new Exception("Exit code " + code);
|
||||||
}
|
}
|
||||||
System.out.println("Opened browser");
|
|
||||||
|
LOGGER.info("Opened browser"); // TODO make this debug?
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println("Failed to open browser: " + e);
|
LOGGER.error("Failed to open browser: {}", String.valueOf(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@ package eu.m724.blog.data;
|
||||||
|
|
||||||
import org.eclipse.jgit.api.Git;
|
import org.eclipse.jgit.api.Git;
|
||||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
@ -40,6 +42,8 @@ public record Post(
|
||||||
Map<String, String> custom,
|
Map<String, String> custom,
|
||||||
String rawContent
|
String rawContent
|
||||||
) {
|
) {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(Post.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a {@link Post} instance by reading and parsing the content of a post file.
|
* Creates a {@link Post} instance by reading and parsing the content of a post file.
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -71,7 +75,7 @@ public record Post(
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (properties.putIfAbsent(key, data) != null)
|
if (properties.putIfAbsent(key, data) != null)
|
||||||
System.out.printf("Post %s: Duplicate property \"%s\". Only the first one will be used.\n", slug, key);
|
LOGGER.warn("[Post {}] Ignoring duplicate property: {}", slug, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
var content = String.join("\n", lines).strip();
|
var content = String.join("\n", lines).strip();
|
||||||
|
@ -122,7 +126,7 @@ public record Post(
|
||||||
}
|
}
|
||||||
} catch (GitAPIException e) {
|
} catch (GitAPIException e) {
|
||||||
draft = true;
|
draft = true;
|
||||||
System.out.printf("%s: Git exception, making draft: %s\n", slug, e.getMessage());
|
LOGGER.warn("[Post {}] Draft because of a Git exception: {}\n", slug, e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Post(slug, title, summary, draft, revisions, createdBy, createdAt, modifiedBy, modifiedAt, custom, content);
|
return new Post(slug, title, summary, draft, revisions, createdBy, createdAt, modifiedBy, modifiedAt, custom, content);
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package eu.m724.blog.data;
|
package eu.m724.blog.data;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.snakeyaml.engine.v2.api.Load;
|
import org.snakeyaml.engine.v2.api.Load;
|
||||||
import org.snakeyaml.engine.v2.api.LoadSettings;
|
import org.snakeyaml.engine.v2.api.LoadSettings;
|
||||||
|
|
||||||
|
@ -13,6 +15,8 @@ import java.util.Map;
|
||||||
public record RenderOptions(
|
public record RenderOptions(
|
||||||
List<String> compress // TODO rename?
|
List<String> compress // TODO rename?
|
||||||
) {
|
) {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(RenderOptions.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a {@link Site} object by reading and parsing the configuration file at the specified path.<br>
|
* Creates a {@link Site} object by reading and parsing the configuration file at the specified path.<br>
|
||||||
* The configuration file must be a JSON file.
|
* The configuration file must be a JSON file.
|
||||||
|
@ -31,12 +35,11 @@ public record RenderOptions(
|
||||||
var value = yaml.get(key);
|
var value = yaml.get(key);
|
||||||
|
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case "compress": {
|
case "compress":
|
||||||
compress = (List<String>) value;
|
compress = (List<String>) value;
|
||||||
}
|
break;
|
||||||
default: {
|
default:
|
||||||
System.out.println("Unrecognized option (render): " + key);
|
LOGGER.warn("Ignoring unrecognized render option: {}", key);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
11
src/main/resources/simplelogger.properties
Normal file
11
src/main/resources/simplelogger.properties
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
org.slf4j.simpleLogger.defaultLogLevel=info
|
||||||
|
|
||||||
|
# TODO maybe?
|
||||||
|
#org.slf4j.simpleLogger.showDateTime=true
|
||||||
|
#org.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss.SSS
|
||||||
|
|
||||||
|
org.slf4j.simpleLogger.showThreadName=false
|
||||||
|
org.slf4j.simpleLogger.showShortLogName=true
|
||||||
|
org.slf4j.simpleLogger.levelInBrackets=true
|
||||||
|
|
||||||
|
org.slf4j.simpleLogger.logFile=System.out
|
Loading…
Add table
Reference in a new issue