From e12c0d23ad72ffa9389d90311453db535f57e450 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Mon, 4 Mar 2024 16:24:45 +0000 Subject: Prepare to move to zine static site generator instead of hugo https://zine-ssg.io/documentation/ --- converter/src/main.zig | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 converter/src/main.zig (limited to 'converter/src/main.zig') diff --git a/converter/src/main.zig b/converter/src/main.zig new file mode 100644 index 0000000..bc60e35 --- /dev/null +++ b/converter/src/main.zig @@ -0,0 +1,41 @@ +const std = @import("std"); + +pub fn main() !void { + const a = std.heap.page_allocator; + const contentdir = try std.fs.cwd().openDir("../content", .{.iterate = true}); + var walker = try contentdir.walk(a); + while (try walker.next()) |we| { + if (std.mem.endsWith(u8, we.basename, ".md")) { + std.log.info("converting file {s}", .{we.basename}); + const file = try we.dir.readFileAlloc(a, we.basename, 1_000_000); + const start = (std.mem.indexOf(u8, file, "---\n") orelse return error.NoFrontmatter) + 4; + const end = (std.mem.lastIndexOf(u8, file, "---\n") orelse return error.NoFrontMatter); + + var childproc = std.process.Child.init(&.{"yq", "-o", "json"}, a); + childproc.stdin_behavior = .Pipe; + childproc.stdout_behavior = .Pipe; + childproc.stderr_behavior = .Pipe; + std.log.info("spawn", .{}); + try childproc.spawn(); + std.log.info("writeAll", .{}); + try childproc.stdin.?.writeAll(file[start..end]); + childproc.stdin.?.close(); + childproc.stdin = null; + + var stdout = std.ArrayList(u8).init(a); + var stderr = std.ArrayList(u8).init(a); + std.log.info("collectOutput", .{}); + try childproc.collectOutput(&stdout, &stderr, 1_000_000); + std.log.info("got output {s}", .{stdout.items}); + std.log.info("wait", .{}); + const term = try childproc.wait(); + if (term.Exited != 0) { + return error.ProcessError; + } + const fm_json = try stdout.toOwnedSlice(); + const newfile = try std.mem.concat(a, u8, &.{"---\n", fm_json, file[end..]}); + try we.dir.writeFile(we.basename, newfile); + } + } + std.log.info("done!", .{}); +} \ No newline at end of file -- cgit v1.2.3-ZIG