about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2026-02-09 10:44:52 +0100
committerRobert Günzler <r@gnzler.io>2026-02-09 10:53:50 +0100
commitd18a77969196692e5a405990541ba896731dac3c (patch)
tree0640faf8040f1625973711235d40a60d88148ed1
parentdd8655367b13d3218b7f86fa41a576c1ec9bd16a (diff)
downloadsite-d18a77969196692e5a405990541ba896731dac3c.tar.gz
refactor root generator loop
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to '')
-rw-r--r--src/gen.zig81
-rw-r--r--src/main.zig6
2 files changed, 50 insertions, 37 deletions
diff --git a/src/gen.zig b/src/gen.zig
index 3f360e4..fbc67bd 100644
--- a/src/gen.zig
+++ b/src/gen.zig
@@ -16,7 +16,7 @@ pub fn generate(ctx: *Context) !void {
 
     log.info("Generated index.", .{});
 
-    try genSite(ctx);
+    try genSites(ctx);
 
     log.info("Generated {d} files.", .{ctx.generated.items.len});
     log.info("Generated {d} tildes.", .{ctx.tilde});
@@ -76,57 +76,61 @@ fn genIndex(ctx: *Context) !void {
     try w.interface.flush();
 }
 
-fn genSite(ctx: *Context) !void {
+fn genSites(ctx: *Context) !void {
     for (ctx.pages.items) |p| {
-        const pname = std.fs.path.basename(p);
-
-        ctx.progress_start(pname);
-        defer ctx.progress_end();
-
-        if (std.mem.startsWith(u8, pname, "meta.")) {
+        if (std.mem.startsWith(u8, std.fs.path.basename(p), "meta.")) {
+            // meta pages
             try meta(ctx, p);
             continue;
         }
 
-        const dstname = try fs.stripExtension(p);
-        const srcpath = try fs.joinPathAndFilename(ctx.allocator(), &.{Context.params.PAGE_PATH}, &.{ dstname, ".htm" });
-        defer ctx.allocator().free(srcpath);
-        const dstpath = try fs.joinPathAndFilename(ctx.allocator(), &.{Context.params.SITE_PATH}, &.{ dstname, ".html" });
-        defer ctx.allocator().free(dstpath);
-        const rel_dstpath = try std.fs.path.relative(ctx.allocator(), Context.params.SITE_PATH, dstpath);
-        defer ctx.allocator().free(rel_dstpath);
+        // regular pages
+        try genSite(ctx, p);
+    }
+}
 
-        if (std.fs.path.dirname(rel_dstpath)) |d| {
-            const dstdir = try fs.joinPathAndFilename(ctx.allocator(), &.{Context.params.SITE_PATH}, &.{d});
-            defer ctx.allocator().free(dstdir);
+fn genSite(ctx: *Context, p: []const u8) !void {
+    ctx.progress_start(std.fs.path.basename(p));
+    defer ctx.progress_end();
 
-            try std.fs.cwd().makePath(dstdir);
-        }
+    const dstname = try fs.stripExtension(p);
+    const srcpath = try fs.joinPathAndFilename(ctx.allocator(), &.{Context.params.PAGE_PATH}, &.{ dstname, ".htm" });
+    defer ctx.allocator().free(srcpath);
+    const dstpath = try fs.joinPathAndFilename(ctx.allocator(), &.{Context.params.SITE_PATH}, &.{ dstname, ".html" });
+    defer ctx.allocator().free(dstpath);
+    const rel_dstpath = try std.fs.path.relative(ctx.allocator(), Context.params.SITE_PATH, dstpath);
+    defer ctx.allocator().free(rel_dstpath);
 
-        const srcmtime = try fs.getLastEdit(srcpath);
-        const dstmtime = try fs.getLastEdit(dstpath);
-        if (srcmtime.sec < dstmtime.sec) continue;
+    if (std.fs.path.dirname(rel_dstpath)) |d| {
+        const dstdir = try fs.joinPathAndFilename(ctx.allocator(), &.{Context.params.SITE_PATH}, &.{d});
+        defer ctx.allocator().free(dstdir);
 
-        log.debug("generating {s} at {s}", .{ p, rel_dstpath });
+        try std.fs.cwd().makePath(dstdir);
+    }
 
-        const f = try fs.FileSwapper.createAt(ctx.allocator(), dstpath);
+    const srcmtime = try fs.getLastEdit(srcpath);
+    const dstmtime = try fs.getLastEdit(dstpath);
+    if (srcmtime.sec < dstmtime.sec) return;
 
-        var buffer: [1024]u8 = undefined;
-        var w = f.writer(&buffer);
+    log.debug("generating {s} at {s}", .{ p, rel_dstpath });
+
+    const f = try fs.FileSwapper.createAt(ctx.allocator(), dstpath);
+
+    var buffer: [1024]u8 = undefined;
+    var w = f.writer(&buffer);
 
-        const page_name = try std.mem.replaceOwned(u8, ctx.allocator(), dstname, "_", " ");
-        defer ctx.allocator().free(page_name);
+    const page_name = try std.mem.replaceOwned(u8, ctx.allocator(), dstname, "_", " ");
+    defer ctx.allocator().free(page_name);
 
-        try build(ctx.allocator(), &w.interface, page_name, srcpath);
+    try build(ctx.allocator(), &w.interface, page_name, srcpath);
 
-        f.close();
+    f.close();
 
-        if (f.swapped.*) {
-            try ctx.generated.append(
-                ctx.allocator(),
-                try ctx.allocator().dupe(u8, dstpath),
-            );
-        }
+    if (f.swapped.*) {
+        try ctx.generated.append(
+            ctx.allocator(),
+            try ctx.allocator().dupe(u8, dstpath),
+        );
     }
 }
 
@@ -227,6 +231,9 @@ fn build(allocator: std.mem.Allocator, w: *std.Io.Writer, page_name: []const u8,
 // - meta.nav: used for populating the global navigation menu
 // - meta.tilde: used to create ~foobar files
 fn meta(ctx: *Context, p: []const u8) !void {
+    ctx.progress_start(std.fs.path.basename(p));
+    defer ctx.progress_end();
+
     ctx.meta += 1;
 
     if (std.mem.startsWith(u8, p, "meta.nav.")) return;
diff --git a/src/main.zig b/src/main.zig
index f47bd36..dfcedfa 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -93,6 +93,10 @@ pub const Context = struct {
     }
 };
 
+// TODO: make index smarter, it should know which files reference each other.
+// with this information, we could make better decisions whether to generate
+// output.
+//
 fn index(ctx: *Context) !void {
     // avoid reindexing
     if ((ctx.pages.items.len != 0) and (ctx.markdown.items.len != 0)) return;
@@ -126,6 +130,8 @@ fn index(ctx: *Context) !void {
                     std.log.debug("Indexing {s}", .{e.path});
                     try ctx.markdown.append(ctx.allocator(), try ctx.allocator().dupe(u8, e.path));
                 }
+
+                // XXX: scan for refs here?
             },
             else => {},
         }