pub const RssFormatter = struct {
buffer: std.ArrayList(u8),
alloc: std.mem.Allocator,
+ /// URL of the live post
+ url: []const u8,
/// Create a new RssFormatter that will use the provided allocator.
- pub fn init(alloc: std.mem.Allocator) @This() {
+ pub fn init(alloc: std.mem.Allocator, url: []const u8) @This() {
return .{
.buffer = .empty,
.alloc = alloc,
+ .url = url,
};
}
self.alloc,
\\<item>
- \\<pubDate>
+ \\
);
- try self.buffer.appendSlice(self.alloc, doc.date);
- try self.buffer.appendSlice(
- self.alloc,
- \\</pubDate>
- \\<title>
- );
+ try self.tag("<pubDate>", "</pubDate>", doc.date);
+ try self.tag("<title>", "</title>", doc.title);
+ try self.tag("<guid>", "</guid>", self.url);
+ try self.tag("<link>", "</link>", self.url);
- 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)
+ try self.buffer.appendSlice(self.alloc, "</item>");
+
// TODO: Add a description
return self.buffer.items;
}
+
+ fn tag(self: *@This(), openingTag: []const u8, closingTag: []const u8, inside: []const u8) !void {
+ try self.buffer.appendSlice(self.alloc, openingTag);
+ try self.buffer.appendSlice(self.alloc, inside);
+ try self.buffer.appendSlice(self.alloc, closingTag);
+ try self.buffer.append(self.alloc, '\n');
+ }
};
test "rss formatter can format markdown document" {
\\Aujourd'hui, mamam est morte.
;
- var formatter = RssFormatter.init(alloc);
+ const postURL = "https://frustrated-functor.dev/a-post-title.md.html";
+ var formatter = RssFormatter.init(alloc, postURL);
defer formatter.deinit();
var mdDoc = try md.MarkdownDoc.parse(doc, alloc);
\\<pubDate>12 april 2026</pubDate>
\\<title>A post title</title>
+ \\<guid>https://frustrated-functor.dev/a-post-title.md.html</guid>
+ \\<link>https://frustrated-functor.dev/a-post-title.md.html</link>
\\</item>
,
result