]> git.frustrated-labs.net Git - frustrated-functor.dev.git/commitdiff
feat: add rss formatter to turn markdown post into rss entry
authorAlexander Goussas <[email protected]>
Sun, 3 May 2026 20:31:42 +0000 (15:31 -0500)
committerAlexander Goussas <[email protected]>
Sun, 3 May 2026 20:31:42 +0000 (15:31 -0500)
.gitignore
bin/blog-processor/src/root.zig
bin/blog-processor/src/rss_formatter.zig [new file with mode: 0644]

index f10c28b89d9dafc7cf2b361f0b9409f51435bd89..0bfa928d178312081ed7033228b49829001c9152 100644 (file)
@@ -2,3 +2,4 @@ zig-out
 .zig-cache
 !public/index.html
 public/*.html
+sample.rss
index 9270be57757abea3a9a4a75cfa4095ea86e45193..8634ea0cd35ebfa8fd71a069c598b1a4a6e4a7dd 100644 (file)
@@ -52,4 +52,5 @@ test "all tests" {
     _ = @import("./markdown_parser.zig");
     _ = @import("./html_formatter.zig");
     _ = @import("./string_utils.zig");
+    _ = @import("./rss_formatter.zig");
 }
diff --git a/bin/blog-processor/src/rss_formatter.zig b/bin/blog-processor/src/rss_formatter.zig
new file mode 100644 (file)
index 0000000..a6fabfc
--- /dev/null
@@ -0,0 +1,80 @@
+const std = @import("std");
+
+const md = @import("./markdown_parser.zig");
+
+pub const RssFormatter = struct {
+    buffer: std.ArrayList(u8),
+    alloc: std.mem.Allocator,
+
+    /// Create a new RssFormatter that will use the provided allocator.
+    pub fn init(alloc: std.mem.Allocator) @This() {
+        return .{
+            .buffer = .empty,
+            .alloc = alloc, 
+        };
+    }
+
+    pub fn deinit(self: *@This()) void {
+        self.buffer.deinit(self.alloc);
+    }
+
+    /// Format the provided 
+    pub fn format(self: *@This(), doc: md.MarkdownDoc) ![] const u8 {
+        try self.buffer.appendSlice(
+            self.alloc,
+            \\<item>
+            \\<author>[email protected] (Alexander Goussas)</author>
+            \\<pubDate>
+        );
+
+        try self.buffer.appendSlice(self.alloc, doc.date);
+        try self.buffer.appendSlice(
+            self.alloc,
+            \\</pubDate>
+            \\<title>
+        );
+
+        try self.buffer.appendSlice(self.alloc, doc.title);
+        try self.buffer.appendSlice(
+            self.alloc,
+            \\</title>
+            \\</item>
+        );
+
+        // TODO: Add link
+        // TODO: Add guid (same as link)
+        // TODO: Add a description
+        
+        return self.buffer.items;
+    }
+};
+
+test "rss formatter can format markdown document" {
+    const alloc = std.testing.allocator;
+    const doc = 
+        \\---
+        \\title: A post title
+        \\date: 12 april 2026
+        \\---
+        \\# A post title
+        \\Aujourd'hui, mamam est morte.
+        ;
+
+    var formatter = RssFormatter.init(alloc);
+    defer formatter.deinit();
+
+    var mdDoc = try md.MarkdownDoc.parse(doc, alloc);
+    defer mdDoc.deinit(alloc);
+
+    const result = try formatter.format(mdDoc);
+
+    try std.testing.expectEqualStrings(
+        \\<item>
+        \\<author>[email protected] (Alexander Goussas)</author>
+        \\<pubDate>12 april 2026</pubDate>
+        \\<title>A post title</title>
+        \\</item>
+        ,
+        result
+    );
+}