From 5d9e4743e9ddb637997fc3a5f9a7f5193b5874fb Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Thu, 24 Sep 2026 19:58:04 +0200 Subject: properly handle initial build with markdown files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Robert Günzler --- src/main.zig | 80 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 46 insertions(+), 34 deletions(-) diff --git a/src/main.zig b/src/main.zig index a280863..8781f8d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -185,23 +185,24 @@ const Cli = struct { const target_dir = try Dir.init(io, Io.Dir.cwd(), target_path, .{}); defer target_dir.fd.close(io); - var work: std.ArrayList([]const u8) = .empty; - defer work.deinit(gpa); + var work: std.BufSet = .init(gpa); + defer work.deinit(); - var mdn = std.BufSet.init(gpa); + var mdn: std.BufSet = .init(gpa); defer mdn.deinit(); - var iter = root_dir.fd.iterate(); + var iter = root_dir.fd.iterateAssumeFirstIteration(); while (try iter.next(io)) |child| { if (child.kind != .file) continue; + if (std.mem.endsWith(u8, child.name, "meta.baseof.htm")) continue; + if (std.mem.endsWith(u8, child.name, "meta.nav.htm")) continue; + if (std.mem.endsWith(u8, child.name, ".md")) { try mdn.insert(child.name); - continue; + } else { + try work.insert(child.name); } - if (std.mem.endsWith(u8, child.name, "meta.baseof.htm")) continue; - if (std.mem.endsWith(u8, child.name, "meta.nav.htm")) continue; - try work.append(gpa, child.name); } var progress = if (!DEBUG) std.Progress.start(io, .{}); @@ -210,49 +211,57 @@ const Cli = struct { var mdnProgress = if (!DEBUG) progress.start("Generating markdown", mdn.count()); defer if (!DEBUG) mdnProgress.end(); - var htmProgress = if (!DEBUG) progress.start("Generating html", work.items.len); + var htmProgress = if (!DEBUG) progress.start("Generating html", work.count()); defer if (!DEBUG) htmProgress.end(); var outgoingRefs: std.StringHashMapUnmanaged([][]const u8) = .empty; defer outgoingRefs.deinit(gpa); // TODO: parallelize? - for (work.items) |path| { - const md_name = try std.fmt.allocPrint(gpa, "{s}.md", .{std.fs.path.stem(path)}); - defer gpa.free(md_name); - const md_path = try std.fs.path.join(gpa, &.{ root_path, md_name }); - defer gpa.free(md_path); - const full_path = try std.fs.path.join(gpa, &.{ root_path, path }); + + // process markdown first + var mdnIter = mdn.iterator(); + while (mdnIter.next()) |path| { + const full_path = try std.fs.path.join(gpa, &.{ root_path, path.* }); defer gpa.free(full_path); if (flags.contains("-l") or flags.contains("--list")) { // only print - if (mdn.contains(md_name)) { - try Io.File.stdout().writeStreamingAll(io, md_path); - try Io.File.stdout().writeStreamingAll(io, "\n"); - } try Io.File.stdout().writeStreamingAll(io, full_path); try Io.File.stdout().writeStreamingAll(io, "\n"); continue; } - // process markdown first (if required) - if (mdn.contains(md_name)) { - const mdnNode = if (!DEBUG) mdnProgress.start(md_name, 0); - defer if (!DEBUG) mdnNode.end(); + const mdnNode = if (!DEBUG) mdnProgress.start(path.*, 0); + defer if (!DEBUG) mdnNode.end(); + + var source = try File.init(io, root_dir, path.*, null); + defer source.close(io); + + const b = try Builder.init(gpa, root_dir, source, target_dir, .{}); + defer b.deinit(gpa); + try b.build(io, gpa); - var source = try File.init(io, root_dir, md_name, null); - defer source.close(io); + const htm_name = try std.fmt.allocPrint(gpa, "{s}.htm", .{std.fs.path.stem(path.*)}); + try work.insert(htm_name); + } + + var workIter = work.iterator(); + while (workIter.next()) |path| { + const full_path = try std.fs.path.join(gpa, &.{ root_path, path.* }); + defer gpa.free(full_path); - const b = try Builder.init(gpa, root_dir, source, target_dir, .{}); - defer b.deinit(gpa); - try b.build(io, gpa); + if (flags.contains("-l") or flags.contains("--list")) { + // only print + try Io.File.stdout().writeStreamingAll(io, full_path); + try Io.File.stdout().writeStreamingAll(io, "\n"); + continue; } - const htmNode = if (!DEBUG) htmProgress.start(path, 0); + const htmNode = if (!DEBUG) htmProgress.start(path.*, 0); defer if (!DEBUG) htmNode.end(); - var source = try File.init(io, root_dir, path, null); + var source = try File.init(io, root_dir, path.*, null); defer source.close(io); const b = try Builder.init(gpa, root_dir, source, target_dir, .{}); @@ -266,11 +275,14 @@ const Cli = struct { try outgoingRefs.put(gpa, std.fs.path.stem(source.path), refs.items); } + if (LIST) return; + // filter index for post processing - var index: std.ArrayList([]const u8) = try .initCapacity(gpa, work.items.len); - while (work.pop()) |e| { - if (std.mem.startsWith(u8, std.fs.path.stem(e), "meta.")) continue; - index.appendAssumeCapacity(e); + var index: std.ArrayList([]const u8) = try .initCapacity(gpa, work.count()); + var workIter2 = work.iterator(); + while (workIter2.next()) |e| { + if (std.mem.startsWith(u8, std.fs.path.stem(e.*), "meta.")) continue; + index.appendAssumeCapacity(e.*); } { -- cgit 1.4.1