const std = @import("std"); const datetime = @import("datetime.zig"); const Date = datetime.Date; const string = []const u8; // TODO: Create something that can serialize the markdown // TODO: Create a content structure // TODO: Support native github flavored markdown // TODO: Enable node graph support inside the markdown // TODO: Add a way to deserialize tags into an array of strings const ArticleError = error{Deserialize}; fn FrontmatterKey(comptime value_type: type) type { return struct { value: value_type }; } const Title = FrontmatterKey(string); const Tags = FrontmatterKey([]string); pub const Article = struct { title: Title, date: string, tags: ?Tags, /// Initializes frontmatter directly pub fn init(title: string, date: Date, tags: []string) Article { return Article{ .title = title, .date = date, .tags = tags }; } /// Initializes frontmatter from a string pub fn fromStr(content: string) !void { // Parse out the frontmatter var tok = std.mem.tokenizeSequence(u8, content, "\n"); var in_frontmatter_block = false; var title: Title = undefined; var date: Date = undefined; // var tags: []string = undefined; while (tok.next()) |line| { if (std.mem.eql(u8, line, "---")) { in_frontmatter_block = !in_frontmatter_block; continue; } // Parsing frontmatter if (in_frontmatter_block) { // Divide the line into k: v var metakey = std.mem.tokenizeSequence(u8, line, ":"); while (metakey.next()) |key| { // Peek at the next item for the expected value const value = metakey.rest(); // Parse the title if (std.mem.eql(u8, key, "title")) { const title_value = @as(string, value); std.debug.print("TITLE: {s}\n", .{title_value}); title = Title{ .value = title_value }; break; } // Parse the date if (std.mem.eql(u8, key, "date")) { const whitespace = " \t\n\r"; const date_trimmed = std.mem.trim(u8, value, whitespace); date = try Date.fromStr(@as(string, date_trimmed)); std.debug.print("DATE: {any}\n", .{date}); break; } // Parse the tags if (std.mem.eql(u8, key, "tags")) { // TODO: Parse tags break; } std.debug.print("unknown property\n", .{}); return ArticleError.Deserialize; } } // Parsing content if (!in_frontmatter_block) { std.debug.print("CONTENT: {s}\n", .{line}); } } // If we're still in a frontmatter block, it's an invalid file if (in_frontmatter_block) { return ArticleError.Deserialize; } } };