diff options
Diffstat (limited to 'converter')
-rw-r--r-- | converter/build.zig | 4 | ||||
-rw-r--r-- | converter/build.zig.zon | 9 | ||||
-rw-r--r-- | converter/convert_posts.zig | 0 | ||||
-rw-r--r-- | converter/src/main.zig | 37 |
4 files changed, 39 insertions, 11 deletions
diff --git a/converter/build.zig b/converter/build.zig index 10f855a..f1563cf 100644 --- a/converter/build.zig +++ b/converter/build.zig @@ -10,8 +10,8 @@ pub fn build(b: *std.Build) void { .target = target, .optimize = optimize, }); - // const zig_yaml = b.dependency("zig-yaml", .{}); - // exe.root_module.addImport("yaml", zig_yaml.module("yaml")); + const ziggy = b.dependency("ziggy", .{}); + exe.root_module.addImport("ziggy", ziggy.module("ziggy")); b.installArtifact(exe); diff --git a/converter/build.zig.zon b/converter/build.zig.zon index 6e262b1..1d3de63 100644 --- a/converter/build.zig.zon +++ b/converter/build.zig.zon @@ -2,11 +2,10 @@ .name = "converter", .version = "0.0.0", .dependencies = .{ - // It doesn't work! - //.@"zig-yaml" = .{ - // .url = "https://github.com/kubkon/zig-yaml/archive/refs/heads/main.tar.gz", - // .hash = "122084941d6e06491a85e1356c7cca24a078103d34155e34a10a16a53f420d6bc37b", - //}, + .ziggy = .{ + .url = "https://github.com/kristoff-it/ziggy/archive/refs/heads/main.tar.gz", + .hash = "1220037474a924385b0d2ccb0e5c416c595f43f93a3450d55f8b6d5ad6f3ba091ca5", + } }, .paths = .{ "", diff --git a/converter/convert_posts.zig b/converter/convert_posts.zig deleted file mode 100644 index e69de29..0000000 --- a/converter/convert_posts.zig +++ /dev/null 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 +} |