diff options
author | Martin Ashby <martin@ashbysoft.com> | 2024-03-25 21:36:21 +0000 |
---|---|---|
committer | Martin Ashby <martin@ashbysoft.com> | 2024-03-25 21:36:21 +0000 |
commit | 92787c159262a57fa20b2eb05ed710e1e6cfca96 (patch) | |
tree | 508cab2af110a063767564571a98d157926bfb05 /converter/src | |
parent | e12c0d23ad72ffa9389d90311453db535f57e450 (diff) | |
download | mfashby.net-92787c159262a57fa20b2eb05ed710e1e6cfca96.tar.gz mfashby.net-92787c159262a57fa20b2eb05ed710e1e6cfca96.tar.bz2 mfashby.net-92787c159262a57fa20b2eb05ed710e1e6cfca96.tar.xz mfashby.net-92787c159262a57fa20b2eb05ed710e1e6cfca96.zip |
Final conversion to Zine
Diffstat (limited to 'converter/src')
-rw-r--r-- | converter/src/main.zig | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/converter/src/main.zig b/converter/src/main.zig index bc60e35..edaf51f 100644 --- a/converter/src/main.zig +++ b/converter/src/main.zig @@ -1,8 +1,23 @@ const std = @import("std"); +const ziggy = @import("ziggy"); + +const Page = struct { + title: ?[]const u8 = null, + author: ?[]const u8 = null, + date: ?[]const u8 = null, + layout: ?[]const u8 = null, + // draft: ?bool = null, + // params: ?struct { + // comments: bool, + // } = null, +}; pub fn main() !void { const a = std.heap.page_allocator; - const contentdir = try std.fs.cwd().openDir("../content", .{.iterate = true}); + var args = std.process.args(); + if (!args.skip()) @panic("errors kipping program name?"); + const contentdirname = args.next() orelse return error.NoDir; + const contentdir = try std.fs.cwd().openDir(contentdirname, .{ .iterate = true }); var walker = try contentdir.walk(a); while (try walker.next()) |we| { if (std.mem.endsWith(u8, we.basename, ".md")) { @@ -11,7 +26,7 @@ pub fn main() !void { 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); + var childproc = std.process.Child.init(&.{ "yq", "-o", "json" }, a); childproc.stdin_behavior = .Pipe; childproc.stdout_behavior = .Pipe; childproc.stderr_behavior = .Pipe; @@ -33,9 +48,23 @@ pub fn main() !void { return error.ProcessError; } const fm_json = try stdout.toOwnedSlice(); - const newfile = try std.mem.concat(a, u8, &.{"---\n", fm_json, file[end..]}); + // Now parse it and add missing fields + const fm_json_parsed_value = try std.json.parseFromSlice(Page, a, fm_json, .{ + .ignore_unknown_fields = true, + }); + defer fm_json_parsed_value.deinit(); + var fm_json_parsed = fm_json_parsed_value.value; + fm_json_parsed.title = fm_json_parsed.title orelse ""; + fm_json_parsed.author = fm_json_parsed.author orelse "Martin Ashby"; + fm_json_parsed.date = fm_json_parsed.date orelse "1900-01-01T00:00:00Z"; + fm_json_parsed.layout = fm_json_parsed.layout orelse "single.html"; + + var out = std.ArrayList(u8).init(a); + defer out.deinit(); + try ziggy.stringify(fm_json_parsed, .{.whitespace = .space_2}, out.writer()); + const newfile = try std.mem.concat(a, u8, &.{ "---\n", out.items, "\n", file[end..] }); try we.dir.writeFile(we.basename, newfile); } } std.log.info("done!", .{}); -}
\ No newline at end of file +} |