about summary refs log tree commit diff
path: root/src/main.zig
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2026-09-24 18:49:06 +0200
committerRobert Günzler <r@gnzler.io>2026-10-02 10:39:48 +0200
commitd470bfa5bb13d44ea799e332ecbaac8e493c4f2c (patch)
tree18f0b96ceff0e53387a61f0f3c188fdcba76d1a7 /src/main.zig
parent34997a61cc8e37e91ce6b64d9234bbd93395d92d (diff)
downloadsite-d470bfa5bb13d44ea799e332ecbaac8e493c4f2c.tar.gz
properly support setting link prefix
Signed-off-by: Robert Günzler <r@gnzler.io>
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig32
1 files changed, 18 insertions, 14 deletions
diff --git a/src/main.zig b/src/main.zig
index fcf2723..5988e75 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -26,6 +26,7 @@ pub fn main(init: std.process.Init) !void {
 
 // TODO: make this not a global
 var FORCE: bool = false;
+var LINK_PREFIX: []const u8 = "/site";
 
 const Cli = struct {
     const FlagSet = std.BufSet;
@@ -121,6 +122,7 @@ const Cli = struct {
 
     fn run(init: std.process.Init) !void {
         if (init.environ_map.get("RBC_FORCE")) |_| FORCE = true;
+        if (init.environ_map.get("RBC_LINK_PREFIX")) |e| LINK_PREFIX = e;
 
         // parse options
         var opts = init.minimal.args.iterate();
@@ -478,9 +480,7 @@ const Builder = struct {
     index: ?std.ArrayList([]const u8) = null,
 
     pub const Options = struct {
-        // TODO: this is a bit of a hack no? we should just track the target
-        // path and use that
-        offset_from_siteroot: ?[]const u8 = "/site",
+        link_prefix: []const u8 = "/",
     };
 
     const DELIM_START: []const u8 = "{[";
@@ -500,6 +500,8 @@ const Builder = struct {
             .target_dir = target_dir,
             .options = options,
         };
+
+        b.options.link_prefix = LINK_PREFIX;
         return b;
     }
 
@@ -671,13 +673,15 @@ const Builder = struct {
                 \\<ul>
                 \\
             );
-            for (self.index.?.items) |e| try w.print(
-                \\<li><a href="{s}">{s}</a></li>
-                \\
-            , .{
-                try formatLink(gpa, std.fs.path.stem(e), self.options.offset_from_siteroot),
-                try formatName(gpa, std.fs.path.stem(e)),
-            });
+            for (self.index.?.items) |e| {
+                try w.print(
+                    \\<li><a href="{s}">{s}</a></li>
+                    \\
+                , .{
+                    try formatLink(gpa, std.fs.path.stem(e), self.options.link_prefix),
+                    try formatName(gpa, std.fs.path.stem(e)),
+                });
+            }
             try w.writeAll(
                 \\</ul>
                 \\</nav>
@@ -750,7 +754,7 @@ const Builder = struct {
 
         const ref_name = try formatName(gpa, ref);
         defer gpa.free(ref_name);
-        const ref_link = try formatLink(gpa, ref, self.options.offset_from_siteroot);
+        const ref_link = try formatLink(gpa, ref, self.options.link_prefix);
         defer gpa.free(ref_link);
 
         try w.print(
@@ -771,7 +775,7 @@ const Builder = struct {
         ref_name: []const u8,
         w: *std.Io.Writer,
     ) !void {
-        const link = try formatLink(gpa, ref_name, self.options.offset_from_siteroot);
+        const link = try formatLink(gpa, ref_name, self.options.link_prefix);
         defer gpa.free(link);
         try w.writeAll(link);
     }
@@ -896,10 +900,10 @@ fn formatName(gpa: Allocator, name: []const u8) ![]u8 {
     return try std.mem.replaceOwned(u8, gpa, name, "_", " ");
 }
 
-fn formatLink(gpa: Allocator, name: []const u8, pathPrefix: ?[]const u8) ![]u8 {
+fn formatLink(gpa: Allocator, name: []const u8, prefix: []const u8) ![]u8 {
     const link_name = try std.fmt.allocPrint(gpa, "{s}.html", .{name});
     defer gpa.free(link_name);
-    return try std.fs.path.join(gpa, &.{ pathPrefix orelse "/", link_name });
+    return try std.fs.path.join(gpa, &.{ prefix, link_name });
 }
 
 fn targetExists(