Add indexes (WIP)

This commit is contained in:
MrLetsplay 2024-01-16 20:53:46 +01:00
parent ff4445035a
commit 6db5cdd701
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
3 changed files with 70 additions and 4 deletions

View File

@ -6,15 +6,21 @@ import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchKey; import java.nio.file.WatchKey;
import java.nio.file.WatchService; import java.nio.file.WatchService;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import me.mrletsplay.mdblog.blog.Post; import me.mrletsplay.mdblog.blog.Post;
import me.mrletsplay.simplehttpserver.dom.html.HtmlDocument;
import me.mrletsplay.simplehttpserver.dom.html.HtmlElement;
import me.mrletsplay.simplehttpserver.http.HttpRequestMethod; import me.mrletsplay.simplehttpserver.http.HttpRequestMethod;
import me.mrletsplay.simplehttpserver.http.HttpStatusCodes;
import me.mrletsplay.simplehttpserver.http.document.FileDocument; import me.mrletsplay.simplehttpserver.http.document.FileDocument;
import me.mrletsplay.simplehttpserver.http.request.HttpRequestContext; import me.mrletsplay.simplehttpserver.http.request.HttpRequestContext;
import me.mrletsplay.simplehttpserver.http.response.TextResponse;
import me.mrletsplay.simplehttpserver.http.server.HttpServer; import me.mrletsplay.simplehttpserver.http.server.HttpServer;
public class MdBlog { public class MdBlog {
@ -34,6 +40,10 @@ public class MdBlog {
.port(3706) .port(3706)
.create()); .create());
server.getDocumentProvider().registerPattern(HttpRequestMethod.GET, "/posts", () -> {
createPostsIndex(POSTS_PATH);
});
server.getDocumentProvider().registerPattern(HttpRequestMethod.GET, "/posts/{path...}", () -> { server.getDocumentProvider().registerPattern(HttpRequestMethod.GET, "/posts/{path...}", () -> {
HttpRequestContext ctx = HttpRequestContext.getCurrentContext(); HttpRequestContext ctx = HttpRequestContext.getCurrentContext();
String path = ctx.getPathParameters().get("path"); String path = ctx.getPathParameters().get("path");
@ -44,16 +54,22 @@ public class MdBlog {
} }
Path resolved = POSTS_PATH.resolve(path).normalize(); Path resolved = POSTS_PATH.resolve(path).normalize();
if(!resolved.startsWith(POSTS_PATH) || resolved.getFileName().toString().endsWith(".md")) { if(!resolved.startsWith(POSTS_PATH)) {
server.getDocumentProvider().getNotFoundDocument().createContent(); server.getDocumentProvider().getNotFoundDocument().createContent();
return; return;
} }
if(!Files.isRegularFile(resolved)) { if(!Files.isRegularFile(resolved)) {
if(!Files.isDirectory(resolved)) {
server.getDocumentProvider().getNotFoundDocument().createContent(); server.getDocumentProvider().getNotFoundDocument().createContent();
return; return;
} }
createPostsIndex(resolved);
return;
}
try { try {
new FileDocument(resolved).createContent(); new FileDocument(resolved).createContent();
} catch (IOException e) { } catch (IOException e) {
@ -62,6 +78,7 @@ public class MdBlog {
}); });
extractAndRegister("style/base.css"); extractAndRegister("style/base.css");
extractAndRegister("style/index.css");
extractAndRegister("style/post.css"); extractAndRegister("style/post.css");
server.start(); server.start();
@ -92,6 +109,41 @@ public class MdBlog {
} }
} }
private static void createPostsIndex(Path directory) {
try {
// Generate posts index
List<Path> inDir = Files.list(directory)
.filter(p -> Files.isDirectory(p) || posts.values().stream().anyMatch(post -> post.getFilePath().equals(p)))
.collect(Collectors.toList());
Collections.sort(inDir);
HtmlDocument index = new HtmlDocument();
index.setTitle("Index of " + directory.getFileName());
index.addStyleSheet("/style/index.css");
HtmlElement ul = new HtmlElement("ul");
for(Path p : inDir) {
HtmlElement li = new HtmlElement("li");
HtmlElement a = new HtmlElement("a");
String relPath = directory.relativize(p).toString();
if(Files.isRegularFile(p)) relPath = relPath.substring(0, relPath.length() - Post.FILE_EXTENSION.length());
// TODO: only works with trailing /
a.setAttribute("href", relPath);
a.setText(relPath);
li.appendChild(a);
ul.appendChild(li);
}
index.getBodyNode().appendChild(ul);
index.createContent();
}catch(IOException e) {
e.printStackTrace();
HttpRequestContext ctx = HttpRequestContext.getCurrentContext();
ctx.respond(HttpStatusCodes.INTERNAL_SERVER_ERROR_500, new TextResponse("Failed to create index"));
}
}
private static void extractAndRegister(String path) throws IOException { private static void extractAndRegister(String path) throws IOException {
Path filePath = FILES_PATH.resolve(path); Path filePath = FILES_PATH.resolve(path);
if(!Files.exists(filePath)) { if(!Files.exists(filePath)) {
@ -111,12 +163,12 @@ public class MdBlog {
Files.walk(POSTS_PATH) Files.walk(POSTS_PATH)
.filter(Files::isRegularFile) .filter(Files::isRegularFile)
.filter(f -> f.getFileName().toString().endsWith(".md")) .filter(f -> f.getFileName().toString().endsWith(Post.FILE_EXTENSION))
.filter(f -> posts.values().stream().noneMatch(p -> p.getFilePath().equals(f))) .filter(f -> posts.values().stream().noneMatch(p -> p.getFilePath().equals(f)))
.forEach(f -> { .forEach(f -> {
try { try {
String path = POSTS_PATH.relativize(f).toString(); String path = POSTS_PATH.relativize(f).toString();
path = path.substring(0, path.length() - ".md".length()); path = path.substring(0, path.length() - Post.FILE_EXTENSION.length());
posts.put(path, new Post(f)); posts.put(path, new Post(f));
} catch (IOException e) {} } catch (IOException e) {}
}); });

View File

@ -13,6 +13,8 @@ import me.mrletsplay.simplehttpserver.dom.html.HtmlDocument;
public class Post { public class Post {
public static final String FILE_EXTENSION = ".md";
private static final MessageDigest MD_5; private static final MessageDigest MD_5;
private static final MdRenderer RENDERER = new MdRenderer(); private static final MdRenderer RENDERER = new MdRenderer();
@ -39,6 +41,11 @@ public class Post {
return filePath; return filePath;
} }
public String getName() {
String fileName = filePath.getFileName().toString();
return fileName.substring(0, fileName.length() - FILE_EXTENSION.length());
}
public PostMetadata getMetadata() { public PostMetadata getMetadata() {
return metadata; return metadata;
} }

View File

@ -0,0 +1,7 @@
ul {
}
li {
}