]> git.frustrated-labs.net Git - frustrated-functor.dev.git/commitdiff
feat: format an empty RSS document
authorAlexander Goussas <[email protected]>
Sun, 3 May 2026 21:11:43 +0000 (16:11 -0500)
committerAlexander Goussas <[email protected]>
Sun, 3 May 2026 21:11:43 +0000 (16:11 -0500)
bin/blog-processor/src/root.zig
bin/blog-processor/src/rss_document.zig [new file with mode: 0644]

index 8634ea0cd35ebfa8fd71a069c598b1a4a6e4a7dd..6a8db43a6260bc44a5ba4b9bdfb18f1dde17b9a8 100644 (file)
@@ -53,4 +53,5 @@ test "all tests" {
     _ = @import("./html_formatter.zig");
     _ = @import("./string_utils.zig");
     _ = @import("./rss_formatter.zig");
+    _ = @import("./rss_document.zig");
 }
diff --git a/bin/blog-processor/src/rss_document.zig b/bin/blog-processor/src/rss_document.zig
new file mode 100644 (file)
index 0000000..7b7cbe7
--- /dev/null
@@ -0,0 +1,60 @@
+const std = @import("std");
+
+const rss = @import("./rss_formatter.zig");
+const md = @import("./markdown_parser.zig");
+
+const HEADER = 
+    \\<?xml version="1.0" encoding="utf-8" standalone="yes"?>
+    \\<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
+    \\<channel>
+    \\<title>Posts on Alexander Goussas</title>
+    \\<link>https://frustrated-functor.dev</link>
+    \\<description></description>
+    \\<generator></generator>
+    \\<language>en-us</language>
+    \\<managingEditor>[email protected] (Alexander Goussas)</managingEditor>
+    \\<webMaster>[email protected] (Alexander Goussas)</webMaster>
+    \\<lastBuildDate></lastBuildDate>
+    \\<atom:link href="https://frustrated-functor.dev/rss.xml" rel="self" type="application/rss+xml"/>
+    ;
+
+const FOOTER = 
+    \\</channel>
+    \\</rss>
+    ;
+
+// TODO: Create a MarkdownMetaDoc that includes the post URL.
+
+pub const RssDocument = struct {
+    buffer: std.ArrayList(u8) = .empty,
+
+    pub fn md2rss(self: *@This(), alloc: std.mem.Allocator, docs: std.ArrayList(md.MarkdownDoc)) ![]const u8 {
+        try self.buffer.appendSlice(alloc, HEADER);
+
+        for (docs.items) |doc| {
+            // TODO: Thread URL here.
+            // TODO: Add an API to RSS formatter to be able to reset the buffer.
+            var rssFormatter = rss.RssFormatter.init(alloc, "");
+            defer rssFormatter.deinit();
+
+            const formattedEntry = try rssFormatter.format(doc);
+            try self.buffer.appendSlice(alloc, formattedEntry);
+        }
+
+        try self.buffer.appendSlice(alloc, FOOTER);
+
+        return self.buffer.toOwnedSlice(alloc);
+    }
+};
+
+test "rss document can format an empty document" {
+    const alloc = std.testing.allocator;
+
+    var rssDoc: RssDocument = .{};
+
+    const docs: std.ArrayList(md.MarkdownDoc) = .empty;
+    const result = try rssDoc.md2rss(alloc, docs);
+    defer alloc.free(result);
+
+    try std.testing.expectEqualStrings(HEADER ++ FOOTER, result);
+}