aboutsummaryrefslogtreecommitdiff
path: root/src/eval
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2026-04-26 23:10:17 +0200
committerAnhgelus Morhtuuzh <william@herges.fr>2026-04-26 23:17:26 +0200
commitde948492e8b38a79d5db9c506c1b7b82e86c6b12 (patch)
tree122e4c004d37193c64d6b8b89d9a252f7e237cfa /src/eval
parentae6ee68d6f4ef79fef609b4d09b543fc06326e95 (diff)
feat(): support code block
Diffstat (limited to 'src/eval')
-rw-r--r--src/eval/blocks.zig40
1 files changed, 39 insertions, 1 deletions
diff --git a/src/eval/blocks.zig b/src/eval/blocks.zig
index 9ad10ba..8ec42da 100644
--- a/src/eval/blocks.zig
+++ b/src/eval/blocks.zig
@@ -3,7 +3,45 @@ const Allocator = std.mem.Allocator;
const HTML = @import("html/Element.zig");
const Element = @import("Element.zig");
-pub const Code = Element.Simple("pre");
+pub const Code = struct {
+ content: std.ArrayList(Element),
+ attribute: ?[]const u8 = null,
+
+ const Self = @This();
+
+ pub fn init(alloc: Allocator) !*Self {
+ const v = try alloc.create(Self);
+ v.* = .{ .content = try .initCapacity(alloc, 2) };
+ return v;
+ }
+
+ pub fn element(self: *Self) Element {
+ return .{ .ptr = self, .vtable = .{ .deinit = destroy, .html = Self.html } };
+ }
+
+ pub fn deinit(self: *Self, alloc: Allocator) void {
+ destroy(self, alloc);
+ }
+
+ fn destroy(context: *anyopaque, alloc: Allocator) void {
+ var self: *Self = @ptrCast(@alignCast(context));
+ for (self.content.items) |it| it.deinit(alloc);
+ self.content.deinit(alloc);
+ alloc.destroy(self);
+ }
+
+ fn html(context: *anyopaque, alloc: Allocator) HTML.Error!HTML {
+ const self: *Self = @ptrCast(@alignCast(context));
+ var el = try HTML.init(alloc, .content, "pre");
+ errdefer el.deinit();
+ if (self.attribute) |attr| try el.setAttribute("data-code", attr);
+ var code = try HTML.init(alloc, .content, "code");
+ errdefer code.deinit();
+ for (self.content.items) |it| try code.appendContent(try it.html(alloc));
+ try el.appendContent(code);
+ return el;
+ }
+};
pub const Figure = struct {
content: Element,