about summary refs log tree commit diff
path: root/src/main.zig
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/main.zig64
1 files changed, 44 insertions, 20 deletions
diff --git a/src/main.zig b/src/main.zig
index aa76b93..10eead9 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -10,7 +10,6 @@ const PAGE_PATH = "src/inc";
 const SITE_ROOT = ".";
 const SITE_PATH = "site";
 
-const DOMAIN_URL = "https://gzr.im";
 const SITE_NAME = "gzr.im";
 const LICENSE_HOLDER = "robertgzr";
 const LICENSE_NAME = "BY-NC-SA 4.0";
@@ -19,6 +18,11 @@ const SOURCE_URL = "https://git.sr.ht/~robertgzr/site/tree/rabbits";
 
 const BuildErrors = (std.fs.File.OpenError || std.fs.File.WriteError || std.mem.Allocator.Error);
 
+// tune std.log options
+pub const std_options = .{
+    .log_level = .info,
+};
+
 const Context = struct {
     allocator: std.mem.Allocator,
     pages: [][]const u8,
@@ -29,11 +33,10 @@ const Context = struct {
 
 // also trims off base paths
 fn stripExt(path: []const u8) ![]const u8 {
-    const basename = std.fs.path.basename(path);
     const ext = std.fs.path.extension(path);
-    const ext_idx = std.mem.indexOfPosLinear(u8, basename, 0, ext);
+    const ext_idx = std.mem.indexOfPosLinear(u8, path, 0, ext);
     std.debug.assert(ext_idx != null);
-    return basename[0..ext_idx.?];
+    return path[0..ext_idx.?];
 }
 
 fn joinIn(allocator: std.mem.Allocator, paths: []const []const u8, filename_parts: []const []const u8) ![]u8 {
@@ -167,21 +170,20 @@ fn build(allocator: std.mem.Allocator, f: std.fs.File, page_name: []const u8, sr
         \\<!DOCTYPE html><html lang="en">
         \\<head>
         \\<meta charset="utf-8">
-        \\<meta name="thumbnail" content="{[DOMAIN]s}/media/services/thumbnail.jpg">
+        \\<meta name="thumbnail" content="/media/services/thumbnail.jpg">
         \\<meta name="viewport" content="width=device-width,initial-scale=1">
-        \\<link rel="alternate" type="application/rss+xml" title="RSS Feed" href="../links/rss.xml">
-        \\<link rel="stylesheet" type="text/css" href="../links/main.css">
-        \\<link rel="shortcut icon" type="image/png" href="../media/services/icon.png">
+        \\<link rel="alternate" type="application/rss+xml" title="RSS Feed" href="/links/rss.xml">
+        \\<link rel="stylesheet" type="text/css" href="/links/main.css">
+        \\<link rel="shortcut icon" type="image/png" href="/media/services/icon.png">
         \\<title>{[SITE_NAME]s} &mdash; {[PAGE_NAME]s}</title>
         \\</head>
         \\<body>
         \\
         \\<header>
-        \\<a href="/"><img src="../media/interface/logo.svg" alt="{[SITE_NAME]s}"></a>
+        \\<a href="/"><img src="/media/interface/logo.svg" alt="{[SITE_NAME]s}"></a>
         \\</header>
         \\
     , .{
-        .DOMAIN = DOMAIN_URL,
         .SITE_NAME = SITE_NAME,
         .PAGE_NAME = page_name,
     });
@@ -304,7 +306,8 @@ fn meta(ctx: *Context, p: []const u8) !void {
 
 fn genSite(ctx: *Context) !void {
     for (ctx.pages) |p| {
-        if (std.mem.startsWith(u8, p, "meta.")) {
+        const pname = std.fs.path.basename(p);
+        if (std.mem.startsWith(u8, pname, "meta.")) {
             try meta(ctx, p);
             continue;
         }
@@ -314,6 +317,17 @@ fn genSite(ctx: *Context) !void {
         defer ctx.allocator.free(srcpath);
         const dstpath = try joinIn(ctx.allocator, &.{SITE_PATH}, &.{ dstname, ".html" });
         defer ctx.allocator.free(dstpath);
+        const rel_dstpath = try std.fs.path.relative(ctx.allocator, SITE_PATH, dstpath);
+        defer ctx.allocator.free(rel_dstpath);
+
+        std.log.debug("generating {s} at {s}", .{ p, rel_dstpath });
+
+        if (std.fs.path.dirname(rel_dstpath)) |d| {
+            const dstdir = try joinIn(ctx.allocator, &.{SITE_PATH}, &.{d});
+            defer ctx.allocator.free(dstdir);
+
+            try std.fs.cwd().makePath(dstdir);
+        }
 
         const f = try std.fs.cwd().createFile(dstpath, .{});
 
@@ -372,25 +386,33 @@ fn genIndexHtml(ctx: *Context) !void {
     );
 }
 
-fn index(allocator: std.mem.Allocator, path: []const u8) ![][]const u8 {
-    var dir = std.fs.cwd().openDir(path, .{ .iterate = true }) catch |err| {
+fn index(allocator: std.mem.Allocator, rootpath: []const u8) ![][]const u8 {
+    var root = std.fs.cwd().openDir(rootpath, .{ .iterate = true }) catch |err| {
         if (err == error.FileNotFound) {
             std.log.err("Missing {s} folder.", .{PAGE_PATH});
             std.os.linux.exit(1);
         } else return err;
     };
-    defer dir.close();
+    defer root.close();
 
     var pages = std.ArrayList([]const u8).init(allocator);
     defer pages.deinit();
 
-    var dirIter = dir.iterate();
-    while (try dirIter.next()) |e|
-        if (e.kind == .file)
-            if (std.mem.endsWith(u8, e.name, ".htm"))
-                try pages.append(try allocator.dupe(u8, e.name));
+    var walk = try root.walk(allocator);
+    defer walk.deinit();
+
+    while (try walk.next()) |e| {
+        switch (e.kind) {
+            .file => {
+                if (std.mem.endsWith(u8, e.basename, ".htm")) {
+                    std.log.debug("Indexing {s}", .{e.path});
+                    try pages.append(try allocator.dupe(u8, e.path));
+                }
+            },
+            else => {},
+        }
+    }
 
-    std.log.info("Indexed {d} pages.", .{pages.items.len});
     return try pages.toOwnedSlice();
 }
 
@@ -408,6 +430,8 @@ pub fn main() !void {
         allocator.free(pages);
     }
 
+    std.log.info("Indexed {d} pages.", .{pages.len});
+
     std.log.info("Generated index.", .{});
 
     var ctx = Context{