about summary refs log tree commit diff
path: root/src/main.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/main.zig b/src/main.zig
index febd896..0fb913a 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -32,6 +32,9 @@ const Cli = struct {
             \\  build [-f, --force] SOURCE-FILE TARGET-DIR
             \\    builds while resolving references
             \\
+            \\  gen ROOT-DIR TARGET-DIR
+            \\    runs build for relevant files
+            \\
             \\
         , .{});
     }
@@ -50,6 +53,7 @@ const Cli = struct {
             if (std.mem.eql(u8, o, "--help")) return Cli.usage();
             if (std.mem.eql(u8, o, "-h")) return Cli.usage();
             if (std.mem.eql(u8, o, "build")) return Cli.build(io, gpa, &opts);
+            if (std.mem.eql(u8, o, "gen")) return Cli.gen(io, gpa, &opts);
         }
         return Cli.usage();
     }
@@ -90,6 +94,67 @@ const Cli = struct {
         defer b.deinit(gpa);
         try b.build(io, gpa);
     }
+
+    fn gen(io: Io, gpa: Allocator, opts: *std.process.Args.Iterator) !void {
+        var args: std.ArrayList([]const u8) = .empty;
+        defer args.deinit(gpa);
+
+        while (opts.next()) |o| {
+            if (std.mem.eql(u8, o, "help")) return Cli.usage();
+            if (std.mem.eql(u8, o, "--help")) return Cli.usage();
+            if (std.mem.eql(u8, o, "-h")) return Cli.usage();
+            if (std.mem.eql(u8, o, "--force")) FORCE = true and continue;
+            if (std.mem.eql(u8, o, "-f")) FORCE = true and continue;
+            try args.append(gpa, o);
+        }
+
+        if (args.items.len < 2) return Cli.usage();
+
+        const root_path = args.items[0];
+        const target_path = args.items[1];
+
+        log.info("gen : target={s} root={s}", .{ target_path, root_path });
+
+        const root_dir = try Io.Dir.cwd().openDir(io, root_path, .{ .iterate = true });
+        defer root_dir.close(io);
+
+        // open/create destination dir
+        const target_dir = try Io.Dir.cwd().createDirPathOpen(io, target_path, .{});
+        defer target_dir.close(io);
+
+        // TODO: parallelize?
+
+        var work: std.ArrayList([]const u8) = .empty;
+        defer work.deinit(gpa);
+
+        var iter = root_dir.iterate();
+        while (try iter.next(io)) |child| {
+            if (child.kind != .file) continue;
+
+            if (std.mem.endsWith(u8, child.name, ".md")) continue;
+            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, .{});
+        defer if (!DEBUG) progress.end();
+
+        var htmProgress = if (!DEBUG) progress.start("Generating html", work.items.len);
+        defer if (!DEBUG) htmProgress.end();
+
+        for (work.items) |path| {
+            const htmNode = if (!DEBUG) htmProgress.start(path, 0);
+            defer if (!DEBUG) htmNode.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);
+        }
+    }
 };
 
 const File = struct {