From 00e8ec8f029d3fb248139bae366220d5b01e8cd8 Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Sat, 17 Jan 2026 17:17:09 +0100 Subject: fix for zig 0.15 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit writergate Signed-off-by: Robert Günzler --- src/gen.zig | 80 ++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 33 deletions(-) (limited to 'src/gen.zig') diff --git a/src/gen.zig b/src/gen.zig index 3245e7c..eb768e5 100644 --- a/src/gen.zig +++ b/src/gen.zig @@ -65,7 +65,10 @@ fn genIndex(ctx: *Context) !void { const f = try fs.FileSwapper.createAt(ctx.allocator(), srcpathIndex); defer f.close(); - try f.writer().writeAll( + var buffer: [1024]u8 = undefined; + var w = f.writer(&buffer); + + try w.interface.writeAll( \\ \\ ); + try w.interface.flush(); + if (!try f.diff()) try f.undo(); } @@ -138,16 +143,19 @@ fn genSite(ctx: *Context) !void { const f = try fs.FileSwapper.createAt(ctx.allocator(), dstpath); defer f.close(); + 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); - try build(ctx.allocator(), f.writer().any(), page_name, srcpath); + try build(ctx.allocator(), &w.interface, page_name, srcpath); ctx.generated += 1; } } // build a single html file -fn build(allocator: std.mem.Allocator, w: std.io.AnyWriter, page_name: []const u8, srcpath: []const u8) !void { +fn build(allocator: std.mem.Allocator, w: *std.Io.Writer, page_name: []const u8, srcpath: []const u8) !void { const mtime = try fs.getMtime(srcpath); const mtime_str = try fs.formatTimespec(allocator, mtime, "%F"); defer allocator.free(mtime_str); @@ -235,6 +243,8 @@ fn build(allocator: std.mem.Allocator, w: std.io.AnyWriter, page_name: []const u \\ \\ ); + + try w.flush(); } // build a virtual html file, currently either: @@ -266,7 +276,10 @@ fn meta(ctx: *Context, p: []const u8) !void { const f = try fs.FileSwapper.createAt(ctx.allocator(), dstpath); defer f.close(); - try f.writer().print( + var buffer: [1024]u8 = undefined; + var w = f.writer(&buffer); + + try w.interface.print( \\ \\ \\ @@ -287,6 +300,7 @@ fn meta(ctx: *Context, p: []const u8) !void { , .{ .LINK = link, }); + try w.interface.flush(); ctx.tilde += 1; return; @@ -296,7 +310,10 @@ fn meta(ctx: *Context, p: []const u8) !void { } // parses srcpath, resolves refs and portals and writes everything to w -fn inject(allocator: std.mem.Allocator, w: std.io.AnyWriter, srcpath: []const u8) !void { +fn inject(allocator: std.mem.Allocator, w: *std.Io.Writer, srcpath: []const u8) !void { + var arena = std.heap.ArenaAllocator.init(allocator); + defer arena.deinit(); + const inc = std.fs.cwd().openFile(srcpath, .{ .mode = .read_only }) catch |err| { if (err == error.FileNotFound) log.warn("Missing {s} include.", .{srcpath}); @@ -305,30 +322,27 @@ fn inject(allocator: std.mem.Allocator, w: std.io.AnyWriter, srcpath: []const u8 }; defer inc.close(); - var buf_writer = std.io.bufferedWriter(w); - var out_stream = buf_writer.writer(); + var buffer: [1024]u8 = undefined; + var r = inc.readerStreaming(&buffer); - var buf_reader = std.io.bufferedReader(inc.reader()); - var in_stream = buf_reader.reader(); + var chunkw = std.Io.Writer.Allocating.init(arena.allocator()); + defer chunkw.deinit(); - var arena = std.heap.ArenaAllocator.init(allocator); - defer arena.deinit(); + while (true) { + defer chunkw.clearRetainingCapacity(); // reset chunk + + _ = r.interface.streamDelimiter(&chunkw.writer, '\n') catch |err| + if (err == error.EndOfStream) break + else return err; - while (in_stream.readUntilDelimiterOrEofAlloc( - arena.allocator(), - '\n', - buf_reader.buf.len, - ) catch { - @panic("Failed to read from stream"); - }) |chunk_| { - defer _ = arena.reset(.free_all); + _ = r.interface.toss(1); // skip newline delimiter - var chunk = chunk_; + var chunk = chunkw.written(); templating: while (true) { if (std.mem.indexOfPosLinear(u8, chunk, 0, "{")) |start| { if (std.mem.indexOfPosLinear(u8, chunk, start + 1, "}")) |end| { - try out_stream.writeAll(chunk[0..start]); - template(allocator, out_stream.any(), chunk[start + 1 .. end]) catch |err| switch (err) { + try w.writeAll(chunk[0..start]); + template(allocator, w, chunk[start + 1 .. end]) catch |err| switch (err) { error.FileNotFound => { // log.warn("ref {s} not found", .{chunk[start + 1 .. end]}); // trim already written chunk and exit templating @@ -342,16 +356,16 @@ fn inject(allocator: std.mem.Allocator, w: std.io.AnyWriter, srcpath: []const u8 } else break :templating; } - try out_stream.writeAll(chunk); - try out_stream.writeByte('\n'); - try buf_writer.flush(); + try w.writeAll(chunk); + try w.writeByte('\n'); + try w.flush(); } - try buf_writer.flush(); + try w.flush(); } // implements various reference types that can be used in .htm templates -fn template(allocator: std.mem.Allocator, w: std.io.AnyWriter, refname: []const u8) anyerror!void { +fn template(allocator: std.mem.Allocator, w: *std.Io.Writer, refname: []const u8) anyerror!void { switch (refname[0]) { '/' => return try portal(allocator, w, refname[1..], false), '$' => return try ref_link(allocator, w, refname[1..]), @@ -393,7 +407,7 @@ const Page = struct { } }; -fn portal(allocator: std.mem.Allocator, w: std.io.AnyWriter, refname: []const u8, insert_heading: bool) anyerror!void { +fn portal(allocator: std.mem.Allocator, w: *std.Io.Writer, refname: []const u8, insert_heading: bool) anyerror!void { const page = try Page.from_ref(allocator, refname); defer page.deinit(); @@ -412,7 +426,7 @@ fn portal(allocator: std.mem.Allocator, w: std.io.AnyWriter, refname: []const u8 // XXX: track ref count? } -fn ref(allocator: std.mem.Allocator, w: std.io.AnyWriter, refname: []const u8) anyerror!void { +fn ref(allocator: std.mem.Allocator, w: *std.Io.Writer, refname: []const u8) anyerror!void { const page = try Page.from_ref(allocator, refname); defer page.deinit(); @@ -427,14 +441,14 @@ fn ref(allocator: std.mem.Allocator, w: std.io.AnyWriter, refname: []const u8) a // XXX: track ref count? } -fn ref_link(allocator: std.mem.Allocator, w: std.io.AnyWriter, refname: []const u8) anyerror!void { +fn ref_link(allocator: std.mem.Allocator, w: *std.Io.Writer, refname: []const u8) anyerror!void { const page = try Page.from_ref(allocator, refname); defer page.deinit(); try w.print("/{s}{s}", .{ page.html, page.anchor }); } -fn ref_name(allocator: std.mem.Allocator, w: std.io.AnyWriter, refname: []const u8) anyerror!void { +fn ref_name(allocator: std.mem.Allocator, w: *std.Io.Writer, refname: []const u8) anyerror!void { const page = try Page.from_ref(allocator, refname); defer page.deinit(); -- cgit 1.4.1