// TODO: just let compiler infer error set through recursive calls // depends on: https://github.com/ziglang/zig/issues/2971 // This code is heavily insprired by Devine's implementation in C: // https://github.com/hundredrabbits/100r.co/blob/main/src/main.c and his // personal site https://github.com/XXIIVV/Oscean const std = @import("std"); const gen = @import("gen.zig"); const log = std.log.scoped(.root); // tune std.log options pub const std_options = std.Options{ .log_level = .info, }; pub const Context = struct { arena: std.heap.ArenaAllocator, pages: [][]const u8 = undefined, generated: usize = 0, meta: usize = 0, tilde: usize = 0, pub const params = struct { pub const PAGE_PATH = "src/inc"; pub const SITE_PATH = "site"; pub const LINK_PATH = "links"; pub const SITE_NAME = "gzr.im"; pub const LICENSE_HOLDER = "robertgzr"; pub const LICENSE_YEAR = "2025"; pub const LICENSE_NAME = "BY-NC-SA 4.0"; pub const LICENSE_URL = "https://git.sr.ht/~robertgzr/site/blob/master/LICENSE.content"; pub const SOURCE_URL = "https://git.sr.ht/~robertgzr/site/tree/master"; pub const PUBLIC_INBOX = "~robertgzr/public-inbox@lists.sr.ht"; }; fn new(parent_alloc: std.mem.Allocator) *Context { const ctx = parent_alloc.create(Context) catch |err| @panic(@errorName(err)); ctx.* = .{ .arena = std.heap.ArenaAllocator.init(parent_alloc), }; return ctx; } fn deinit(self: *Context) void { self.arena.deinit(); self.arena.child_allocator.destroy(self); } pub fn allocator(self: *Context) std.mem.Allocator { return self.arena.allocator(); } }; fn index(ctx: *Context) !void { var root = std.fs.cwd().openDir(Context.params.PAGE_PATH, .{ .iterate = true }) catch |err| switch (err) { error.FileNotFound => { std.log.err("Missing {s} folder.", .{Context.params.PAGE_PATH}); std.os.linux.exit(1); }, else => |e| return e, }; defer root.close(); var pages = std.ArrayList([]const u8).init(ctx.allocator()); defer pages.deinit(); var walk = try root.walk(ctx.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 ctx.allocator().dupe(u8, e.path)); } }, else => {}, } } ctx.pages = try pages.toOwnedSlice(); return; } pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator(); defer _ = gpa.deinit(); var ctx = Context.new(allocator); defer ctx.deinit(); index(ctx) catch { log.err("Failed to index pages.", .{}); std.os.linux.exit(1); }; log.info("Indexed {d} pages.", .{ctx.pages.len}); Cli.parse()(ctx); } const RunFn = fn (*Context) void; const Cli = struct { fn parse() *const RunFn { var argv = std.process.args(); const more = argv.skip(); // skip arg0 if (!more) return &Cli.help_cmd; const next_arg = argv.next() orelse return &Cli.help_cmd; if (std.mem.eql(u8, next_arg, "gen")) { return &Cli.gen_cmd; } else { return &Cli.help_cmd; } } fn help_cmd(_: *Context) void { std.debug.print("usage: rbl [gen]\n", .{}); std.os.linux.exit(0); } fn gen_cmd(ctx: *Context) void { gen.generate(ctx) catch { log.err("Failed to generate site.", .{}); std.os.linux.exit(1); }; } };